我有以下类型的结构(带有嵌套):
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
但是,此代码会产生以下输出:
tarskin@5-PARA-11-0120:/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
有谁知道会发生什么?
-- 编辑 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);
}
我展示的测试用例是针对单一化合物的(所以 i = 1)。