1

Is there any way I can do this without using dynamic memory allocation?

#include <stdio.h>

int main() {
    char* str = NULL;
    scanf("%s", str);
    printf("%s\n", str);
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Rajdeep Biswas
  • 187
  • 2
  • 11

3 Answers3

6

There is no straightforward way as per the C standard (C11), however, POSIX defines optional assignment-allocation character m as part of the conversion specifier, which relives the programmer from the responsibility of allocating memory.

However, under the hood, this does use dynamic memory allocation, and you need to call free() on the pointer later on.

Quoting the reference:

The %c, %s, and %[ conversion specifiers shall accept an optional assignment-allocation character 'm', which shall cause a memory buffer to be allocated to hold the string converted including a terminating null character. In such a case, the argument corresponding to the conversion specifier should be a reference to a pointer variable that will receive a pointer to the allocated buffer. The system shall allocate a buffer as if malloc() had been called. The application shall be responsible for freeing the memory after usage. If there is insufficient memory to allocate a buffer, the function shall set errno to [ENOMEM] and a conversion error shall result. If the function returns EOF, any memory successfully allocated for parameters using assignment-allocation character 'm' by this call shall be freed before the function returns.

Something like

#include <stdio.h>
#include <stdlib.h>

int main() {
    char* str = NULL;
    scanf("%ms", &str);
    printf("%s\n", str);
    free(str);

    return 0;
}

would do the job.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

This way:

int main()
{
  char str[100];
  scanf("%s", str);
  printf("%s\n", str);
}
Leo
  • 741
  • 6
  • 14
-1

Yes, but you are going to waste memory, or have the risk of segmentanltion violation:

#include <stdio.h> 
int main() { 
     char str[512] = {0} // as suggested by Jabberwocky
     //sprintf(str, "");
     //scanf("%s", str); // protecting the input from more than 511 chars
     scanf("%511s", str);
     printf("%s\n", str); 
}
vfalcao
  • 332
  • 1
  • 3
  • 12
  • Can you explain the risk? Is there any workaround? Perhaps another function that could be called to enter the string? – Tim Randall Feb 07 '19 at 15:56
  • Why `sprintf(str, "");`? Why not `str[0] = 0;` or `char str[512] = {0};`. And what if the user enters a string longer than 511 characters? – Jabberwocky Feb 07 '19 at 16:13
  • So restrict the input from scanf to a fixed number of characters. Or preferably use fgets. Or better yet, forget about using stdio.h. – Lundin Feb 07 '19 at 16:15
  • If the input is greater than the "buffer size" (say, 511 characters + ending \0) you are going to try to access memory that was not allocated to your process, and a SEGV (segmentation violation) will occur. You can limit the input, using `scanf("%511s", str);` – vfalcao Feb 07 '19 at 17:37
  • the idiom `sprintf(str, "");` or the suggested `str[0] = 0` are unneccessary in this particular piece of code; in any case, the `sprintf` version seems easier to understand. Regarding the limiting, I have updated my suggested answer. – vfalcao Feb 07 '19 at 17:40