C Char pointers

后端 未结 4 1882
旧时难觅i
旧时难觅i 2021-01-19 15:49

Let us say we have a array of pointers:

char *ptr[30];

Now this works fine, and doesn\'t seems to do anything unexpected! I can input names easi

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-19 16:36

    When you define a pointer like:

    char *ptr = 0; // NULL pointer: dereferencing it will crash
    puts(ptr);    // crash
    

    You merely create a link to a place in memory:

    ptr = "string"; // dereferencing it will show the string
    puts(ptr);     // displaying "string"
    

    So having an array of pointers merely creates a list of references to other variables.

    To reference a place in memory, you then have to assign variables to your pointers, or allocate memory for each pointer.

提交回复
热议问题