数组分割错误 [英] Segmentation fault with array
问题描述
我对数组感到困惑,希望有人可以在我的代码上点亮
。
我写这篇文章试图学习文件和数组正如我想的那样,如果
可以抓住每个元素并将它们放入一个数组中,我可以用数组索引操作来自文件的叮咬。
也许有更好的方法,但我正在学习。
#include< stdio.h>
#include< stdlib.h> ;
int main()
{
char c;
int num = 0;
int count = 0;
int i;
char instring [10];
FILE * fp;
if((fp = fopen(" testdata.txt"," r"))== NULL)
{
printf(打开文件时出错。\ n);
退出(2);
}
while(!feof(fp))
{
num ++;
if((c = getc(fp) ))== EOF)
{
printf(" EOF到达\\ n");
休息;
}
其他
{
if(c!=''\''')
{
instring [count] = c;
printf(&count; count =%d,instring =%c \ n",count,instring [count]);
count ++;
}
} < br $>
}
i = 0;
printf(" char 3 is%c\ n,instring [i + 3] ]);
返回0;
}
testdata.txt包含
ab $
ef
1we
@ \ /
" @''
:] _
..
当我执行程序时,我得到以下结果:
count = 0,instring = a
count = 1,instring = b
count = 2,instring = $
count = 3,instring =
count = 4,instring = e
count = 5,instring =。
count = 6,instring = f
count = 7,instring =
count = 8,instring = 1
count = 9,instring = w
count = 10,instring = e
count = 11,instring =
count = 12,instring = @
count = 13,instring = \
count = 14,instring = /
count = 15,instring =
count = 16,instring ="
count = 17,instring = @
count = 18,instring =''
count = 19,instring =
分段错误(核心转储)
我的问题是,如果instring有10个元素,那么为什么它会打印到元素19的
?
如果我输入一个很大的数字,比如char instring [150],然后它会工作
我是否完全误解了数组?
Pat
ef
1we
@ \ /
" @''
:] _
..
当我执行程序时,我得到以下结果:
count = 0,instring = a
count = 1,instring = b
count = 2,instring =
count = 3,instring =
count = 4,instring = e
count = 5,instring =。
count = 6,instring = f
count = 7,instring =
count = 8,instring = 1
count = 9,instring = w
count = 10,instring = e
count = 11,instring =
count = 12,instring = @
count = 13,instring = \
count = 14,instring = /
count = 15,instring =
count = 16,instring ="
count = 17,instring = @
count = 18,instring = ''
count = 19,instring =
分段错误(核心转储)
我的问题是,如果instring有10个元素,那么为什么要打印元素19的
?
如果我输入一个很大的数字,比如char instring [150],那么它会工作
我是否完全误解了阵列?
Pat
pt*****@gmail.com 写道:
我对数组感到困惑,希望有人可以在我的代码上放弃
。
我写这篇文章试图学习文件和数组,因为我认为如果我可以获取每个元素并将它们放入一个数组中,我可以操作来自带有数组索引的文件中的叮咬。
也许有更好的方法,但我正在学习。
#include< stdio.h>
#include< stdlib。 h>
int main()
{
char c;
int num = 0 ;
int count = 0;
int i;
char instring [10];
FILE * fp;
if((fp = fopen(" testdata.txt"," r"))== NULL)
{
printf(打开文件时出错。\ n);
退出(2);
}
while(!feof(fp))
{
num ++;
if((c = getc(fp))== EOF)
{
printf(EOF到达\ n);
休息;
}
其他
{
if(c!=''\'''' )
{
instring [count] = c;
printf(" count =%d,instring =%c\ n" ,count,instring [count]);
count ++;
}
}
}
i = 0;
printf(char 3 is%c\ n,instring [i + 3]);
返回0;
}
我的问题是如果instring有10个元素那么为什么要打印
out元素19?
如果我把大数字,如cha r instring [150],然后它会工作
我是否完全误解了数组?
你有一个单数组然后你填写的字符,并保持
写入,而不检查你是否已经写完了。它看起来好像你打算读行而不是单个字符。
-
Ian Collins。
Hi, I''m getting confused with arrays and hope someone can shed light
on my code.
I wrote this to try and learn about files and arrays as I thought if I
could grab each element and place them into an array I can manipulate
the stings from the file with array indexes.
Perhaps there''s a better method but I''m learning.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int num=0;
int count=0;
int i;
char instring[10];
FILE * fp;
if((fp=fopen("testdata.txt","r"))==NULL)
{
printf("Error in openning the file.\n");
exit(2);
}
while(!feof(fp))
{
num++;
if ((c = getc(fp))==EOF)
{
printf("EOF reached \n");
break;
}
else
{
if (c != ''\0'')
{
instring[count] =c;
printf("count = %d, instring = %c\n",count,instring[count]);
count++;
}
}
}
i=0;
printf("char 3 is %c\n",instring[i+3]);
return 0;
}
The testdata.txt contains
ab$
e.f
1we
@\/
"@''
:]_
..
When I execute the program I get the following result:
count = 0, instring = a
count = 1, instring = b
count = 2, instring = $
count = 3, instring =
count = 4, instring = e
count = 5, instring = .
count = 6, instring = f
count = 7, instring =
count = 8, instring = 1
count = 9, instring = w
count = 10, instring = e
count = 11, instring =
count = 12, instring = @
count = 13, instring = \
count = 14, instring = /
count = 15, instring =
count = 16, instring = "
count = 17, instring = @
count = 18, instring = ''
count = 19, instring =
Segmentation Fault (core dumped)
My question is if the instring has 10 elements then why is it printing
out to element 19 ?
If I put a large number, such as char instring[150], then it will work
Have I misunderstood arrays completely ?
Pat
e.f
1we
@\/
"@''
:]_
..
When I execute the program I get the following result:
count = 0, instring = a
count = 1, instring = b
count = 2, instring =
count = 3, instring =
count = 4, instring = e
count = 5, instring = .
count = 6, instring = f
count = 7, instring =
count = 8, instring = 1
count = 9, instring = w
count = 10, instring = e
count = 11, instring =
count = 12, instring = @
count = 13, instring = \
count = 14, instring = /
count = 15, instring =
count = 16, instring = "
count = 17, instring = @
count = 18, instring = ''
count = 19, instring =
Segmentation Fault (core dumped)
My question is if the instring has 10 elements then why is it printing
out to element 19 ?
If I put a large number, such as char instring[150], then it will work
Have I misunderstood arrays completely ?
Pat
pt*****@gmail.com wrote:Hi, I''m getting confused with arrays and hope someone can shed light
on my code.
I wrote this to try and learn about files and arrays as I thought if I
could grab each element and place them into an array I can manipulate
the stings from the file with array indexes.
Perhaps there''s a better method but I''m learning.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int num=0;
int count=0;
int i;
char instring[10];
FILE * fp;
if((fp=fopen("testdata.txt","r"))==NULL)
{
printf("Error in openning the file.\n");
exit(2);
}
while(!feof(fp))
{
num++;
if ((c = getc(fp))==EOF)
{
printf("EOF reached \n");
break;
}
else
{
if (c != ''\0'')
{
instring[count] =c;
printf("count = %d, instring = %c\n",count,instring[count]);
count++;
}
}
}
i=0;
printf("char 3 is %c\n",instring[i+3]);
return 0;
}
My question is if the instring has 10 elements then why is it printing
out to element 19 ?
If I put a large number, such as char instring[150], then it will work
Have I misunderstood arrays completely ?
You have an array of single characters, which you then fill, and keep
writing to without checking if you have written past the end. It looks
like you intended to read lines, rather than single characters.
--
Ian Collins.
这篇关于数组分割错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!