3

why is it not possible for sscanf() to write the string into char* s? I initialised it to NULL as I do not want uninitialised variables.

#include <stdio.h>
#include <string.h>

int main()
{

    char* t = "I am a monkey";
    char *s = NULL;
    sscanf(t, "%s",s);
    printf("%s\n",s);


}
6
  • 1
    Think of what it means to initialize a pointer to NULL. Commented Dec 20, 2015 at 5:20
  • 1
    There is a need to secure an area to be written. char *s = malloc(16); instead of char *s = NULL;. (malloc #include <stdlib.h> then printf("%s\n",s);free(s); Commented Dec 20, 2015 at 5:25
  • 2
    @kuan It can't because it is specified to do something else. Read the docs. Commented Dec 20, 2015 at 5:26
  • 1
    sscanf(t, "%ms", &s); when use glibc. (free(s)); Commented Dec 20, 2015 at 5:29
  • 1
    @kuan re: your comment. C functions are pass by value, so sscanf is passed a copy of s (which contains NULL). It cannot change the value of s. Look up pass by value if you are still confused. Commented Dec 20, 2015 at 5:33

3 Answers 3

8

The line char *s = NULL creates a variable that holds the memory address of a character. Then it sets that memory address to zero (NULL is address zero).

Then the line sscanf(t, "%s",s); tries to write the contents of t to the string at the location s. This will segfault because your process cannot access address zero.

Your instincts were good to avoid uninitialized variables, but you traded this for unallocated pointers!

Fix this by allocating some space on the stack (or heap) for s by declaring:

char s[STRING_LENGTH];

Where STRING_LENGTH is #defined to be however many characters you want to allocate. This allocates a chunk of memory to hold the null-terminated character array and sets s to the address of the first character

Sign up to request clarification or add additional context in comments.

Comments

1

Here sscanf defination says that,,,

int sscanf ( const char * s, const char * format, ...);

sscanf(t, "%s",s);

So here it will take text from source as t but on destination side s is just pointer to Null there is no memory.

So allocate some memory to s using malloc or assign s to some valid memory location.

Comments

0

why is it not possible for sscanf() to write the string into char* s? I initialised it to NULL

The string functions in the C standard library do not allocate storage. All they do is copy things around. You have to manage every byte of string storage yourself. This will seem strange to someone coming from pretty much every other language.

In other words, you have to point *s to some memory for sscanf to write into. If you use NULL then sscanf writes to memory that starts at address NULL (usually 0).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.