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 whilst char 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 you scanf
, that's where the data is being written. That's if you just use a
, 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 with sscanf
(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:
#include <stdio.h>
int main()
{
char x[100];
char *a = x; // a points to the storage provided by x on the stack
scanf("%s",a); // should work fine
printf("%s",a);
return 0;
}
Note that printf("%s",x);
yields exactly the same result, a
is pointing to x
, so that's where your string will 'live'.
Or you could have the memory management handle it by using malloc
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc (100*sizeof(char)) // a points to the storage provided by malloc
if (a != null)
{
perror ("malloc"); // unable to allocate memory.
return 1;
}
scanf("%s",a); // should work fine
printf("%s",a);
free (a); // just remember to free the memory when you're done with it.
return 0;
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With