LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-05-2013, 04:04 PM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
c program to read a line and store it in a array


please explain me why i am getting segmentation fault in my program.

Code:
#include<stdio.h>
#define max 10000
main(){
int i ,c ;
char s[max];

for(i=0 ;i<max-1 && (c=getchar())!= EOF && c!= '\n'; i++){
s[i] =c;

printf("%s" , s[i]);
}

}
 
Old 02-05-2013, 04:20 PM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 314

Rep: Reputation: 134Reputation: 134
Code:
$ gcc test.c
test.c: In function ‘main’:
test.c:10:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
Do you now know why? Enable all warnings in your compiler.

By the way, you are not terminating the string with a NUL byte.

Also, use correct indentation, proper prototype of main is int main(void), and it should return 0; at the end.
 
1 members found this post helpful.
Old 02-05-2013, 09:20 PM   #3
kedarp
Member
 
Registered: Jul 2012
Location: Pune, India
Distribution: Mageia 4
Posts: 176

Rep: Reputation: 22
You declared int c and using it to get a char value.

Code:
char c=getchar()
Second thing,
get printf() out of the for loop and pass it the base address.

Code:
printf("%s",s);

Last edited by kedarp; 02-05-2013 at 09:30 PM.
 
Old 02-05-2013, 09:53 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,308

Rep: Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768Reputation: 768
Quote:
Originally Posted by kedarp View Post
You declared int c and using it to get a char value.
getchar() returns an int value, EOF doesn't fit in a char.
 
1 members found this post helpful.
Old 02-06-2013, 02:49 AM   #5
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266
A C string is an array, so why not read a string using fscanf or fgets ?
 
  


Reply

Tags
programing


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
read a line from file , parse it and store in variables,print them using shell script swapnika.a60 Linux - Newbie 2 12-20-2012 07:20 AM
[SOLVED] read pdb file and store x,y,z data into array vjramana Programming 2 11-28-2012 09:03 AM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 5 12-05-2010 02:08 PM
Store multi-line output into an array in a Linux bash script steven.c.banks Linux - General 2 12-17-2009 09:10 AM
How to read in line-by-line from a file and store them in several variables ncjlee Programming 1 09-28-2008 06:20 PM


All times are GMT -5. The time now is 01:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration