将内存分配给结构中的char *变量 [英] Allocate memory to char * variables in structure

查看:79
本文介绍了将内存分配给结构中的char *变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在做一个我正在做的家庭作业项目,所以要转发,我是新的

C编程。但无论如何,在内存中遇到一些问题

分配问题与char *相关,这是一个变量,它是一个变量,它是一个

结构中的变量。我一直在收到分段错误错误,而且我很难理解为什么。这是有问题的代码部分...


这是我们定义的结构的.h文件的一部分......


enum color {COLORS};

typedef enum color颜色;


struct person

{

char * firstName;

char * lastName;

char *业余爱好;

颜色favColor;

char sex;

int age;

};

typedef struct person Person;


struct node

{

struct node * next;

Person client;

};

typedef结构节点节点;


现在这里是我遇到问题的功能...如果你看看

下到那里找到这一行...


member-> firstName =(char *)calloc(strlen(fname)+1,sizeof(char));


编译器在那里停止并生成seg错误消息。这是'/ b $ b全功能。顺便说一下,我测试了

函数的第一部分,文件指针存在,从printf

到stdout的输出工作正常,直到我得到上面的行。然后程序崩溃。


我是否试图以错误的方式分配内存?

Person * createMember(FILE * infile)

{

char fname [30];

char lname [30];

char sex [10];

char age [10];

char color [20];

char hobby [30];

char * colorS;

人*会员;


if(!feof(infile))

{

fgets (fname,30,infile);

fgets(lname,30,infile);

fgets(性别,10,infile);

fgets(年龄10岁,infile);

fgets(颜色,20,infile);

fgets(业余爱好,30岁,infile);

}


else

返回NULL;

printf(" top of createMember \ n");

/ *从文件中读取成员属性* /

fgets(fname,sizeof fname,infile);

fname [strlen(fname)-1] = ''\''';

fgets(lname,sizeof lname,infile);

lname [strlen(lname)-1] =''\\ 0'';

fgets(性别,性别,性别,infile);

性别[strlen(性别)-1] =''\ 0'';

fgets(年龄,年龄,年龄,年龄);

年龄[strlen(年龄)-1] =''\ 0'';

fgets(颜色,颜色,颜色,infile);

颜色[strlen(color)-1] =''\ 0'';

fgets(业余爱好,业余爱好) ,infile);

爱好[strlen(业余爱好)-1] =''\ 0'';

printf(通过fgets语句。\ n ;);


/ *为成员属性创建内存分配* /

member-> firstName =(char *)calloc(strlen(fname)+ 1,sizeof(char));

printf(" calloc firstName \ n");

member-> lastName =(char *)malloc(sizeof( fname)* sizeof(char));

member-> hobby =(char *)malloc(sizeof(hobby)* sizeof(char));

colorS = (char *)malloc(sizeof(color)* sizeof(char));

printf(过去malloc statements \ n);


/ *商店成员属性* /

strcpy(member-> firstName,fname);

printf(" fname processed\\\
);

sscanf(lname,"%s",member-> lastName);

sscanf(sex,"%c",member-> sex);

sscanf(年龄,%d,会员 - >年龄);

strncpy(colorS,color,sizeof(color));

member-> favColor = readColor(colorS);

sscanf(业余爱好,%s,会员 - >爱好);

printf(" createMember \\\
的底部) ;);


返回会员;

}


David

Hi,

I have a homework project I am working on, so be forwarned, I''m new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable inside of a
structure. I keep getting segmentation fault errors and I am having
trouble understanding why. Here''s the parts of the code in question...

This is part of the .h file where the struct us defined...

enum color { COLORS };
typedef enum color Color;

struct person
{
char *firstName;
char *lastName;
char *hobby;
Color favColor;
char sex;
int age;
};
typedef struct person Person;

struct node
{
struct node *next;
Person client;
};
typedef struct node Node;

Now here is the function that I am having trouble with... If you look
down to the section where this line is found...

member->firstName = (char *)calloc(strlen(fname)+1, sizeof(char));

The compiler stops there and generates the seg fault message. Here''s
the full function. By the way I have tested the first part of the
function, the file pointer exists and the output to stdout from printf
is working ok until I get the line above. Then the program crashes.

Am I trying to allocate the memory in the wrong way?
Person *createMember(FILE *infile)
{
char fname[30];
char lname[30];
char sex[10];
char age[10];
char color[20];
char hobby[30];
char *colorS;
Person *member;

if (!feof(infile))
{
fgets(fname, 30, infile);
fgets(lname, 30, infile);
fgets(sex, 10, infile);
fgets(age, 10, infile);
fgets(color, 20, infile);
fgets(hobby, 30, infile);
}

else
return NULL;
printf("top of createMember\n");
/* read member attributes from file */
fgets(fname, sizeof fname, infile);
fname[strlen(fname)-1] = ''\0'';
fgets(lname, sizeof lname, infile);
lname[strlen(lname)-1] = ''\0'';
fgets(sex, sizeof sex, infile);
sex[strlen(sex)-1] = ''\0'';
fgets(age, sizeof age, infile);
age[strlen(age)-1] =''\0'';
fgets(color, sizeof color, infile);
color[strlen(color)-1] = ''\0'';
fgets(hobby, sizeof hobby, infile);
hobby[strlen(hobby)-1] = ''\0'';
printf("got through fgets statements.\n");

/* create memory allocation for member attributes */
member->firstName = (char *)calloc(strlen(fname)+1, sizeof(char));
printf("calloc firstName\n");
member->lastName = (char *)malloc(sizeof(fname) * sizeof(char));
member->hobby = (char *)malloc(sizeof(hobby) * sizeof(char));
colorS = (char *)malloc(sizeof(color) * sizeof(char));
printf("got past malloc statements\n");

/* store member attributes */
strcpy(member->firstName, fname);
printf("fname processed\n");
sscanf(lname, "%s", member->lastName);
sscanf(sex, "%c", member->sex);
sscanf(age, "%d", member->age);
strncpy(colorS, color, sizeof(color));
member->favColor = readColor(colorS);
sscanf(hobby, "%s", member->hobby);
printf("bottom of createMember\n");

return member;
}

David

推荐答案

dt ******* @ gmail.com 写道:




我有一个我正在做的家庭作业项目,所以要转发,我'很新的

C编程。但无论如何,在内存中遇到一些问题

分配问题与char *相关,这是一个变量,它是一个变量,它是一个

结构中的变量。我一直在收到分段错误错误,而且我很难理解为什么。这是有问题的代码部分...
Hi,

I have a homework project I am working on, so be forwarned, I''m new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable inside of a
structure. I keep getting segmentation fault errors and I am having
trouble understanding why. Here''s the parts of the code in question...


>


现在这里是我遇到麻烦的功能...如果你看到这个行找到的那个部分是

...


member-> ; firstName =(char *)calloc(strlen(fname)+1,sizeof(char));
>

Now here is the function that I am having trouble with... If you look
down to the section where this line is found...

member->firstName = (char *)calloc(strlen(fname)+1, sizeof(char));



永远不要转换calloc / malloc / realloc的返回值。 sizeof(char)也是通过

定义1.删除强制转换并确保包含< stdlib.h>

Never cast the return of calloc/malloc/realloc. Also sizeof(char) is by
definition 1. Remove the casts and make sure you have included <stdlib.h>


编译器在那里停止并生成seg错误消息。
The compiler stops there and generates the seg fault message.



我假设你应该说你运行它时程序会崩溃吗?

I assume you should have said the program crashes when you run it?


sscanf(性别,%c,会员 - >性别);

sscanf(年龄,%d,会员 - >年龄);
sscanf(sex, "%c", member->sex);
sscanf(age, "%d", member->age);



以上应该使用& member-> sex和& ember->年龄。


-

Ian Collins。

the above should use &member->sex and &ember->age.

--
Ian Collins.


dt ** *****@gmail.com 写道:




我有一个家庭作业项目我正在努力,所以转发,我是新的

C编程。但无论如何,在内存中遇到一些问题

分配问题与char *相关,这是一个变量,它是一个变量,它是一个

结构中的变量。我一直在收到分段错误错误,而且我很难理解为什么。这是有问题的代码部分...


这是我们定义的结构的.h文件的一部分......


enum color {COLORS};
Hi,

I have a homework project I am working on, so be forwarned, I''m new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable inside of a
structure. I keep getting segmentation fault errors and I am having
trouble understanding why. Here''s the parts of the code in question...

This is part of the .h file where the struct us defined...

enum color { COLORS };



只有一个枚举常量?

Only one enumeration constant?


typedef enum color color;


struct person

{

char * firstName;

char * lastName;

char * hobby ;

颜色favColor;

char性别;

int age;

};

typedef struct person Person;


struct node

{

struct node * next;

人客户;

};

typedef struct node节点;


现在这里是我遇到问题的功能...如果你看到

到找到这一行的部分......


member-> firstName =(char *)calloc( strlen(fname)+ 1,sizeof(char));
typedef enum color Color;

struct person
{
char *firstName;
char *lastName;
char *hobby;
Color favColor;
char sex;
int age;
};
typedef struct person Person;

struct node
{
struct node *next;
Person client;
};
typedef struct node Node;

Now here is the function that I am having trouble with... If you look
down to the section where this line is found...

member->firstName = (char *)calloc(strlen(fname)+1, sizeof(char));



sizeof(char)总是C中的一个。也不需要转换calloc的返回值

,(或malloc或realloc) ),因为它可以隐藏某些错误。

sizeof(char) is always one in C. Also no need to cast the return value
of calloc, (or malloc or realloc), as it can hide certain mistakes.


编译器停在那里并生成seg错误消息。
The compiler stops there and generates the seg fault message.



您的意思是编译因分段错误而停止。如果是这样的话,那么

编译器就坏了。

You mean compilation stops with a segmentation fault. If so the
compiler is broken.


这里是'

全功能。顺便说一下,我测试了

函数的第一部分,文件指针存在,从printf

到stdout的输出工作正常,直到我得到上面的行。程序崩溃了。


我是否试图以错误的方式分配内存?


Person * createMember(FILE * infile)< br $>
{

char fname [30];

char lname [30];

char sex [10];

char年龄[10];

char color [20];

char hobby [30];

char * colorS;

人*会员;


if(!feof(infile))
Here''s
the full function. By the way I have tested the first part of the
function, the file pointer exists and the output to stdout from printf
is working ok until I get the line above. Then the program crashes.

Am I trying to allocate the memory in the wrong way?
Person *createMember(FILE *infile)
{
char fname[30];
char lname[30];
char sex[10];
char age[10];
char color[20];
char hobby[30];
char *colorS;
Person *member;

if (!feof(infile))



在C中,使用feof和ferror _ after_调用其中一个I / O
函数报告失败。

In C, feof and ferror are used _after_ a call to one of the I/O
functions have reported failure.


{

fgets(fname,30,infile);

fgets(lname,30,infile);

fgets(性别,10,infile) ;

fgets(年龄10岁,infile);

fgets(颜色,20,infile);

fgets(业余爱好,30岁,infile) );
{
fgets(fname, 30, infile);
fgets(lname, 30, infile);
fgets(sex, 10, infile);
fgets(age, 10, infile);
fgets(color, 20, infile);
fgets(hobby, 30, infile);



检查所有这些调用是否失败。你是否还确定各种

字段小于数组大小?

Check all these calls for failure. Are you also sure that the various
fields will be less than the array sizes?


}


else

返回NULL;

printf(" top of createMember \ n");

/ *读取成员属性来自file * /

fgets(fname,sizeof fname,infile);
}

else
return NULL;
printf("top of createMember\n");
/* read member attributes from file */
fgets(fname, sizeof fname, infile);



为什么要再次覆盖fname?

Why are you overwriting fname again?


fname [strlen(fname)-1] =' '\0'';
fname[strlen(fname)-1] = ''\0'';



这已经由fgets完成了。

This will already be done for you by fgets.


fgets(lname,sizeof lname,infile );

lname [strlen(lname)-1] =''\ 0'';

fgets(性别,性别,性别),

sex [strlen(sex)-1] =''\ 0'';

fgets(年龄,年龄,年龄);

年龄[ strlen(age)-1] =''\ 0'';

fgets(颜色,sizeof颜色,infile);

color [strlen(color)-1 ] =''\ 0'';

fgets(业余爱好,业余爱好,虚弱);

爱好[strlen(业余爱好)-1] =''\ 0'';

printf(通过fgets statements.\ n获得);
fgets(lname, sizeof lname, infile);
lname[strlen(lname)-1] = ''\0'';
fgets(sex, sizeof sex, infile);
sex[strlen(sex)-1] = ''\0'';
fgets(age, sizeof age, infile);
age[strlen(age)-1] =''\0'';
fgets(color, sizeof color, infile);
color[strlen(color)-1] = ''\0'';
fgets(hobby, sizeof hobby, infile);
hobby[strlen(hobby)-1] = ''\0'';
printf("got through fgets statements.\n");



所有这些陈述都与你之前在if构造中写的
重复。之前使用

读取的覆盖值可能是错误的值。这真的是你想要的吗?


另外检查fgets语句是否失败。不要只是假设

已安全通过。

All these statements are duplicates of the what you''d written earlier
within the if construct. The overwrite values read earlier with
possibly wrong values. Is this really what you want?

Also check the fgets statements for failure. Don''t simply assume to
have got through safely.


/ *为成员属性创建内存分配* /

member-> firstName =(char *)calloc(strlen(fname)+ 1,sizeof(char));
/* create memory allocation for member attributes */
member->firstName = (char *)calloc(strlen(fname)+1, sizeof(char));



这是你的主要错误。 member是指向Person类型的指针,(实际上是
类型为person的结构),但是它还没有指向

一个Person的有效实例宾语。你需要定义一个Person对象,

设置成员指向它,然后为

字段进行内存分配,如:


人p1;

member =& p1;

/ * ... * /

Here''s your main error. member is pointer to a Person type, (which is
actually a struct of type person), but as yet it doesn''t point to
valid instance of a Person object. You need to define a Person object,
set member to point to it and then do the memory allocation for the
fields like:

Person p1;
member = &p1;
/* ... */


printf(" calloc firstName \ n");

member-> lastName =(char *)malloc(sizeof(fname)* sizeof(char));

member-> hobby =(char *)malloc(sizeof(hobby)* sizeof(char));

colorS =(char *)malloc(sizeof(color)* sizeof(char) );
printf("calloc firstName\n");
member->lastName = (char *)malloc(sizeof(fname) * sizeof(char));
member->hobby = (char *)malloc(sizeof(hobby) * sizeof(char));
colorS = (char *)malloc(sizeof(color) * sizeof(char));



与上述相同。也不要转换* alloc的返回值。我会把这些

语句写成:

Same as above. Also don''t cast return value of *alloc. I''d write these
statements as:


printf(get past malloc statements\\\
);
printf("got past malloc statements\n");



无错误检查。

Without error checking.


/ *商店会员属性* /

strcpy(member-> firstName,fname);

printf(" fname processed\\\
");

sscanf(lname,"%s",成员 - > lastName的);
/* store member attributes */
strcpy(member->firstName, fname);
printf("fname processed\n");
sscanf(lname, "%s", member->lastName);



为什么strcpy用于firstName和sscanf用于lastName?

Why strcpy for firstName and sscanf for lastName?


sscanf(sex,"%c" ,会员 - >性别);

sscanf(年龄,%d,会员 - >年龄);

strncpy(颜色,颜色,尺寸(颜色) ));

member-> favColor = readColor(colorS);

sscanf(业余爱好,"%s",member->业余爱好);

printf(" createMember\\\
"的底部);


返回会员;

}
sscanf(sex, "%c", member->sex);
sscanf(age, "%d", member->age);
strncpy(colorS, color, sizeof(color));
member->favColor = readColor(colorS);
sscanf(hobby, "%s", member->hobby);
printf("bottom of createMember\n");

return member;
}



确保你的Person对象不是本地对象,因为如果是这样的话,当执行离开这个函数时,它会被摧毁
。动态

分配对象或使其成为文件范围之一。

Make sure your Person object is not a local one, since, if so, it''ll
get destroyed when execution leaves this function. Either dynamically
allocate the object or make it a file scope one.


3月4日,下午10:02,Ian Collins< ian-n ... @ hotmail.comwrote:
On Mar 4, 10:02 pm, Ian Collins <ian-n...@hotmail.comwrote:

dtscho ... @ gmail.com写道:
dtscho...@gmail.com wrote:


Hi,


我有一个我正在做的家庭作业项目,所以要转发,我是
的新手/>
C编程。但无论如何,在内存中遇到一些问题

分配问题与char *相关,这是一个变量,它是一个变量,它是一个

结构中的变量。我一直在收到分段错误错误,而且我很难理解为什么。这里是有问题的代码的部分...
I have a homework project I am working on, so be forwarned, I''m new to
C programming. But anyway, having some trouble with a memory
allocation issue related to a char * that is a variable inside of a
structure. I keep getting segmentation fault errors and I am having
trouble understanding why. Here''s the parts of the code in question...


现在这里是我遇到麻烦的函数...如果你看看

到找到这一行的部分...
Now here is the function that I am having trouble with... If you look
down to the section where this line is found...


member-> firstName =(char * )calloc(strlen(fname)+ 1,sizeof(char));
member->firstName = (char *)calloc(strlen(fname)+1, sizeof(char));



永远不要转换calloc / malloc / realloc的返回值。 sizeof(char)也是通过

定义1.删除强制转换并确保包含< stdlib.h>


Never cast the return of calloc/malloc/realloc. Also sizeof(char) is by
definition 1. Remove the casts and make sure you have included <stdlib.h>


编译器在那里停止并生成seg错误消息。
The compiler stops there and generates the seg fault message.



我认为你应该说你运行它时程序会崩溃吗?


I assume you should have said the program crashes when you run it?


sscanf(性别, %c,member-> sex;

sscanf(年龄,%d,成员 - >年龄);
sscanf(sex, "%c", member->sex);
sscanf(age, "%d", member->age);



以上应该使用& member-> sex和& ember-> age。


the above should use &member->sex and &ember->age.



好的,我理解我应该更改代码

如下吗?


member-> firstName = calloc(strlen(fname) )+ 1,sizeof(char));


注意我从calloc前面删除了(char *)。实际上,

留下它是如何在课堂上有任何例子。所以我很困惑。


我的意思是程序崩溃,而不是编译器停止。我太累了今晚很想清楚。


顺便说一句,我只是通过删除
$ b $重新编译并运行程序b(char *)强制转换,但仍存在相同的seg错误错误。我将

需要更多地查看我的代码。


此外,我在标题中包含了stdlib.h库。

David

OK, am I correct in understanding that I should be changing the code
as follows?

member->firstName = calloc(strlen(fname)+1, sizeof(char));

Notice I removed the (char *) from in front of calloc. Actually,
leaving it there is how any example in class had it. So I am confused.

I did also mean the program crashes, not the compiler stops. I am too
tired to think clearly tonight.

By the way, I just recompiled and ran the program by removing the
(char *) cast, but the same seg fault error is still there. I will
need to review my code more.

Also, I am including the stdlib.h library in my header.

David


这篇关于将内存分配给结构中的char *变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆