Arrays 动态阵列中的分段故障(堆芯转储)错误访问元素

Arrays 动态阵列中的分段故障(堆芯转储)错误访问元素,arrays,pointers,char,dynamic-arrays,Arrays,Pointers,Char,Dynamic Arrays,我想从用户那里获取一个字符串,将其打印出来,并访问它的第一个字符,但通过下面的代码,我得到了 分段故障(堆芯转储) 代码 #包括 #包括 #包括 #定义增长10倍 int main(){ 字符*str\u p、*next\u p、*tmp\p; int ch,need,chars_read=0; 如果(增长printf(“%c\n”,stru p[0])错误是我使用了%s而不是%cprintf(“%s\n”,str\p):此外,由于字符串不以NUL结尾,因此访问超出了有效范围。在评论(1)中指出

我想从用户那里获取一个字符串,将其打印出来,并访问它的第一个字符,但通过下面的代码,我得到了

分段故障(堆芯转储)

代码

#包括
#包括
#包括
#定义增长10倍
int main(){
字符*str\u p、*next\u p、*tmp\p;
int ch,need,chars_read=0;
如果(增长<2){
fprintf(stderr,“生长常数太小”\n);
退出(退出失败);
}
str_p=(char*)malloc(GROW_BY);
next_p=str_p;
而((ch=getchar())!=EOF){
如果(ch='\n'){
printf(“%s\n”,stru\p);
//下面是我也尝试过的错误*(str_p+0),(*str_p)[0]
printf(“%s\n”,str_p[0]);
免费(str_p);
str_p=(char*)malloc(GROW_BY);
next_p=str_p;
字符读取=0;
继续;
}
if(chars\u read==按-1增长){
*下一步p=0;
need=next_p-str_p+1;
tmp_p=(char*)malloc(需要+增长);
if(tmp_p==NULL){
fprintf(stderr,“无初始存储\n”);
退出(退出失败);
}
strcpy(tmp\u p,str\u p);
免费(str_p);
str_p=tmp_p;
next_p=str_p+need-1;
字符读取=0;
}
*next_p++=ch;
chars_read++;
}
退出(退出成功);

}
stru p[0]
是字符而不是字符串

因此,您应该使用
%c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

 #define GROW_BY 10

int main(){
   char *str_p, *next_p, *tmp_p;
   int ch, need, chars_read = 0;
   if(GROW_BY < 2){
    fprintf(stderr, "Growth constant is too small\n");
    exit(EXIT_FAILURE);
}
str_p = (char *)malloc(GROW_BY);
next_p = str_p;
while((ch = getchar()) != EOF){
    if(ch == '\n'){
        printf("%s\n", str_p);  
            //Here is the error I also tried *(str_p + 0), (*str_p)[0]
        printf("%s\n", str_p[0]);
        free(str_p);
        str_p = (char *)malloc(GROW_BY);
        next_p = str_p;
        chars_read = 0;
        continue;
    }
    if(chars_read == GROW_BY - 1){
        *next_p = 0;
        need = next_p - str_p + 1;
        tmp_p = (char *)malloc(need + GROW_BY);
        if(tmp_p == NULL){
            fprintf(stderr, "No initial store\n");
            exit(EXIT_FAILURE);
        }
        strcpy(tmp_p, str_p);
        free(str_p);
        str_p = tmp_p;
        next_p = str_p + need - 1;
        chars_read = 0;
    }
    *next_p++ = ch;
    chars_read++;
}
exit(EXIT_SUCCESS);
printf("%c\n", str_p[0]);
这是因为变量参数没有类型安全性,在
printf()
中,如果格式说明符错误,代码将从内存读取无效结果,可能会崩溃

帮助您调试的一个有用提示是启用编译器警告,例如在GCC中:

gcc -Wall main.c -o main
这将为您的程序显示以下警告

warning: format specifies type 'char *' but the argument has type 'char' [-Wformat]
        printf("%s\n", str_p[0]);
                ~~     ^~~~~~~~
                %c
1 warning generated.

强烈建议使用
-Wall
标志来捕获程序中的某些此类问题。

1)CString必须以NUL字符(
'\0'
)结尾。2)
printf(“%s\n”,str_p[0])
:str\u p[0]的类型是
char
,而不是
char*
<代码>printf(“%s\n”,str_p[0])-->
printf(“%c\n”,stru p[0])错误是我使用了%s而不是%c
printf(“%s\n”,str\p):此外,由于字符串不以NUL结尾,因此访问超出了有效范围。在评论(1)中指出。我已经添加了它,效果很好,thx很多人我觉得很傻,不要担心你们可以改进问题或你们的答案!并扩展它的信息顺便说一句,我将删除下一票
warning: format specifies type 'char *' but the argument has type 'char' [-Wformat]
        printf("%s\n", str_p[0]);
                ~~     ^~~~~~~~
                %c
1 warning generated.