Rajan Chennai
Rajan Chennai

Reputation: 145

String - Segmentation Fault

I have a problem with this code. I am using a gcc compiler and when i compile and execute this code i am getting a seg fault. I am just assigning two variables, name_1 as pointer and name_2 as string. When i am trying to provide string input for the two values i am getting a seg fault. This seg fault is always associated with the pointer variable that i am using.

Below i have provided the code and the screenshot of the error.

#include <stdio.h>

int main()
{
char *name_1 ;
char name_2[10] ;

/*      Getting 2 strings as an input from the user
        and is stored in the pointer variable name_1 and name_2*/
scanf("%s",name_1) ;
scanf("%s",name_2) ;

/*      Printing the values of the varibales 
        name_1 and name_2 in string format      */
printf("\n%s",name_1) ;
printf("\n%s",name_2) ;

printf("\n\n") ;
return 0 ;
}

Please help me in this code.

Seg fault

Upvotes: 1

Views: 2649

Answers (4)

Shash
Shash

Reputation: 4260

Your pointer char *name_1 should point to something. As a rule follow

Declaring a pointer variable does not create the type of variable, 
it points at. It creates a pointer variable. So in case you are pointing 
to a string buffer you need to specify the character array and a buffer 
pointer and point to the address of the character array.

Recommended change :

  • You can have your char *name_1 to point to another array of characters or

  • you can have it as an array..

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 754910

The original version of the question contained:

char *name_1;
...
scanf("%s", &name_1);

The question has since been revised to contain:

char *name_1;
...
scanf("%s", name_1);

You haven't allocated any space for name_1 to point to. You also passed a char ** (namely &name_1) to scanf() with a %s format which expects to be given a char *.

Possible fix:

int main(void)
{
    char name_1[20];
    char name_2[10];

    scanf("%s", name_1);
    scanf("%s", name_2);

Another possible fix:

int main(void)
{
    char name_0[20];
    char *name_1 = name_0;
    char name_2[20];

    scanf("%s", name_1);
    scanf("%s", name_2);

Upvotes: 2

Billy ONeal
Billy ONeal

Reputation: 106609

char *name_1;, is a pointer. Initially, it points to some random garbage. You then ask scanf to place a string at whatever random garbage address name_1 happens to point to when your program starts; this is undefined behavior. A conformant C implementation could have this program work as expected only on Tuesdays if it wants. :)

If you're going to pass a pointer, you have to make sure it points to a valid buffer first.

Moreover, you have a level of indirection violation in your call to scanf -- name_1 is already a pointer. You don't want to pass a pointer to a pointer to scanf; just a pointer.

Upvotes: 3

codaddict
codaddict

Reputation: 455380

char *name_1 ;
...
scanf("%s",&name_1) ;

name_1 is a dangling pointer and you are trying to use it, which is incorrect.

Upvotes: 0

Related Questions