C 指向嵌套结构中的结构的指针
我正在尝试运行以下代码(在Fedora11i586上的GCC4.3中):C 指向嵌套结构中的结构的指针,c,pointers,struct,C,Pointers,Struct,我正在尝试运行以下代码(在Fedora11i586上的GCC4.3中): #包括 #包括 #包括 struct s_smallstruct{ int smallstruct; }; 结构s_测试2{ char*test2; struct s_smallstruct*smallstruct; }; 结构s_测试3{ char*test3; struct s_smallstruct*smallstruct; }; 结构s_测试1{ char*test1;
#包括
#包括
#包括
struct s_smallstruct{
int smallstruct;
};
结构s_测试2{
char*test2;
struct s_smallstruct*smallstruct;
};
结构s_测试3{
char*test3;
struct s_smallstruct*smallstruct;
};
结构s_测试1{
char*test1;
结构s_test2*test2;
结构s_test3*test3;
};
int main(){
结构s_test1*test1=(结构s_test1*)malloc(test1的大小);
test1->test2[0]。smallstruct[0]。smallstruct=123;
int num=test1->test2[0]。smallstruct[0]。smallstruct;
//struct s_smallstruct*smallstruct=(struct s_smallstruct*)malloc(smallstruct的大小);
//smallstruct[0]。smallstruct=12;
//int num=smallstruct[0].smallstruct;
printf(“%d\n”,num);
返回退出成功;
}
但是我在test1->test2[0]中遇到了一个segfault。smallstruct[0]。smallstruct=123。注释部分正在运行,没有错误。这种行为的原因是什么。我对C不是很精通,因此我非常感谢任何帮助。尝试更改:
struct s_test1 *test1 = (struct s_test1 *) malloc( sizeof test1 );
到
test1
是一个指针,在32位环境中是4个字节。我可以看到您的代码有三个问题:
const int length = 2;
struct s_test1 *test1 = malloc( length * sizeof *test1 );
test1->test2 = malloc( length * sizeof *test1->test2 );
test1->test2->smallstruct = malloc( length * sizeof *test1->test2->smallstruct );
test1[1].test2[0].smallstruct[1].smallstruct = 123;
int num = test1[1].test2[0].smallstruct[1].smallstruct;
您没有为内部结构分配任何内存。sizeof(test1)只有足够的空间容纳3个指针,而不是整个结构
另外,该语句中有5个(!)解引用运算符。即使您分配了足够大的内存块,您也没有以确保其连续性的方式进行布局——您要求它在块之间跳跃5次。我编译了问题海报@systemsfault标记为正确的代码,但却丢弃了核心。我希望在这里进行健康有益的讨论,为这个问题提供正确的解决方案。我也是stackoverflow的新手,所以如果我说错了什么,请随时纠正我。现在,我们开始 由于旧的解决方案不适用,我尝试了两个步骤来解决它。首先,我添加了for循环,但仍然得到了由(a1)和(a2)引起的内核转储 接下来,我使用行(b1)和(b2)来替换(a1)和(a2)。现在它已编译并正确运行 我已清理了结构,使其更易于阅读:
#include <stdio.h>
#include <stdlib.h>
struct s_smallstruct{
int smallstruct;
};
struct s_test2{
struct s_smallstruct *smallstruct;
};
struct s_test1{
struct s_test2 *test2;
};
int main() {
int i, j, length = 2;
struct s_test1 *test1 = malloc( length * sizeof *test1 );
for (i=0; i<length; i++) {
//test1[i].test2 = malloc( length * sizeof *test1->test2 );//(a1)
test1[i].test2 = malloc( length * sizeof *test1[i].test2 );//(b1)
for (j=0; j<length; j++) {
//test1[i].test2[i].smallstruct = malloc( length * sizeof *test1->test2->smallstruct );//(a2)
test1[i].test2[j].smallstruct = malloc( length * sizeof *test1[i].test2[j].smallstruct );//(b2)
}
}
test1[1].test2[0].smallstruct[1].smallstruct = 123;
int num = test1[1].test2[0].smallstruct[1].smallstruct;
printf("num:%d\n", num);
return 0;
}
#包括
#包括
struct s_smallstruct{
int smallstruct;
};
结构s_测试2{
struct s_smallstruct*smallstruct;
};
结构s_测试1{
结构s_test2*test2;
};
int main(){
int i,j,长度=2;
结构s_test1*test1=malloc(长度*sizeof*test1);
对于(i=0;itest2);//(a1)
test1[i].test2=malloc(长度*sizeof*test1[i].test2);/(b1)
对于(j=0;jtest2->smallstruct);/(a2)
test1[i].test2[j].smallstruct=malloc(长度*sizeof*test1[i].test2[j].smallstruct);/(b2)
}
}
test1[1]。test2[0]。smallstruct[1]。smallstruct=123;
int num=test1[1]。test2[0]。smallstruct[1]。smallstruct;
printf(“num:%d\n”,num);
返回0;
}
更好的是:malloc(sizeof*test1);这不是解决方案,但请始终选中*ALLOC!!
const int length = 2;
struct s_test1 *test1 = malloc( length * sizeof *test1 );
test1->test2 = malloc( length * sizeof *test1->test2 );
test1->test2->smallstruct = malloc( length * sizeof *test1->test2->smallstruct );
test1[1].test2[0].smallstruct[1].smallstruct = 123;
int num = test1[1].test2[0].smallstruct[1].smallstruct;
#include <stdio.h>
#include <stdlib.h>
struct s_smallstruct{
int smallstruct;
};
struct s_test2{
struct s_smallstruct *smallstruct;
};
struct s_test1{
struct s_test2 *test2;
};
int main() {
int i, j, length = 2;
struct s_test1 *test1 = malloc( length * sizeof *test1 );
for (i=0; i<length; i++) {
//test1[i].test2 = malloc( length * sizeof *test1->test2 );//(a1)
test1[i].test2 = malloc( length * sizeof *test1[i].test2 );//(b1)
for (j=0; j<length; j++) {
//test1[i].test2[i].smallstruct = malloc( length * sizeof *test1->test2->smallstruct );//(a2)
test1[i].test2[j].smallstruct = malloc( length * sizeof *test1[i].test2[j].smallstruct );//(b2)
}
}
test1[1].test2[0].smallstruct[1].smallstruct = 123;
int num = test1[1].test2[0].smallstruct[1].smallstruct;
printf("num:%d\n", num);
return 0;
}