Assigning strings to pointer in C Language

后端 未结 4 1251
遥遥无期
遥遥无期 2020-12-11 09:25

I am a new learner of C language, my question is about pointers. As far I learned and searched pointers can only store addresses of other variables, but cannot store the act

4条回答
  •  渐次进展
    2020-12-11 09:32

    1. char *c; c="name";

      If you observe here, you are not assigning string "name" to variable c but, you are assigning the base address of memory where name is stored into variable c.

    2. The string name is stored in the string table created by the compiler, all the strings of this form are stored in string table, this string table is a const type, meaning you cannot write again to this location. e.g you can try these two lines char *p = "Hello" ; strcpy(p,"Hi");. While compilation you will get error at second line.

    3. int *c; c = 10;

      In the above code you are creating an integer pointer and assigning 10 to it, the compiler here understands that you are assigning 10 as an address. One more thing you need to understand is all the pointer variables store unsigned interger constants only. So even if it is char *c or int *c in both of these cases the variable c stores an unsigned integer only.

提交回复
热议问题