-1

i have character pointer input and have set 50 bytes of memory for it. After i take input from keyboard using scanf , for example "apple" then i assume that remaining spaces are filled with null character. if it is so , how can i get all the characters or memory location including location of null characters. Is there any way to do it?

int main() {
    char *input;
    int size;

    input = (char *)malloc(50);
    printf("%s\n", "enter something:");
    scanf("%s", input);

    size = strlen(input) + 1;
Anonymous
  • 318
  • 4
  • 14
  • 1
    You wouldn't be able to use `scanf`, at least not usefully. You have no way to know how much it read in, which is needed. `fread` and `read` could be used. – ikegami Dec 03 '21 at 15:33
  • 3
    What is "get"? Get them to where? Also *"i assume that remaining spaces are filled with null character"* - is an incorrect assumption. You can only assume *one* null character in the end of the string. – Eugene Sh. Dec 03 '21 at 15:35
  • 3
    Beware that `scanf("%s", input);` is vulnerable to memory overruns. If the user enters 50 characters or more, you're into undefined behavior territory. This can be, and has been, exploited for nefarious purposes. It is exactly [as dangerous as `gets()`](https://stackoverflow.com/q/1694036/10077). – Fred Larson Dec 03 '21 at 15:35
  • 3
    `then i assume that remaining spaces are filled with null character.`. That assumption is invalid. – William Pursell Dec 03 '21 at 16:20
  • so if only one character in the end of string is filled with null character , then are other spaces filled with garbage values? – Anonymous Dec 04 '21 at 04:43

2 Answers2

2

The string manipulation functions work on strings. If you want to treat your character array as a character array instead of a string, just avoid the string functions (generally, those that are named str*). eg:

#include <stdio.h>

int
main(int argc, char **argv)
{
    char input[50] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvw";
    printf("%s\n", "enter something:");
    if( 1 == scanf("%49s", input)) {
        fwrite(input, sizeof(char), sizeof input, stdout);
        putchar('\n');
    }
    return 0;
}

Be aware that if you are expecting to view this data, the non-printable characters will probably not be rendered well. In particular, if you view the data in a terminal, the null character written by scanf may cause some confusion. YMMV.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

or example "apple" then i assume that remaining spaces are filled with null character

Your assumption is wrong. All other elements in the array have undermined values.

how can i get all the characters or memory location including location of null characters. Is there any way to do it?

You cant access the characters beyond the null character using any string functions. But you can access them as "normal" elements of the array

int main() {
    char *input;
    int size;

    input = (char *)malloc(50);
    printf("%s\n", "enter something:");
    scanf("%s", input);

    for(size_t i = 0; i < 50; i++)
    {
        printf("Char no [%zu] is 0x%02x = %s\n", i, input[i], (input[i] >= ' ' && input[i] < 127) ? (char[]){'\'', input[i], '\'', 0} : "not printable");
    }
}
0___________
  • 60,014
  • 4
  • 34
  • 74