LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   This is a c program to collect lines in a buffer .. (http://www.linuxquestions.org/questions/programming-9/this-is-a-c-program-to-collect-lines-in-a-buffer-4175449866/)

batman4 02-12-2013 03:16 PM

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

mina86 02-12-2013 05:16 PM

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.

yahoosam 02-13-2013 08:04 AM

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;
}


NevemTeve 02-14-2013 08:36 AM

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;
}



All times are GMT -5. The time now is 08:40 AM.