How to take input for a character pointer without using fget?

Posted by ashish yadav on Stack Overflow See other posts from Stack Overflow or by ashish yadav
Published on 2010-03-08T14:19:51Z Indexed on 2010/03/08 14:36 UTC
Read the original article Hit count: 237

Filed under:
|

consider the code

#include<stdio.h>
int main()
{
   char* a;
   scanf("%s",a);//&a and &a[0] give same results-crashes 
   printf("%s",);
   return 0;
}

why does this code results in crashing?whereas this code using character array works fine?

#include<stdio.h>
int main()
{
   char a[100];
   scanf("%s",&a[0]);//works fine
   printf("%s",a);
   return 0;
}

the difference being character array and pointer?but i knew that pointer just points to the first element that is &a[0] should work fine but upper code crashes for all three that is a,&a and &a[0]? the main thing i would to gather is how can i take input of a character pointer if i insist on using scanf only? i apologize if i am not clear. thanks in advance:)

© Stack Overflow or respective owner

Related posts about c

    Related posts about input