#include <stdio.h>
#include <string.h>
#define LEN 20
struct names
{
char first[LEN];
char last[LEN];
};
struct pnames
{
char *first;
char *last;
};
int main()
{
struct names veeo = {"Veeo", "<NAME>"};
struct pnames veeo1 = {"GZHU", "GHK"};
scanf("%s", veeo.first); // char first[LEN];无问题
// scanf("%s", veeo1.first); // char *first; 出问题
printf("%s and%s\n", veeo.first, veeo1.first);
printf("%p,%p", &veeo.first, &veeo1.first);
return 0;
}
今天在学习字符数组和字符指针来储存字符串时发现
可以使用指向char的指针来代替字符数组,但是通过字符指针来存储的字符串并没有分配内存空间来存储字符串,使用 scanf
向这些字段中读取字符串时可能会导致内存访问错误或者段错误。
具体来说,struct pnames
中的 first
和 last
字段被定义为指针类型 char *
,而不是数组类型,因此它们并没有被分配内存空间来存储字符串。当你尝试使用 scanf
读取字符串到这些字段时,会涉及到未分配的内存地址,从而导致程序运行时错误。
相反,struct names
中的 first
和 last
字段被定义为数组类型 char [LEN]
,这意味着它们有固定大小的内存空间来存储字符串。因此,使用 scanf
来向这些字段中读取字符串是安全的。
为了修正这个问题,你可以考虑将 struct pnames
中的 first
和 last
字段也改为数组类型 char [LEN]
,以确保有足够的内存空间来存储字符串。另外,当使用 scanf
读取字符串时,最好使用安全的输入函数,如 fgets
,并且要确保输入的字符串不会超出预分配的内存空间。
这里我补充一下,其实使用指针变量也是可以的,也就是使用malloc()这个函数
用malloc(LEN * sizeof(char))
为veeo1.first
和veeo1.last
分配了LEN
个char
类型大小的内存空间。这样就为这两个指针分配了足够的内存来存储字符串。在使用完这些内存后,通过调用free
函数释放分配的内存,以避免内存泄漏问题。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 20
struct names
{
char first[LEN];
char last[LEN];
};
struct pnames
{
char *first;
char *last;
};
int main()
{
struct names veeo = {"Veeo", "<NAME>"};
struct pnames veeo1;
veeo1.first = (char *)malloc(LEN * sizeof(char));
veeo1.last = (char *)malloc(LEN * sizeof(char));
if (veeo1.first == NULL || veeo1.last == NULL)
{
printf("Memory allocation failed\n");
return 1;
}
scanf("%19s", veeo.first);
scanf("%19s", veeo1.first);
printf("%s and %s\n", veeo.first, veeo1.first);
printf("%p, %p", &veeo.first, &veeo1.first);
free(veeo1.first);
free(veeo1.last);
return 0;
}