LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
Search this Thread
Old 02-12-2013, 02:16 PM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
This is a c program to collect lines in a buffer ..


I am getting segmentation error whenever i try to print the har array for the program
I am reading a book of c and this was the function in that ,but i am trying to write it with main. ie without using fuction

Code:
#include<stdio.h>
main(){
 char s[lim];
int lim =100;  
 
     int c=0 ,i;
   for (i=0; i < lim-1 && (c=getchar())!=EOF &&c!='\n';++i)           

s[i] = c;    
   if (c == '\n') 
{           s[i] = c; 
          ++i;      
/*because of this code i am getting segmentation fault*/
for(i=0 ;i<lim-1 ;i++)
printf("%s" ,s[i]);

 
}       s[i] = '\0';
       return i;
   }
Please explain me why i am getting htis segmentation fault
 
Old 02-12-2013, 04:16 PM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 375

Rep: Reputation: 150Reputation: 150
First, and foremost! Indent your code properly. Without proper indentation not only you won't see what your program is doing, but also no one here will bother to read the code.

Second of all, when you compile your code, enable all warnings and even turn them into errors, like so:
Code:
gcc -o example -Wall -Werror -Wextra example.c
If you do that for this code, you'll notice three things:
  • “lim” is used before it's initialised,
  • you should specify return type of “main()” to be “int”,
  • and printf's “%s” expects “char *” but it's given and “int”.

I hope those hints will help you solve your problem. Since you are learning it's better if you figure this one out for yourself.

And remember, compiler is your friend – listen to what its saying.
 
Old 02-13-2013, 07:04 AM   #3
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Rep: Reputation: Disabled
Not getting the purpose of your code..
the Error free code is here

Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*  gcc -o <object> -Wall -Werror -Wextra file.c  */
int main()
{
        int lim =100;
        char s[lim];
        int c=0 ,i;
        for (i=0; i < lim-1 && (c=getchar())!=EOF &&c!='\n';++i)
                s[i] = c;
        if (c == '\n')
        {           s[i] = c;
                ++i;
                /*because of this code i am getting segmentation fault*/
                for(i=0 ;i<lim-1 ;i++)
                        printf("%c" ,s[i]);
        }
        s[i] = '\0';
        printf("\n");
        return 0;
}
 
Old 02-14-2013, 07:36 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,764

Rep: Reputation: 494Reputation: 494Reputation: 494Reputation: 494Reputation: 494
try this:

Code:
/* batman4.c */

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

int main (void)
{
        int lim= 100;
        char s[lim];
        int c, i, len;

        for (i=0; i < lim-1 && (c=getchar())!=EOF &&c!='\n';++i) {
            s[i] = c;
        }
        len= i;
        if (len>0 && s[len-1]=='\n') --len;
        s[len] = '\0';

        for (i=0; i<len; ++i) {
            printf("%c %d %x\n", s[i], s[i], s[i]);
        }
        return 0;
}
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] program with frame buffer lwhat Programming 6 03-10-2010 08:18 AM
how to retrieve some specific set of lines from a file and store it in a char buffer. vigneshinbox Programming 3 04-02-2009 01:16 AM
Vim: How do I read lines of text into a buffer JosephS Linux - Software 1 05-26-2008 10:20 PM
Make a program can collect linux command output regularly and send to a tftp server fragon Programming 2 04-07-2008 04:49 PM
What is the difference between the free buffer and buffer in the buffer hash queue? Swagata Linux - Enterprise 0 05-25-2006 11:57 PM


All times are GMT -5. The time now is 02:47 AM.

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