Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why am I getting segmentation fault?

0 votes
asked Nov 12, 2019 by anonymous
#include <stdio.h>

void main()
{
    char *name[]={"ravi","ravindra","ravindrababu"};
    printf("%s",*name+1);
    printf("%s",*(name+1));
    printf("%s",*(*(name+2)+7));

    
}

3 Answers

0 votes
answered Nov 12, 2019 by Zohar
#include <stdio.h>

void main()
{
    char *name[]={"ravi","ravindra","ravindrababu"};
    printf("%s",*name+1);
    printf("%s",*(name+1));
    printf("%s",(*(name+2)+7));-- you don't need a * here after getting *(name+2) other wise it will raise a segment fault
}
0 votes
answered Nov 12, 2019 by mayur007 (180 points)

printf("%s",*(*(name+2)+7));

What are you trying to do in the above code?

Please explain...

*(name+2) will return "ravindrababu" which is of type char*. We can't add int with a char*

0 votes
answered Nov 13, 2019 by gameforcer (2,990 points)
edited Nov 13, 2019 by gameforcer

Third printf() should be

    printf("%s",*(name+2)+7);

I think that you thought that since it makes a string you need to pass a pointer to it but that's not the case.

This:

*(name+2)+7

is not in fact a string but a pointer to a position at name[2][7]

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...