C 如何在结构中正确使用指针数组
正如您在下面的代码中看到的,我有两个结构:父结构和子结构。父结构有一个类型为child的指针数组。当程序进入for循环时,我得到一个分段错误。我的代码有什么错误吗? 我不想使用方括号的原因是,我有一个函数,它接受child类型的指针参数,并且我想将每个子指针传递给该函数,而不需要使用& 任何帮助都将不胜感激 多谢各位C 如何在结构中正确使用指针数组,c,pointers,memory-management,malloc,structure,C,Pointers,Memory Management,Malloc,Structure,正如您在下面的代码中看到的,我有两个结构:父结构和子结构。父结构有一个类型为child的指针数组。当程序进入for循环时,我得到一个分段错误。我的代码有什么错误吗? 我不想使用方括号的原因是,我有一个函数,它接受child类型的指针参数,并且我想将每个子指针传递给该函数,而不需要使用& 任何帮助都将不胜感激 多谢各位 #include <stdio.h> #include <string.h> #include <stdlib.h> typedef stru
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
int id;
} child;
typedef struct {
child** c;
} parent;
int main(int argc, char **argv) {
int number_of_children = 5;
parent* p = (parent*)malloc(sizeof(parent));
p -> c = (child **) malloc(number_of_children * sizeof(child*));
int i;
for(i=0; i<number_of_children; i++)
p -> c[i] -> id = i;
}
#包括
#包括
#包括
类型定义结构{
int-id;
}儿童;
类型定义结构{
儿童**c;
}父母;
int main(int argc,字符**argv){
子项的整数=5;
父*p=(父*)malloc(sizeof(父));
p->c=(子**)malloc(子数量*子大小*);
int i;
对于(i=0;ic[i]>id=i;
}
您正确分配了子指针表,但没有分配任何子指针
int i
for(i = 0; i < number_of_children; ++i) {
p->c[i] = (child *) malloc(sizeof(child));
p -> c[i] -> id = i
}
inti
对于(i=0;i<子项的数量;++i){
p->c[i]=(子*)malloc(sizeof(子));
p->c[i]->id=i
}
您正确分配了子指针表,但没有分配任何子指针
int i
for(i = 0; i < number_of_children; ++i) {
p->c[i] = (child *) malloc(sizeof(child));
p -> c[i] -> id = i
}
inti
对于(i=0;i<子项的数量;++i){
p->c[i]=(子*)malloc(sizeof(子));
p->c[i]->id=i
}
未经测试,但应符合以下要求:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
int id;
} child;
typedef struct {
child** c;
} parent;
int main(int argc, char **argv) {
const int number_of_children = 5;
parent* p = malloc(sizeof(parent));
if(p == NULL) halt_and_catch_fire();
p->c = malloc(number_of_children * sizeof(child*));
if(p->c == NULL) halt_and_catch_fire();
for(int i=0; i<number_of_children; i++)
{
p->c[i] = malloc(sizeof(child));
if(p->c[i] == NULL) halt_and_catch_fire();
p->c[i]->id = i;
}
// free() *everything* allocated
}
#包括
#包括
#包括
类型定义结构{
int-id;
}儿童;
类型定义结构{
儿童**c;
}父母;
int main(int argc,字符**argv){
const int number_of_children=5;
父*p=malloc(sizeof(父));
如果(p==NULL)停止并引发火灾();
p->c=malloc(子项数*子项数*);
如果(p->c==NULL)停止并引发火灾();
对于(int i=0;ic[i]=malloc(sizeof(child));
如果(p->c[i]==NULL)停止并引发火灾();
p->c[i]->id=i;
}
//free()*所有内容*已分配
}
正如我们所看到的,您正试图分配一个分段指针到指针对象,这显然是没有必要的,因为您不分配多个最内部的对象。如果您试图创建多维数组,您不应该制造一个分段混乱,。未经测试,但应该是以下内容:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
int id;
} child;
typedef struct {
child** c;
} parent;
int main(int argc, char **argv) {
const int number_of_children = 5;
parent* p = malloc(sizeof(parent));
if(p == NULL) halt_and_catch_fire();
p->c = malloc(number_of_children * sizeof(child*));
if(p->c == NULL) halt_and_catch_fire();
for(int i=0; i<number_of_children; i++)
{
p->c[i] = malloc(sizeof(child));
if(p->c[i] == NULL) halt_and_catch_fire();
p->c[i]->id = i;
}
// free() *everything* allocated
}
#包括
#包括
#包括
类型定义结构{
int-id;
}儿童;
类型定义结构{
儿童**c;
}父母;
int main(int argc,字符**argv){
const int number_of_children=5;
父*p=malloc(sizeof(父));
如果(p==NULL)停止并引发火灾();
p->c=malloc(子项数*子项数*);
如果(p->c==NULL)停止并引发火灾();
对于(int i=0;ic[i]=malloc(sizeof(child));
如果(p->c[i]==NULL)停止并引发火灾();
p->c[i]->id=i;
}
//free()*所有内容*已分配
}
正如我们所看到的,您正试图分配一个分段指针到指针对象,这显然是没有必要的,因为您不分配多个最内部的对象。如果您试图创建多维数组,您不应该制造分段混乱。您还需要为
子对象*
分配内存指向。谢谢,问题解决你还需要为子项所指向的内容分配内存。谢谢,问题解决非常感谢,问题解决非常感谢,问题解决谢谢,那很有帮助谢谢,那很有帮助