我有一個結構(具有嵌套)變化:值存儲在C結構「神奇」以下類型的自己
typedef struct {
float precursor_mz;
float precursor_int;
int scan;
float time;
spectrum* spectra; /* Nested struct */
int array_length;
int mz_length;
int int_length;
char* mz_binary;
char* int_binary;
int hits;
} compound;
typedef struct {
float mz_value;
float int_value;
int peaks;
} spectrum;
我改變這個結構,允許我使用的qsort,後我將它存儲爲我自己的'類型'。幾行後面的代碼我希望循環結構,但不知何故值更改沒有我訪問他們(之間)。代碼片斷如下:
// The transformating & qsort chunk
for (i = 0; i < compounds->hits; i++) {
spectrum test[(compounds+i)->spectra->peaks];
for (j = 0; j < (compounds+i)->spectra->peaks; j++) {
test[j] = *((compounds+i)->spectra+j);
}
qsort(test,(compounds+i)->spectra->peaks,sizeof(spectrum),compare_mz);
for (j = 0; j < (compounds+i)->spectra->peaks; j++) {
((compounds+i)->spectra+j)->mz_value = test[j].mz_value;
((compounds+i)->spectra+j)->int_value = test[j].int_value;
if (j < 10) {
printf("%i %i\t", i, j);
printf("%f %f\n",((compounds+i)->spectra+j)->mz_value, ((compounds+i)->spectra+j)->int_value); // Here values are still correct
}
}
}
/* Summing values that are in 'mass-tolerance' of each other */
float int_total;
float mz_int_total;
for (i = 0; i < compounds->hits; i++) {
counter = 0;
printf("---\n");
for (j = 0; j < (compounds+i)->spectra->peaks; j++) {
lower_mass = ((compounds+i)->spectra+j)->mz_value - 0.05; //args->mass_tolerance;
upper_mass = ((compounds+i)->spectra+j)->mz_value + 0.05; //args->mass_tolerance;
if (j < 10) {
printf("%i %i\t", i , j);
printf("%f %f\n",((compounds+i)->spectra+j)->mz_value, ((compounds+i)->spectra+j)->int_value); // Here values are borked
}
// Rest of the code chopped off as it should be irrelevant
但此代碼產生以下輸出:
[email protected]:/data/programming/C/Compound_Spectra$ ./Run -f ../PeptMoiety/32757_p_01.mzML -c 1
0 0 168.858765 32489.994141
0 1 168.960327 72930.046875
0 2 169.039993 4924.188477
0 3 169.913681 85340.171875
0 4 169.932312 2406.798096
0 5 171.000320 345949.593750
0 6 171.007950 1034718.312500
0 7 171.034088 882886.562500
0 8 171.034378 58554.589844
0 9 171.056320 871035.500000
---
0 0 168.858765 32489.994141
0 1 168.960327 72930.046875
0 2 169.039993 4924.188477
0 3 169.913681 85340.171875
0 4 0.000000 0.000000
0 5 169.932312 2406.798096
0 6 171.007950 1034718.312500
0 7 0.000000 0.000000
0 8 0.000000 0.000000
0 9 0.000000 0.000000
沒有人有任何想法可能是什麼回事?
- EDIT 1 -
ALK請求compare_mz的代碼,其中如下:
int
compare_mz (const void *a, const void *b)
{
const spectrum *fa = (const spectrum *) a;
const spectrum *fb = (const spectrum *) b;
return (fa->mz_value > fb->mz_value)
-(fa->mz_value < fb->mz_value);
}
測試用例,我表明是爲單一化合物(所以我= 1)。
可以簡化測試案例?現在有太多東西在進行着。 – 2012-04-19 13:00:39
嗯..你有相當多的調試在那裏做。我會將數據集簡化爲兩個無序的項目,然後逐步執行調試程序,並仔細記錄值和索引。隨着對完整代碼的熟悉,編譯以及知道我在做什麼,您應該能夠找到問題。 – 2012-04-19 13:01:37
你還可以提供'compare_mz'的代碼嗎? – alk 2012-04-19 13:09:35