Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C创建字符串结构_C_String_Struct_Segmentation Fault - Fatal编程技术网

C创建字符串结构

C创建字符串结构,c,string,struct,segmentation-fault,C,String,Struct,Segmentation Fault,所以,我一直在为字符串实现这个结构。然而,我在调用*createString()时不断遇到分段错误 这里是.h目录 typedef struct { char *characters; int length; } String; String *createString(); 下面是我的.c文件中的实现 String *createString(){ char *m,b; int n = 0; String *theS = (String *) mal

所以,我一直在为字符串实现这个结构。然而,我在调用*createString()时不断遇到分段错误

这里是.h目录

typedef struct {
    char *characters;
    int length;
} String;

String *createString();
下面是我的.c文件中的实现

String *createString(){
    char *m,b;
    int n = 0;
    String *theS = (String *) malloc (sizeof(String));
    m = theS->characters;
    b = getchar();
    while((b = getchar()) != '\n'){
        *(m+n) = b;
        n++;
        m = realloc(m, n+1);
    }
    *(m+n) = '\0';
    theS->length = strlen(theS->characters);
    return new;
}

正如@hustmr所建议的:当您分配
字符串时,您不会为其
字符
字段分配任何要指向的空间,因此当您尝试访问它所指向的内容时,事情会变得糟糕。

正如@hustmr所建议的:当您分配
字符串
时,您没有为它的
字符
字段分配任何要指向的空间,因此当您尝试访问它所指向的内容时,情况会变得糟糕。

问题1 Q:此行后:

String *theS = (String *) malloc (sizeof(String));
theS->characters
指向什么

A:谁知道呢?但没有什么用处

您需要至少分配一个字符来保存最终插入的
'\0'

String *theS = malloc (sizeof(String));
theS->characters = malloc(1);
问题2 然后你修改了所有的
m
,但从不将该值重新分配给
theS->characters
,所以当你说

theS->length = strlen(theS->characters);
这不是一个很有帮助的答案

在该行前面添加:

theS->characters = m;
问题3 应该是:

return theS;
问题4 你把第一个角色扔掉了。只需删除独立的
b=getchar()

工作示例:

问题1 Q:此行后:

String *theS = (String *) malloc (sizeof(String));
theS->characters
指向什么

A:谁知道呢?但没有什么用处

您需要至少分配一个字符来保存最终插入的
'\0'

String *theS = malloc (sizeof(String));
theS->characters = malloc(1);
问题2 然后你修改了所有的
m
,但从不将该值重新分配给
theS->characters
,所以当你说

theS->length = strlen(theS->characters);
这不是一个很有帮助的答案

在该行前面添加:

theS->characters = m;
问题3 应该是:

return theS;
问题4 你把第一个角色扔掉了。只需删除独立的
b=getchar()

工作示例:

使用您的样式而不检查分配错误:

String *createString(){
    int n = 0;
    char *m = malloc(1);
    int c;
    String *theS = (String*)malloc(sizeof(String));
    while ((c = getchar()) != EOF && c != '\n') {
        m = realloc(m, n + 2);
        m[n] = c;
        n++;
    }
    m[n] = '\0';
    theS->characters = m;
    theS->length = n;
    return theS;
}

使用您的样式且不检查分配错误:

String *createString(){
    int n = 0;
    char *m = malloc(1);
    int c;
    String *theS = (String*)malloc(sizeof(String));
    while ((c = getchar()) != EOF && c != '\n') {
        m = realloc(m, n + 2);
        m[n] = c;
        n++;
    }
    m[n] = '\0';
    theS->characters = m;
    theS->length = n;
    return theS;
}

99%失败是因为
m=theS->characters
@hustmphrr,这行有什么问题吗?您在缓冲区中存储一个尚未分配实际分配的字符一点都不奇怪吗?您的第一次循环迭代取消引用
*(m+0)
,其中
m
是一个不确定的指针。@EugeneSh:显然是个问题,尽管事情在达到这个程度之前已经偏离了方向。@EugeneSh.,我没有键入应该是99%失败,因为
m=theS->字符
@hustmphrr,这行有什么问题?你不觉得有点奇怪吗?你把一个字符存储在一个缓冲区中,而这个缓冲区还没有实际分配给它?您的第一次循环迭代取消引用
*(m+0)
,其中
m
是一个不确定的指针。@EugeneSh:显然是个问题,尽管事情在这么远之前已经偏离了方向。@EugeneSh.,我没有键入它应该是的,我做了所有更改,它正在工作!除了一件事。当我打印出这些->字符时,除了第一个字符外,所有字符都被输出。注意“问题4”,并在链接中仔细检查工作版本。我做了所有更改,它工作了!除了一件事。当我打印出这些->字符时,除第一个字符外,所有字符都被输出。注意“问题4”,并在链接中对照工作版本进行双重检查。注意
getchar
如何返回一个应检查EOF的整数。注意
getchar
如何返回一个应检查EOF的整数。