如何在struct中为char*字段分配内存
如何为struct中的如何在struct中为char*字段分配内存,c,pointers,struct,malloc,C,Pointers,Struct,Malloc,如何为struct中的char*字段分配内存 我的结构: struct student{ int score; char* name; char* surname; }; int main(){ struct student st[]; int i; int n = 5; for(i = 0; i < n; i++){ printf("Score: \n"); scanf("%d", &s
char*
字段分配内存
我的结构:
struct student{
int score;
char* name;
char* surname;
};
int main(){
struct student st[];
int i;
int n = 5;
for(i = 0; i < n; i++){
printf("Score: \n");
scanf("%d", &st[i].score);
printf("Name \n");
scanf("%s", &st[i].name);
printf("Surname \n");
scanf("%s",&st[i].surname)
}
}
如何匹配它?您需要编写一个
初始化\u student(struct student*s)
函数,该函数调用char*
成员上的malloc
。也许这个函数也会使用名称
和姓氏
字符*指针
,您会对它们进行深度复制吗
然后对st
数组的每个成员调用此函数
不要忘记构建相应的
free_student(struct student*s)
函数。您需要编写一个初始化_student(struct student*s)
函数,该函数调用char*
成员上的malloc
。也许这个函数也会使用名称
和姓氏
字符*指针
,您会对它们进行深度复制吗
然后对st
数组的每个成员调用此函数
不要忘记构建相应的free\u student(struct student*s)
函数。例如
#include <stdlib.h>
#include <string.h>
//...
struct student st[1];
char *name = "Marek";
char *surname = "Piszczaniuk";
st[0].name = malloc( strlen( name ) + 1 );
strcpy( st[0].name, name );
st[0].surname = malloc( strlen( surname ) + 1 );
strcpy( st[0].surname, surname );
st[0].score = 100;
_Bool set_name( struct student *st, const char *name )
{
st->name = malloc( strlen( name ) + 1 );
_Bool success = st->name != NULL;
if ( success )
{
strcpy( st->name, name );
}
return success;
}
比如说
#include <stdlib.h>
#include <string.h>
//...
struct student st[1];
char *name = "Marek";
char *surname = "Piszczaniuk";
st[0].name = malloc( strlen( name ) + 1 );
strcpy( st[0].name, name );
st[0].surname = malloc( strlen( surname ) + 1 );
strcpy( st[0].surname, surname );
st[0].score = 100;
_Bool set_name( struct student *st, const char *name )
{
st->name = malloc( strlen( name ) + 1 );
_Bool success = st->name != NULL;
if ( success )
{
strcpy( st->name, name );
}
return success;
}
如果您已经在使用
scanf
,您可以通过使用%ms
而不是%s
告诉函数为您分配sting,如下所示:
char *name;
if (scanf("%ms", &name) != 1) {
fprintf(stderr, "error: could not read name\n");
exit(1);
}
EDIT这可能不是一个有效的解决方案-我忘记了
%ms
仅在POSIX中,而不是在ISO C中。如果您已经在使用scanf
,您可以通过使用%ms
而不是%s
来告诉函数为您分配sting,如下所示:
char *name;
if (scanf("%ms", &name) != 1) {
fprintf(stderr, "error: could not read name\n");
exit(1);
}
编辑这可能不是一个有效的解决方案-我忘记了
%ms
仅在POSIX中,而不是在ISO C中。字符串从何而来?字符串从键盘扫描而来?如何为未知长度的字符串分配空间?struct student st[]代码>:首先,数组的大小是必需的。@MarekPiszczaniuk听起来像一个。请告诉我们更多您实际想要实现的目标。字符串从何而来?字符串从键盘扫描而来。您希望如何为未知长度的字符串分配空间?struct student st[]代码>:首先,数组的大小是必需的。@MarekPiszczaniuk听起来像一个。多告诉我们一点你真正想要达到的目标。这不是答案。你所说的就是:“在函数中使用malloc”@TonyTannous:我们同意不同意(尊重地,自然地)。这个答案与早期C++编译器的工作方式差不多。@ Bathsheba代码> FrutySub(StultSubsS)必须在显示完整结果的循环之后启动,或者可能在显示循环中。这不是答案。你所说的就是:“在函数中使用malloc”@TonyTannous:我们同意不同意(尊重地,自然地)。这个答案与早期的C++编译器的工作方式差不多。@ Bathsheba代码> FrySub(StultSuxS)必须在显示完整结果的循环之后启动,或者可能在显示循环中。我可以尝试这个例子来用MALOC初始化结构吗?我知道这个快速的例子,但我逐渐理解了它。@MarekPiszczaniuk是的,你可以。但是可以正确地说,用字符串初始化结构的数据成员。我可以用malloc初始化这个例子吗?我知道这个快速的例子,但我逐渐理解了它。@MarekPiszczaniuk是的,你可以。但是可以正确地说,用字符串初始化结构的数据成员。这不是标准C的一部分,而是POSIX扩展。这不是标准C的一部分,而是POSIX扩展。