简体   繁体   English

打印字符串数组时出现分段错误和代码 139

[英]Segmentation fault and code 139 while printing a string array

I get an error 139 and a segmentation fault while trying to run, I'm a little baffled since I'm not accessing or creating any files or even getting input.我在尝试运行时收到error 139segmentation fault ,我有点困惑,因为我没有访问或创建任何文件,甚至没有获得输入。 I'm simply printing three arrays in order.我只是按顺序打印三个 arrays 。

#include<stdio.h>
int main(){
int i,x;
//set the values of the names
char nmbr[3][8]={
    "Andrés",
    "Manuel",
    "Peter"};
//print characters of the name 1 by 1
for(i=0;i<3;i++){
    while(nmbr[i][x]!='\0'){
        printf("%c",nmbr[i][x]);
        x++;
    }
    putchar('\n');
}
return 0;
}

x is not initialized which is UB x未初始化,即 UB

for(i=0;i<3;i++){
    x = 0;
    while(nmbr[i][x]!='\0'){
        printf("%c",nmbr[i][x]);
        x++;
    }
    putchar('\n');
}

x is uninitialized. x未初始化。 Your compiler should warn you about this.你的编译器应该警告你这一点。

You probably want x=0 before the while loop.您可能希望x=0while循环之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM