Input using char pointer versus char array

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 crash? then how does this code using an array of characters work fine?

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

difference in character array and pointer? but I knew that the pointer simply points to the first element, which is & a [0] should work fine, but does the top code fail for all three being a & a and & a [0]? the main thing that I would like to collect, how can I take a pointer to a character if I insist on using scanf only? We apologize if I do not know. thanks in advance:)

+4
source share
6 answers

Since char* a; allocates space on the stack for the character pointer, and char a[100]; Allocates space for 100 characters.

In the first case, you need to allocate some actual memory for the pointer that it points to. What happens for your failure is that a set to some arbitrary value (you do not initialize it), and when you scanf , where the data is written. If you just use a , call this type of failure number one.

If you use &a , it will write your input on the pointer itself, which will leave you with a pointer that indicates who-knows-where, which leads to failure type number two. Suppose you did not specify more characters than overflowing the pointer, which may damage the call stack, which will lead to another emergency situation (type three).

And as a tip, please do not use input functions that do not have border checks, unless you absolutely control what data is presented to them. Even with char[100] , someone can cause a stack overflow in your code by typing more characters than it will in your buffer.

I find it best to use fgets (which can restrict character reading) in combination with sscanf (although if all you get is a string, sscanf is not really needed).

+8
source

a does not indicate anything. Or, in fact, it is not initialized, so it points somewhere, not really somewhere.

You can provide storage on the stack, as you have 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); gives exactly the same result, a points to x , so where your line will live.

Or you could manage memory with 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; } 

NTN.

EDIT
Also, before starting the comments ... This is very dangerous code, but I think you are just trying to wrap your head around pointers, so I just tried to push you in the right direction. (If you do not need to look at reading a limited amount of data, make sure that it fits into your buffers, if it is from some other source than the file, you need to make sure that you all received, etc. Etc .).

+3
source

Before using, you need to allocate memory a with malloc() .

+2
source

scanf needs a place to put the characters it reads. You can either give it a pointer to an array of characters (which you do in your second example) or a pointer to some memory received differently (say, malloc ()). In the first example, you pass it an uninitialized pointer, and therefore the problems you observe.

+2
source

In the first example, a is a โ€œhanging pointerโ€ โ€”this is a pointer that contains the address of a somewhat random memory location. In most cases, access to this address will fail. Bottom line: you need to allocate memory for.

+1
source

In your first code, a is an uninitialized pointer to a char. You are trying to write to unallocated memory or reserved memory.

You need to allocate memory for the input string malloc() .


 int main(void) { char* s; /* ... */ s = malloc(100 * sizeof(*s)); if (s == NULL) { return 1; /* ... */ } 
+1
source

Source: https://habr.com/ru/post/1303456/


All Articles