初始化结构时出现分段错误
Segmentation Fault when initializing struct
无法弄清楚为什么我 运行 使用较长的代码段会出现分段错误。
我接受这个 运行 没问题。
struct Earthquake
{
char *prevString;
char *magString;
char *postString;
};
int main(void)
{
struct Earthquake eq;
eq.prevString="PREVIOUS STRING";
eq.magString = "50";
eq.postString = "POST STRING";
printf("\n%s",eq.prevString);
printf("\n%s",eq.magString);
printf("\n%s\n",eq.postString);
}
当我尝试 运行 时出现分段错误,如果我注释掉结构初始化,我将停止出现分段错误。您也许可以用我注释掉的内容来判断,但是一旦我开始工作,我就会尝试制作一个结构数组,此时我知道 eq 需要是地震指针,但我什至无法正常工作.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include <stddef.h>
//#include <paths.h>
struct Earthquake
{
char *prevString;
char *magString;
char *postString;
};
int main()
{
int numLines = 0;
FILE *fileA = fopen("./all_month.csv", "r");
char c[1];
do
{
*c = fgetc(fileA);
//printf("%c", c);
}while(*c != '\n');
do
{
*c = fgetc(fileA);
//printf("%c", c);
if(*c == '\n')
{
numLines++;
}
}while(*c != EOF);
//printf("%d",numLines);
fclose(fileA);
FILE *fileB = fopen("./all_month.csv", "r");
char prevStringTemp[60];
char postStringTemp[150];
char magStringTemp[10];
struct Earthquake eq;
//if(NULL == (entries = (Earthquake *)malloc(sizeof(Earthquake) * numLines)));
//printf("\nmalloc failed\n");
do
{
*c = fgetc(fileB);
//printf("%c", c);
}while(*c != '\n');
char line[200];
int commaCount = 0;
do{
*c = fgetc(fileB);
if(*c==',')
{
commaCount++;
//printf("\n%d", commaCount);
}
strcat(prevStringTemp, c);
}while(commaCount<4);
do
{
*c = fgetc(fileB);
strcat(magStringTemp, c);
}while(*c!=',');
do
{
*c = fgetc(fileB);
strcat(postStringTemp, c);
}while(*c!='\n');
//strcpy(entries[0].prevString, prevString);
//printf(entries[0].prevString);
//fscanf(fileB,"%s",line);
//printf("\n%s\n", line);
fclose(fileB);
//free(entries);
return 0;
}
这就是问题所在
strcat(prevStringTemp, c);
因为 strcat()
期望 nul
终止字符串,而参数不属于您的情况。
你的c
数组应该是
char c[2] = {0};
和这些的第一个元素
char prevStringTemp[60];
char postStringTemp[150];
char magStringTemp[10];
应该初始化为'[=17=]'
,像这样
prevStringTemp[0] = '[=13=]';
postStringTemp[0] = '[=13=]';
magStringTemp[0] = '[=13=]';
修复此问题后,strcat()
将按您预期的方式运行。
无法弄清楚为什么我 运行 使用较长的代码段会出现分段错误。
我接受这个 运行 没问题。
struct Earthquake
{
char *prevString;
char *magString;
char *postString;
};
int main(void)
{
struct Earthquake eq;
eq.prevString="PREVIOUS STRING";
eq.magString = "50";
eq.postString = "POST STRING";
printf("\n%s",eq.prevString);
printf("\n%s",eq.magString);
printf("\n%s\n",eq.postString);
}
当我尝试 运行 时出现分段错误,如果我注释掉结构初始化,我将停止出现分段错误。您也许可以用我注释掉的内容来判断,但是一旦我开始工作,我就会尝试制作一个结构数组,此时我知道 eq 需要是地震指针,但我什至无法正常工作.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include <stddef.h>
//#include <paths.h>
struct Earthquake
{
char *prevString;
char *magString;
char *postString;
};
int main()
{
int numLines = 0;
FILE *fileA = fopen("./all_month.csv", "r");
char c[1];
do
{
*c = fgetc(fileA);
//printf("%c", c);
}while(*c != '\n');
do
{
*c = fgetc(fileA);
//printf("%c", c);
if(*c == '\n')
{
numLines++;
}
}while(*c != EOF);
//printf("%d",numLines);
fclose(fileA);
FILE *fileB = fopen("./all_month.csv", "r");
char prevStringTemp[60];
char postStringTemp[150];
char magStringTemp[10];
struct Earthquake eq;
//if(NULL == (entries = (Earthquake *)malloc(sizeof(Earthquake) * numLines)));
//printf("\nmalloc failed\n");
do
{
*c = fgetc(fileB);
//printf("%c", c);
}while(*c != '\n');
char line[200];
int commaCount = 0;
do{
*c = fgetc(fileB);
if(*c==',')
{
commaCount++;
//printf("\n%d", commaCount);
}
strcat(prevStringTemp, c);
}while(commaCount<4);
do
{
*c = fgetc(fileB);
strcat(magStringTemp, c);
}while(*c!=',');
do
{
*c = fgetc(fileB);
strcat(postStringTemp, c);
}while(*c!='\n');
//strcpy(entries[0].prevString, prevString);
//printf(entries[0].prevString);
//fscanf(fileB,"%s",line);
//printf("\n%s\n", line);
fclose(fileB);
//free(entries);
return 0;
}
这就是问题所在
strcat(prevStringTemp, c);
因为 strcat()
期望 nul
终止字符串,而参数不属于您的情况。
你的c
数组应该是
char c[2] = {0};
和这些的第一个元素
char prevStringTemp[60];
char postStringTemp[150];
char magStringTemp[10];
应该初始化为'[=17=]'
,像这样
prevStringTemp[0] = '[=13=]';
postStringTemp[0] = '[=13=]';
magStringTemp[0] = '[=13=]';
修复此问题后,strcat()
将按您预期的方式运行。