C 为双指针指定一个值
我为r分配了内存,r是指向struct的双指针。 然而,当我试图使用这个变量时,我得到了一个错误,它说: 错误消息 用法 宣言 宣言C 为双指针指定一个值,c,C,我为r分配了内存,r是指向struct的双指针。 然而,当我试图使用这个变量时,我得到了一个错误,它说: 错误消息 用法 宣言 宣言 intb; 结构命令**r; r=(结构命令**)malloc(50*sizeof(结构命令*); 对于(b=0;bC赋值的一般语法 Lvalue=Rvalue; 以上Rvalue可以是表达式或函数调用的结果,也可以是相同类型或常量的另一个变量,Lvalue是能够存储Rvalue的变量 r+i = postfixToTree(postfix_string
intb;
结构命令**r;
r=(结构命令**)malloc(50*sizeof(结构命令*);
对于(b=0;bC赋值的一般语法
Lvalue=Rvalue;
以上Rvalue可以是表达式或函数调用的结果,也可以是相同类型或常量的另一个变量,Lvalue是能够存储Rvalue的变量
r+i = postfixToTree(postfix_string, &csPtr->nodeArray[i]);
^^^
正在尝试将i值添加到r,以代替Lvalue
在C中,左侧不应有任何评估部分
先添加,然后分配
r=r+i; //r+=i;
r = postfixToTree(postfix_string, &csPtr->nodeArray[i]);
你可以把上面的陈述写在下面
r[i] = postfixToTree(postfix_string, &csPtr->nodeArray[i]);
每次调用函数postfixToTree()
(b=0;b我认为你在指针方面做得不对
这样做,
int b;
struct command** r;
r = (struct command**) malloc(50 * sizeof(struct command*));
for( b=0; b<50; b++){
*(r) + b=(struct command*) malloc(50*sizeof(struct command));
}
intb;
结构命令**r;
r=(结构命令**)malloc(50*sizeof(结构命令*);
对于(b=0;b这是您想要的吗?请确保包含stdlib.h
,不需要malloc强制转换,*(r+b)
相当于r[b]
#include <stdlib.h>
struct command {
int x; // just a definition to compile
};
int main()
{
int b;
struct command** r;
r = malloc(50 * sizeof(struct command*));
for( b=0; b<50; b++) {
r[b] = malloc(50*sizeof(struct command));
}
}
#包括
结构命令{
int x;//只是一个要编译的定义
};
int main()
{
int b;
结构命令**r;
r=malloc(50*sizeof(struct-command*));
对于(b=0;b什么是postfix树
返回?另外,r+i
是临时的,即使可以修改它,修改也会立即丢失。您到底想修改什么?请在问题中添加postfix树()的完整声明。
(可能在第一个声明之后
标题。您不喜欢数组下标吗?使用r[b]=(struct command*)malloc(50*sizeof(struct command));
是明智的。您的r+i
代码也应该是r[i]
或*(r+i)
,尽管这样做会泄漏内存(相当多的内存—每次执行postfixToTree()时,都会有50个struct命令副本)
调用。您的代码和错误消息不一致,但您是否#include
?malloc
的演员阵容可能隐藏了malloc未正确声明的事实。这可能不是我们想要的,内存泄漏的可能性很大(在原始代码和大纲解决方案中)。有没有不使用r[i]
表示法的特殊原因?对不起,OP可以使用r[i]。函数中的内存分配会导致巨大的内存泄漏。感谢您指出这一点。
r[i] = postfixToTree(postfix_string, &csPtr->nodeArray[i]);
for( b=0; b<50; b++)
*(r+b)=(struct command*) malloc(50*sizeof(struct command));
int b;
struct command** r;
r = (struct command**) malloc(50 * sizeof(struct command*));
for( b=0; b<50; b++){
*(r) + b=(struct command*) malloc(50*sizeof(struct command));
}
#include <stdlib.h>
struct command {
int x; // just a definition to compile
};
int main()
{
int b;
struct command** r;
r = malloc(50 * sizeof(struct command*));
for( b=0; b<50; b++) {
r[b] = malloc(50*sizeof(struct command));
}
}