Is it possible to have dynamic memory allocation in a string that is read with scanf without first declaring it as an array?
-
1Yes. Allocate it and pass the pointer to `fscanf`. And yet `fscanf` still isn't safe. – StoryTeller - Unslander Monica May 09 '16 at 09:24
-
exactly, i mean scanf! – Stamatis Papadopoulos May 09 '16 at 09:29
-
`scanf` on most implementations is just a call to `vfscanf(stdin, ...)` So what applies to one, applies to another. – StoryTeller - Unslander Monica May 09 '16 at 09:32
2 Answers
It is not possible to dynamically allocate memory for a string
after having read it with scanf
, as scanf
needs a pointer. This means that you must allocate memory first, in order to give it as an argument to scanf
.
You can do this like the following example :
char *str;
str = malloc(X*sizeof(char)); //where x is the number of characters you want to allocate
if (str != NULL)
int result = scanf("%s", str);
In other words, pass the allocated pointer to scanf
.
Note : You should be careful about the input you give, as you might cause a buffer-overflow if you give a string which is larger than the allocated space.

- 4,951
- 1
- 16
- 33
-
-
@AlterMann: `malloc` may return null on embedded systems, but on a PC, it will never do that, unless you exhaust the full address space. – Karsten Koop May 09 '16 at 09:32
-
1@StamatisPapadopoulos could you please clarify your comment? I did not understand if I covered you or not. – Marievi May 09 '16 at 09:33
-
1@KarstenKoop, and what about limiting the amount of available memory per user with [ulimit](http://tldp.org/LDP/solrhe/Securing-Optimizing-Linux-RH-Edition-v1.3/x4733.html)? to be portable always check the result of `malloc` – David Ranieri May 09 '16 at 09:39
-
@StamatisPapadopoulos no, it is not possible to dynamically allocate memory for a string *after* having read it. I will update my post for you :) – Marievi May 09 '16 at 09:41
-
@AlterMann it only makes sense to check if you have a strategy to handle the out-of-memory condition. Otherwise it's better and simpler to just let the program crash. – Karsten Koop May 09 '16 at 09:45
-
1Let the program crash? Without a diagnosis? Without a log? Nothing? No, thank you. – David Ranieri May 09 '16 at 09:49
-
@Marievi, έχω και κάποιες άλλες απορίες, αν μπορούσες να με βοηθήσεις. -Ευχαριστώ- :) – Stamatis Papadopoulos May 09 '16 at 09:55
-
@Marievi η malloc aρχικοποιεί τις τιμές 0, και πότε πρέπει να την αρχικοποιώ; – Stamatis Papadopoulos May 09 '16 at 10:24
-
1@StamatisPapadopoulos, no malloc does *not* initialize to zero, please see [here](http://stackoverflow.com/questions/8029584/why-does-malloc-initialize-the-values-to-0-in-gcc). Did you confuse it with [realloc](http://www.cplusplus.com/reference/cstdlib/realloc/)? Moreover, please notice that you are in an International site, the top for programming, thus comments fully written in Greek, is not a good approach. Ευχαριστώ! :) – gsamaras May 09 '16 at 20:54
From your question is seems you want to allocate the memory for the string after it has been scanned in. Unfortunately you are not able to do this as the scanf
functions needs memory addresses to put the data that it scans in. The best you will be able to do is this:
char *someInput = malloc(sizeof(char) * 80);
scanf("%s", someInput);
Edit: note the 80 is an arbitrary amount, this represents how many characters we are allocating for our string to hold - 80 was chosen as it the usual length of a line in a command prompt.

- 2,931
- 1
- 17
- 30
-
you mean that if i don't use array then the scanf doesn't know where to put the data? – Stamatis Papadopoulos May 09 '16 at 09:41
-
@StamatisPapadopoulos precisely, you need to scan the data into some place in memory right? declaring just means "i have this variable of this type" until you allocate memory for it it's simply just a name. Its important for the answers here that you also understand the relationship between arrays and pointers in c. – Blake Lockley May 09 '16 at 09:44
-
if a have a pointer declared then i don't have the right amount of memory, right? – Stamatis Papadopoulos May 09 '16 at 09:52
-
@StamatisPapadopoulos Thats is not the case. A pointer can point to as much memory you allocate for it for example in my answer the point is pointing to 80 char's worth of memory. A pointer and an array are extremely similar in c, in a lot of cases they are used interchangeably. An array can be seen (and used) as a pointer with each consecutive element following the first. This is a crucial topic in c and its important you go off and do some reading on it - I wouldn't do it justice explaining it in the comments. Also don't forget the accept the answer you found most helpful. – Blake Lockley May 09 '16 at 09:58
-
@StamatisPapadopoulos glad i could help, on stack overflow you can only accept one answer, also you should upvote answers you found helpful! – Blake Lockley May 09 '16 at 10:03