Why must int pointer be tied to variable but not char pointer?

前端 未结 8 685

I am not sure if I worded the question correctly, but here it is spelled out:

char * cp = \"this is a char pointer\";

The code above, based

相关标签:
8条回答
  • 2020-12-10 10:17

    the compiler does not allocate memory for a string neither for an integer if you declare char* or int*.

    the memory is allocated only if you specify it : char* p1 = "string"; for a const string or char* p2 = (char*)malloc(5*sizeof(char)); for a normal dynamically allocated array of char which can be modified or char p3[10]; for array of char which can be use as a string.

    then you can fill p2 and p3 with the string you want : strcpy (p2 , "Hi");

    you cannot not do the same with p1 : strcpy(p1 , "Hi"); will certainly crash the program.

    then when you write int* ip;,int x;, p=&x; : you allocate the memory for the int with the line int x;.

    yo can allocate the memory also with int *ip2 = (int*)malloc(sizeof(int)) then you have not to associate the pointer with a variable. the int can be changed with *ip2 = 3;.

    0 讨论(0)
  • 2020-12-10 10:20

    In C, strings are pointers to null-terminated sequences of characters. That's why:

    char * cp = "this is a char pointer";
    

    is accepted. On the other hand, if:

    int * ip = 5;
    

    were accepted (you can force it if you insist), it would declare "ip is a pointer to an int, whose address is 5").

    0 讨论(0)
  • 2020-12-10 10:28

    Any valid non-null pointer must point to some object. That object needn't be a declared, named variable.

    A string literal specifies an anonymous static array object that exists somewhere in your program's memory. In most contexts, the result of evaluating a string literal is the address of the first element of that array. So given

    char *cp = "this is a char pointer";
    

    the object that cp points to is the initial (0th) element of that anonymous array.

    C99 introduces compound literals, so you can actually write something like:

    int *ip = (int[]){ 5 };
    

    though the behavior is a bit different than that of string literals. In particular, if a compound literal appears inside a function, the anonymous object's lifetime is restricted to the enclosing block. And your compiler may or may not (yet) support compound literals.

    0 讨论(0)
  • 2020-12-10 10:31

    The char[] has been explained, but I think the most important thing to remember in the int example is that:

    int x;  // memory space created for an int
    int *ip;  // pointer
    ip = &x;  // assigning a pointer
    x = 5; // assigning a value to the memory space referenced by x
    

    Whereas

    int *ip; // this is a pointer - no space in memory has been allocated for an int
    *ip = 5;  // hence why you can't assign a value here - there's nowhere to put it!
    
    0 讨论(0)
  • 2020-12-10 10:33

    The simplified reason is that a string is a value that does "float in memory". The compiler actually takes the string and puts it in your executable (or library), and when you load the program into memory, the string has an address, and when you use the string you're actually getting a pointer to that address.

    But for integers, the compiler doesn't automatically put the value in memory. It might do, if you tell it to explicitly (using an assignment into a variable and then taking the address of the variable). But in some cases it may choose to store it in a register, which doesn't have an address. It may also choose not to store it at all, depending on optimisation options.

    0 讨论(0)
  • 2020-12-10 10:35

    A string literal is stored in memory as a character array. You identify an array with its starting address in C. However you cannot do

    int * ip = 5;
    

    Buy you can do (edited:)

    int ip[] = {5, 6, 4};
    

    for example.

    0 讨论(0)
提交回复
热议问题