使用指针打印值
我有一个由一些不确定数量的变量及其计数组成的排序数组。我需要像这样构建一个字符串:
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 条件会不断构建,并在多次迭代后抛出段错误。
两个问题:
1- 即使输入相同的值,什么才能使 strcmp
返回非零? 2- 我可以做什么来修复代码?
I have a sorted array of some indeterminate number of variables and counts thereof. I need to build a string like so:
Attribute[0]: p=2, e=8
My issue is the array is actually a pointer, and I don't want to use a loop of fixed length, so the only solution I see is to use an array pointer.
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);
}
The issue here is strcmp(&cur, ++temp)==0
is returning false every time, even when I see their values in the debugger. Because of this, the else conditional is constantly being built up and throws a segfault after a number of iterations.
Two questions:
1- What can make strcmp
return non-zero even when identical values are entered?
2- What can I do to fix the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的行中:
cur
是本地声明的char
,因此,&cur
只是堆栈上的某个位置,在这个背景。我相信您的意思是检查当前字符
cur
是否与下一个字符*temp
相同。这看起来像:
接下来,我会看看大规模简化你的输出部分:
最后,我怀疑你的循环是否会终止,因为它继续执行
temp++
,而temp != NULL
。我相信你打算检查存储在指针处的 VALUE
临时
。应根据“\0”而不是 NULL 正确检查
*temp
。(\0 和 NULL 碰巧具有相同的值,但它们在语义上不应该被视为相同)
PS 您的简单但出色的评论“// if strings are same “对我理解你的代码非常有帮助。这是一个简短而有意义的评论无价值的绝佳案例。谢谢。
(希望是最终编辑)
总的来说,我建议的改变是这样的:
你觉得效果如何?
In your line:
cur
is achar
declared locally, and therefore,&cur
is just some location on the stack, and rather meaningless in this context.I believe you mean to check if the current-character
cur
is the same as the next character*temp
.This would look like:
Next, I would look at massively simplify your output section:
And finally, I doubt your loop will ever terminate, as it continues to do
temp++
, whiletemp != NULL
.I believe you intend to check the VALUE stored at the pointer
temp
.*temp
should be properly checked against '\0', instead of NULL.(\0 and NULL happen to have the same value, but they should not be treated the same semantically)
P.S. Your simple, but excellent comment "// if characters are same" was extremely helpful for me to understand your code. This is an excellent case of short, meaningful comments being INVALUABLE. Thank you.
(Hopefully final edit)
In total, the changes I recommend look like:
How's that work out for you?