1
votes

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

4
It is not a good idea to alter the question so as to make the answer already provided invalid. It is OK to leave the original — perhaps as a comment — and show a correction; it is not OK just to correct the code and invalidate the answers you've been given.Jonathan Leffler
Note that if you were using GCC and you compiled with -Wall, you'd have received warnings from the compiler about your mistake. Make sure you do compile with (at least) -Wall if you're using GCC. If you're using some other compiler, then find out how to turn on more warnings.Jonathan Leffler
Sir, Now i am actually having more doubts. Whenever i am declaring a variable, is it not allocating a space?Rajan Chennai
The space for name_1 was allocated; it is a char *, so enough space for storing one char pointer was allocated. One part of the trouble is that you never initialized the pointer to point to something. Another part of the trouble is that scanf() not only assumed that the pointer was initialized, it also assumed it was initialized so that it pointed to an area of memory big enough to store the string to be read by scanf(), including the terminating null. By changing the definition of name_1 to, say, char name_1[20], you ensure that there are twenty bytes of space for the string.Jonathan Leffler
[...continued...] If you change it to: char name_0[20]; char *name_1 = name_0; then you have allocated space for scanf() to store the data in, and initialized name_1 to point to that space. However, you can subsequently change your mind and make name_1 point to a different area (eg name_2) if you want to. Which makes most sense depends on what you're going to do with name_1 after you've read the data.Jonathan Leffler

4 Answers

3
votes

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.

2
votes

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);
0
votes
char *name_1 ;
...
scanf("%s",&name_1) ;

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

0
votes

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..