Enter C language string

Asked 2 years ago, Updated 2 years ago, 27 views

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

void SetInteger();

int main(void)
{
    SetInteger();
}

void SetInteger()
{
    unsigned char *ptr;
    scanf("%s", ptr);
    printf("%s", ptr);
}
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    unsigned char *ptr;
    scanf("%s", ptr);
    printf("%s", ptr);
}

In the same case above, the function receives input but does not output the input value, but in the case below, if it is done directly from the main, it outputs the input value. I want to know what the difference is.

c

2022-09-22 19:41

1 Answers

char * cannot accept a string as a scanf() function. To receive a string, you can use the malloc() function to allocate memory. Alternatively, you can declare it as an array of char[] to allocate memory in advance and receive a string.

https://dojang.io/mod/page/view.php?id=337, please refer to it.


2022-09-22 19:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.