Jump to content

need help urgently; getting segmentation fault

- - - - -

  • Please log in to reply
4 replies to this topic

#1
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
#include<stdio.h>
main(){
int i=0;
char *keyword[]={
                "auto",
                "break",
                "case",
                "char",
                "const",
                "continue",
                "default",
                "do",
                "double",
                "else",
                "enum",
                "extern",
                "float",
                "for",
                "goto",
                "if",
                "int",
                "long",
                "register",
                "return",
                "short",
                "signed",
                "sizeof",
                "static",
                "struct",
                "switch",
                "typedef",
                "union",
                "unsigned",
                "void",
                "volatile",
                "while"};

do{
printf("%s\n",keyword[i]);
++i;
} while((keyword[i])!='\0');

printf("%d",i);
}
while executing this program i get segmentation fault after while is printed. the value of i doesn't get printed.
what should i do to make this code run successfully?
is this code correct?

#2
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
First of all, main should be declared like this:
int main()
and it should return a value at the end, 0 to mean success.
return 0;

The reason you get a segfault is because printf tries to print the value after "while". There is no value after while, so it spits back a segmentation fault.
This line does not work:
while((keyword[i])!='\0');
Arrays of strings do not end with a '\0' char, there is actually nothing there that tells you when to end. A way to fix this is to put a NULL pointer at the end of the array, then tell the while loop to stop whenever it sees the NULL pointer.

I have fixed your code to work properly, and I have even indented it for you:
#include<stdio.h>
int main() {
    int i=0;
    char *keyword[]={
        "auto",
        "break",
        "case",
        "char",
        "const",
        "continue",
        "default",
        "do",
        "double",
        "else",
        "enum",
        "extern",
        "float",
        "for",
        "goto",
        "if",
        "int",
        "long",
        "register",
        "return",
        "short",
        "signed",
        "sizeof",
        "static",
        "struct",
        "switch",
        "typedef",
        "union",
        "unsigned",
        "void",
        "volatile",
        "while",
        NULL /* Notice the NULL pointer here */
    };

    do {
        printf("%s\n",keyword[i]);
        ++i;
    } while(keyword[i]!=NULL); /* Stop at NULL */
    printf("%d\n",i);
    return 0;
}

Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#3
While1

While1

    Newbie

  • Members
  • Pip
  • 3 posts
Isn't the NULL pointer implicit?

#4
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
thanks....... though i would like you to elaborate on "There is no value after while".

#5
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts

While1 said:

Isn't the NULL pointer implicit?
No it is not. Try compiling my code without the NULL pointer, and you will get a segmentation fault.

csepraveenkumar said:

thanks....... though i would like you to elaborate on "There is no value after while".
Your array ends like this:
                "while"};
There is nothing else after the "while" string, so when your loop passes the "while" string, there is nothing to print and you get a segmentation fault. That's why I added NULL at the end, so your loop could know when to stop printing values from the array.
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users