#include <stdio.h>

main()
{
    char *s1, *s2;
    printf("Enter string 1 :");
    scanf("%s", s1);
    printf("Enter String 2 :");
    scanf("%s", s2);
    printf("%s\n%s", s1, s2);
}

The output of 1st string is right and the output of 2nd string is wrong (i.e. (null))
Can any 1 explain what is going behind?

Recommended Answers

All 6 Replies

Because you have to allocate a memory block for each pointer. Pointers as its name explain are variables those point to address of memory blocks, so when you declare a pointer it is not garantee to point to suitable area of system memory so you have to do that by yourself and use malloc or alloc for allocate a memory block. Also you can assign it address of another varibale like a1[100].

Because char *s1 and char *s2 allocates space for a character pointer.

And if you declare char s1[100] it allocates space for 100 characters.

In the previous case, you need to allocate memory using malloc or calloc. Example

char * s1 = malloc(sizeof (char) * (100)); what it does is, it allocates space for 100 characters, sizeof(char) gives us the space equired for single char, then we multiply it with 100 as we need 100 character memory spaces.

What you have done is declared pointers to characters, and without making them point to anything, you have used them. You must allocate first, that is make the pointers point to something and then use them as you wish.

But, since this works :char *p = "this is a string";
I expected that this also should work :char *s; scanf("%s",s);
i.e. reading a string from input and assigning the base address of string to character pointer.
by the way, The code works for Turbo C, it doesn't work for gcc.

I think the answer to the question is that scanf is not able to allocate memory for the input string properly, so it is having problem in assigning base address of the allocated string to the character pointer.

by the way, The code works for Turbo C, it doesn't work for gcc.

Which should be your first hint that the code is wrong. If it were right, all compilers would produce the same result.

I think the answer to the question is that scanf is not able to allocate memory for the input string properly

That's indirectly correct. The actual problem is that you were making assumptions about what scanf() is supposed to do, and failed to read the documentation to verify your assumptions. The fact that scanf() is indeed unable to properly allocate memory for the string is an irrelevant point because it's not intended to do so.

so it is having problem in assigning base address of the allocated string to the character pointer.

No allocation happens. scanf() writes to the address stored in the pointer you pass, and it's your job to make sure that the pointer points to enough memory to hold whatever length string scanf() might write to it. You passed an uninitialized pointer, and scanf() rightfully puked when it turned out that the garbage address stored by that pointer didn't refer to memory that your process properly owned.

Thanks deceptikon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.