-1

Possible Duplicate:
Segmentation Fault when trying to use scanf on a struct

I am new to C programming and have the following code.

char *s;
scanf("%s",s);
printf("This is s %s",s);

The above lines of code are causing a Segementation Fault. Can you please explain why? Is there some article where I can read up on this to understand the concepts more deeply?

Community
  • 1
  • 1
Rahul Dhamecha
  • 23
  • 1
  • 1
  • 7
  • 1
    Haven't done C in ages but could you try maybe replacing `char *s;` with `char* s = (char*)malloc(sizeof(char) *30);`? This should make `s` point to a buffer which allows 30 characters. After your last line then just write `free(s);`, this will release any resources, as pointed out by @chris. – npinti Oct 01 '12 at 06:12
  • 3
    What makes you think you own random uninitialized memory wherever `s` happens to be pointing? – chris Oct 01 '12 at 06:12
  • Check the question I just linked to - you need to allocate space for the string to read in to. – Timothy Jones Oct 01 '12 at 06:12
  • @npinti, `char s[30];` does essentially the same, but a bit nicer as you don't have to clean it up. – chris Oct 01 '12 at 06:13

4 Answers4

2

You can write to memory addresses which are owned by that particular entity.

For eg:

char s[10];

The compiler reserves enough memory required to store 10 characters for s and you can freely write to it.

When you say:

char *s;

The pointer s just points to some random memory address which is not owned or reserved for it. Writing to that memory address results in writing to memory owned by some other entity. Technically, this is Undefined Behavior.
Practically, a segfault might occur or not depending on the memory address being written to is owned by some other entity. So you are lucky to get a crash which draws your attention to it. Anyhow, It is undefined behavior and hence should always be avoided.

You need to allocate memory to the pointer to be able to anything meaningful with it.Be it memory on stack or heap, but it should be owned and hence allwed to write to.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Any technical reasoning for this downvote is very much appreciated Or is it that just your dog was barking too loud? – Alok Save Oct 01 '12 at 11:21
1

Try this with a array and pointer using fgets:

static void get_input()
{
    /* Array of 32 btyes long - no input can be 32 characters in total */
#define CHAR_SIZE 32
    char str_array[CHAR_SIZE];
    char *str_ptr = calloc(sizeof(char), CHAR_SIZE);

    /* Get input from user - limit the input to 32 bytes using fgets which is safer */
    printf("Please enter something: ");
    /* Clear the memory before using it */
    memset(str_array, 0, CHAR_SIZE);
    fgets(str_array, CHAR_SIZE, stdin);
    printf("The input was [ %s ]\n", str_array);

    /* Doing the same thing with a pointer */
    printf("Please enter something again: ");
    fgets(str_ptr, CHAR_SIZE, stdin);
    printf("The input again was [ %s ]\n", str_ptr);

    /* free memory */
    free(str_ptr);
}

Hope it helps,

ant2009
  • 27,094
  • 154
  • 411
  • 609
0

a pointer stores an address. and that address should always be that of some reserved memory.

means after doing char *s. you need to reserve/allocate some memory to it using either malloc or calloc.

char *s=malloc(10*sizeof(char));

this will allocate 10 bytes of memory assuming size of char is 1 byte. remember you need to free the memory that was allocated by you using free function after your purpose is done.

Vijay
  • 65,327
  • 90
  • 227
  • 319
0

You have created a pointer but right now it's uninitialized (may point to somewhere you don't have permission to access).

char *s;

Either declare it as an array

char s[20]; //this number should be big enough to hold the input

Or allocate some memory and then point to it.

char *s = (char *) malloc (20*sizeof(char));

Refer: scanf

prashanth
  • 2,059
  • 12
  • 13