带%s的scanf只返回字符串的第一个字符
这是我的主要职能带%s的scanf只返回字符串的第一个字符,c,C,这是我的主要职能 int get_name(void){ char name; printf("Please enter the student name: "); scanf("%s", &name); return name; } 我初始化我的结构,它是 typedef struct{ char name[MAXSTRING]; int id; }student; 通过做 name = get_name(); number= get_i
int get_name(void){
char name;
printf("Please enter the student name: ");
scanf("%s", &name);
return name;
}
我初始化我的结构,它是
typedef struct{
char name[MAXSTRING];
int id;
}student;
通过做
name = get_name();
number= get_id();
student s1 = {.name=name , .id=number};
printf("id is %d\n",s1.id);
printf("name is %s\n", s1.name);
但是我得到的返回值是我输入的字符串的第一个字符。
MAXSTRING在我的student.h中定义为
#define MAXSTRING 20
我很确定我必须以某种方式修改我的name变量,但我尝试了很多方法,但都没有成功。
在这种情况下使用fgets是否更好?在get_name()
函数中name
声明为ofchar
类型而不是char数组
而是将name
声明为某种大小的字符数组。例如
char name[20];
scanf("%s", name); /* & is not needed as name itself address */
然后返回本地数组,如返回名称代码>将导致未定义的行为。最好创建动态数组并返回它
或者将name
声明为指针变量并为其分配内存,然后扫描来自用户的数据
char *name = malloc(SIZE); /* define the size */
scanf("%s",name);
return name; /*returning dynamic array, catch with another pointer in calling function & free it once done */
使用完动态内存后,不要忘记通过调用get_name()中的free(name)
来释放它。
函数name
声明为ofchar
类型而不是char数组
而是将name
声明为某种大小的字符数组。例如
char name[20];
scanf("%s", name); /* & is not needed as name itself address */
然后返回本地数组,如返回名称代码>将导致未定义的行为。最好创建动态数组并返回它
或者将name
声明为指针变量并为其分配内存,然后扫描来自用户的数据
char *name = malloc(SIZE); /* define the size */
scanf("%s",name);
return name; /*returning dynamic array, catch with another pointer in calling function & free it once done */
使用完动态内存后,不要忘记调用free(name)
name
是一个字符,而不是字符串来释放它
您需要将声明更改为char name[MAXSTRING]
或char*name=malloc(MAXSTRING)
。但是,由于返回的是name
,因此应该返回在堆上而不是堆栈上分配的内容的地址。因此,您可能应该使用malloc
然后,如果您想保护代码不受缓冲区溢出的影响,就不必提供name
:scanf(“%s”,name)
,甚至scanf(“%20s”,name)
最后,您还可以将名称作为参数,而不是返回字符串:
void get_name(char *name) {
printf("Please enter the student name: ");
scanf("%d", name);
}
这样说吧:
student s1;
get_name(s1.name);
这样,您就不需要分配和释放一些内存。name
是一个字符,而不是字符串
您需要将声明更改为char name[MAXSTRING]
或char*name=malloc(MAXSTRING)
。但是,由于返回的是name
,因此应该返回在堆上而不是堆栈上分配的内容的地址。因此,您可能应该使用malloc
然后,如果您想保护代码不受缓冲区溢出的影响,就不必提供name
:scanf(“%s”,name)
,甚至scanf(“%20s”,name)
最后,您还可以将名称作为参数,而不是返回字符串:
void get_name(char *name) {
printf("Please enter the student name: ");
scanf("%d", name);
}
这样说吧:
student s1;
get_name(s1.name);
这样,您就不需要分配和释放一些内存。您必须提供字符数组,而不是字符上的指针。那么您就不能将本地数组返回到外部作用域,因此我建议您分配缓冲区:
char *get_name(void){
char *name = malloc(100);
printf("Please enter the student name: ");
scanf("%99s", name); // 99+1 limit avoids buffer overflow
return name;
}
或者使用几乎标准的strdup
string函数,精确分配正确字节数的变体:
char *get_name(void){
char name[100];
printf("Please enter the student name: ");
scanf("%99s", name);
return strdup(name);
}
然后,结构应该保存指针而不是缓冲区:
typedef struct{
char *name;
int id;
}student;
当不再使用时,您需要释放它
您必须提供字符数组,而不是字符上的指针
。那么您就不能将本地数组返回到外部作用域,因此我建议您分配缓冲区:
char *get_name(void){
char *name = malloc(100);
printf("Please enter the student name: ");
scanf("%99s", name); // 99+1 limit avoids buffer overflow
return name;
}
或者使用几乎标准的strdup
string函数,精确分配正确字节数的变体:
char *get_name(void){
char name[100];
printf("Please enter the student name: ");
scanf("%99s", name);
return strdup(name);
}
然后,结构应该保存指针而不是缓冲区:
typedef struct{
char *name;
int id;
}student;
当不再使用时,你需要免费它让·弗朗索瓦·法布的答案非常充分。或者,也可以使用关键字。但是,不建议使用scanf()
的用法。您应该限制输入大小,scanf(“%99s”,name)代码>,以防止
让·弗朗索瓦·法布的回答非常充分。或者,也可以使用关键字。但是,不建议使用scanf()
的用法。您应该限制输入大小,scanf(“%99s”,name)代码>,以防止
name
是一个单独的char
…此外,如果需要字符串,则返回int
?使用char name[20]代码>然后scanf(“%19s”,名称)代码>但是您不能返回本地缓冲区。。。祝你好运:)name
是一个单独的char
…另外,当你想要一个字符串时,你会返回一个int
?使用charname[20]代码>然后scanf(“%19s”,名称)代码>但是您不能返回本地缓冲区。。。祝你好运:)我这样叫它name=get_name()代码>当我运行它并输入一个像chris这样的名称时,我得到一个α的结果:我的指针是否不合适?printf(“%s\n”,name)代码>紧接着get\u name()
调用应该可以工作。如果您同时释放缓冲区,它可能会中断…printf
给出所需的结果,但当我执行student s1={stname}代码>我仍然得到α。可能是因为我的结构是charname[MAXSTRING]代码>是,我的答案建议您将结构成员更改为char*name
(否则指针数据可能被复制而不是指针本身…奇怪:))是的,获取名称,使用strcpy
复制字符串,释放名称。或者将struct字段作为函数的参数作为指针传递,而我这样调用它name=get_name()代码>当我运行它并输入一个像chris这样的名称时,我得到一个α的结果:我的指针是否不合适?printf(“%s\n”,name)代码>紧接着get\u name()
调用应该可以工作。如果你释放buff