consider the code
#include<stdio.h>
int main(void)
{
char* a;
scanf("%s",a);//&a and &a[0] give same results-crashes
printf("%s",a);
return 0;
}
why does this code results in crashing?whereas this code using character array works fine?
#include<stdio.h>
int main(void)
{
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:)
Because
char* a;
allocates space on the stack for a character pointer whilstchar a[100];
allocates space for 100 characters.In the former case, you need to allocate some actual memory for the pointer to point to. What's happening to cause your crash is that
a
is being set to some arbitrary value (you don't initialise it) and, when youscanf
, that's where the data is being written. That's if you just usea
, let's call that crash type number one.If you use
&a
, it will write your input over the pointer itself which will leave you with a pointer that points who-knows-where, leading to crash type number two. That's assuming you haven't entered more characters than will overflow the pointer - that would corrupt the call stack leading to yet another crash situation (type three).And as a word of advice, please don't use input functions that don't have bounds checking unless you're absolutely in control as to what data is presented to them. Even with a
char[100]
, someone could cause a stack overflow in your code by entering more characters than will fit in your buffer.I find the best thing to do is to use
fgets
(which can limit the characters read) in conjunction withsscanf
(although if all you're getting is a string,sscanf
is not really needed).a
doesn't point to anything. Or actually, it's uninitialized so it points somewhere, just not somewhere valid.You could provide storage on the stack like you've already tried:
Note that
printf("%s",x);
yields exactly the same result,a
is pointing tox
, so that's where your string will 'live'.Or you could have the memory management handle it by using malloc
HTH.
EDIT
Also, before the comments start... This is very unsafe code, but I guess you're just trying to wrap your head around pointers, so I just tried to give you a nudge in the right direction. (If not you need to look into reading limited amount of data, make sure it fits in your buffers, if it's from some other source than a file, you need to make sure you got all of it, and so on, and so on).
In you first code,
a
is an uninitialized pointer to char. You are trying to write to non-allocated memory or reserved memory.You need to allocate memory for the input string with
malloc()
.scanf needs a place where to put the characters it reads. You can pass it a pointer to a character array (what you do in your second example), or a pointer to some memory obtained in another way (say malloc()). In your first example, you pass it an uninitialized pointer, thus the problems you are observing.
In the first example a is a "dangling pointer" - it's a pointer that contains the address of a somewhat random memory location. In most cases accessing this address will result in a crash. Bottom line: you need to allocate memory for a.
You have to allocate memory for
a
withmalloc()
before using it .