C 赋值的左操作数是否需要左值?
这是我的密码:C 赋值的左操作数是否需要左值?,c,arrays,pointers,dynamic-memory-allocation,C,Arrays,Pointers,Dynamic Memory Allocation,这是我的密码: void pwd(Fs_sim *files) { Node *curr_node = files->curr; Node **space = NULL; int i = 0; while (curr_node->parent != NULL) { space = realloc(space, sizeof(Node *) * (i + 1)); *space + i = curr_node;
void pwd(Fs_sim *files) {
Node *curr_node = files->curr;
Node **space = NULL;
int i = 0;
while (curr_node->parent != NULL) {
space = realloc(space, sizeof(Node *) * (i + 1));
*space + i = curr_node;
i++;
curr_node = curr_node->parent;
}
if (i == 0)
printf("/\n");
else {
while (i > 0) {
printf("/%s", (*space + i--)->name);
}
printf("\n");
}
free(space);
}
void pwd(Fs_sim *files) {
Node *curr_node = files->curr;
Node **space = NULL;
int i = 0;
while (curr_node->parent != NULL) {
space = realloc(space, sizeof(Node *) * (i + 1));
*(space + i) = curr_node;
i++;
curr_node = curr_node->parent;
}
if (i == 0)
printf("/\n");
else {
while (i >= 0) {
printf("/%s", (*(space + (i-1)))->name);
i--;
}
printf("\n");
}
free(space);
}
“space”是指向正在动态分配的数组的指针。在遍历每个节点时,我希望在动态分配的数组中存储一个指向该节点的指针,并记录有多少个元素。我收到错误消息:
error: lvalue required as left operand of an assignment
在“*space+i=curr_节点上;”线路
我看不出有什么问题。有人能澄清一下吗
更新:
我已经更改了代码,现在它可以编译了,但是当我运行可执行文件时,我遇到了分段错误。这是我的密码:
void pwd(Fs_sim *files) {
Node *curr_node = files->curr;
Node **space = NULL;
int i = 0;
while (curr_node->parent != NULL) {
space = realloc(space, sizeof(Node *) * (i + 1));
*space + i = curr_node;
i++;
curr_node = curr_node->parent;
}
if (i == 0)
printf("/\n");
else {
while (i > 0) {
printf("/%s", (*space + i--)->name);
}
printf("\n");
}
free(space);
}
void pwd(Fs_sim *files) {
Node *curr_node = files->curr;
Node **space = NULL;
int i = 0;
while (curr_node->parent != NULL) {
space = realloc(space, sizeof(Node *) * (i + 1));
*(space + i) = curr_node;
i++;
curr_node = curr_node->parent;
}
if (i == 0)
printf("/\n");
else {
while (i >= 0) {
printf("/%s", (*(space + (i-1)))->name);
i--;
}
printf("\n");
}
free(space);
}
还是找不到有什么问题
提前谢谢。此处为C。更新前回答:
您似乎已将其翻转,它应该是curr\u node=*space+i
。这是因为您要分配表达式值的变量应该位于左侧,因为您不能将curr\u node
分配到两个变量的总和中
如果这不符合您的目的,您也可以执行更新前回答:
您似乎已将其翻转,它应该是curr\u node=*space+i
。这是因为您要分配表达式值的变量应该位于左侧,因为您不能将curr\u node
分配到两个变量的总和中
如果这不符合您的目的,您也可以执行
*(space+i)=curr\u node
我认为错误如下:
while (i >= 0) {
printf("/%s", (*(space + (i-1)))->name);
i--;
}
当i
为0并且您尝试执行(*(空格+(i-1))->name)时会发生什么情况代码>
您将获得空格+-1
我认为错误在于:
while (i >= 0) {
printf("/%s", (*(space + (i-1)))->name);
i--;
}
当i
为0并且您尝试执行(*(空格+(i-1))->name)时会发生什么情况代码>
您将获得空格+-1
我想你的意思是*(space+I)=curr\u node
@VaughnCato:*(space+i)
最好写成space[i]
。我想你的意思是*(space+i)=curr\u节点
@VaughnCato:*(space+i)
最好写成space[i]
。谢谢你,一个愚蠢的错误。我太专注于内存分配了,忘了检查这样的事情。谢谢你,一个愚蠢的错误。我太专注于内存分配,忘记了检查类似的事情。