不確定な数の変数とそのカウントのソートされた配列があります。次のような文字列を作成する必要があります。
Attribute[0]: p=2, e=8
私の問題は、配列が実際にはポインターであり、固定長のループを使用したくないため、唯一の解決策は配列ポインターを使用することです。
void printArray(Node_ptr node){
int count=0; //character count
char *temp= node->attributes; //duplicate attribute array
char cur; //current char
char *outputCat= emalloc(150); //concatenate counts to a single string
strcpy(outputCat, "Attribute %d counts are: ");
qsort(temp, lineCount, sizeof(char), compare); //sort the array
while ((temp) != NULL){
cur= *temp;
++temp;
if (strcmp(&cur, temp)==0) //if characters are same
count++;
else { //build reporting string
strcat(outputCat, &cur);
strcat(outputCat, "= ");
sprintf(outputCat, "%d ", count);
count=0;
}
}
free(outputCat);
}
ここでの問題はstrcmp(&cur, ++temp)==0
、デバッガーで値を確認しても、毎回 false を返すことです。このため、else 条件は常に構築されており、何度か繰り返した後に segfault がスローされます。
2 つの質問:
1-strcmp
同一の値が入力された場合でも、0 以外の値を返すことができるのは何ですか? 2- コードを修正するにはどうすればよいですか?