Why don't I need to dereference a character pointer in C before printing it?

后端 未结 6 1240
遇见更好的自我
遇见更好的自我 2020-12-13 11:19

Why does this code work? I would expect that I would need to dereference ptr, printf(\"%s\\n\", *ptr); before I could print it out, but I get a

6条回答
  •  半阙折子戏
    2020-12-13 12:18

    When you print string we need starting address of string.

    printf("%s\n", ptr);
                    ^ address with %s   
    

    it prints chars till \0 nul encounter.

    Whereas to print chat int .. we need value variable:

    printf("%c\n", *ptr);
                   ^ * with %c print first char
    

    Where as in scanf() a string you always need to give address:

    scanf("%s", ptr);
                ^ string address
    

    Also for int scanf() a char

    scanf("%c", ptr);
                ^ read at first location char address 
    

    Note: Scanf() need address with %c to store a scanned value in memory.

    Be careful your ptr points to a constant string so you can't use in scanf.

    Why Segmentation fault with following code ?

        printf("%s\n", *ptr);
    

    When you do like this, because of %s printf interprets *ptr as an address, but it's actually not an address and if you treat it as address it points to some location that is read protected for your program(process) So it causes a segmentation fault.

    Your ptr via name points to some constant string in memory ("Jordan") as in below diagram:

    name 2002
    ┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
    │ 'J' │ 'o' │ 'r' │ 'd' │ 'a' │ 'n' │'\0' │ ........
    └─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘
      ^
      |
     ptr = name 
    
     ==> ptr = 2002
         *ptr = 'J'
    

    In printf("%s\n", *ptr); the *ptr = 'J' and ASCII value of char 'J' is 74 but 74 address is not under your process control and you are trying to read from that memory location and its a memory violation and segmentation fault occurs.

    If you compile you code containing printf("%s\n", *ptr); then with proper option say -Wall with GCC you will get a warning like below:

    warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’

    Says %s need (expects ) an address of type char* but you are putting value

    notice:

    printf("%s\n",   *ptr);
            ^          ^ argument-2
            argument-1 
    

提交回复
热议问题