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);
}
2
  • 2
    Yes you can use static allocation, e.g. char str[100]; Commented Feb 7, 2019 at 15:49
  • 3
    This sounds like an XY Problem Commented Feb 7, 2019 at 15:51

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

GNU lib C also defines such a feature.
0

This way:

int main()
{
  char str[100];
  scanf("%s", str);
  printf("%s\n", str);
}

3 Comments

What if the user enters a hundred or more characters?
@ryyker Well after the question title, the OP asks for a solution without using dynamic memory allocation? so...
The answer by Sourav is neat, but still uses dynamic memory allocation under the hood.
-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); 
}

5 Comments

Can you explain the risk? Is there any workaround? Perhaps another function that could be called to enter the string?
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?
So restrict the input from scanf to a fixed number of characters. Or preferably use fgets. Or better yet, forget about using stdio.h.
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);
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.