What initializes the pointer?

One thing that has always confused me is a pointer to a symbol. It is after the long four years that I again linger in c.

Take, for example, the case mentioned. Why does the char operator behave this way? How can we directly address the contents of an item when it points to nothing or it looks like a char, the pointer stores unnecessary data!

 #include <stdio.h> #include <stdlib.h> int main() { char* charPtr="I cant understand why"; int* intPtr=60; printf("%d\n", intPtr); //displays 60 printf("%p\n", intPtr); // displays the hex value of 60 printf("%s\n", charPtr); // displays the wh0le string printf("%p\n", charPtr); // displays the start address of the string return 0; 

}

Further, the int pointer, how can it take the value 60 and where will it be stored?

Leaving aside the char and malloc pointer, I thought the main idea of ​​the pointer was to get an address pointing to!

why are these cases

  *intptr = 60 ; // should be setting the pointee value to 60 intptr = 60 ; // sets the address 

throw compile error while

  int* intPtr=60; 

sneak up without receiving the address (or 60 is taken as the address, if so, why is this unacceptable not in the first case) from the addressee!

Something is probably missing me, but hey! Guess what? they told me to search in SO!

EDIT: Pointing a pointer to a char pointer to an int pointer is also error free!

 int8_t* intPtr= (int8_t*)0x80485c8 ; // works without casting too ! I guess addresses are acceptable. 

Dividing it will result in a value equivalent to the first I line. Is this good practice or is there any other explanation for this, excluding the distribution of the byte byte size, for example, int may contain char and so ..?

As hmjd noted, “initialization syntax” is the problem! I have no problem writing my own code, but there is a problem when changing the user code.

+6
source share
4 answers

In C, a string literal like "I can’t understand why" is stored as a char array, so that memory is available for the entire duration of the program (all addresses are pulled from the air and are not intended to represent any particular platform or architecture):

 Item Address 0x00 0x01 0x02 0x03 ----- ------- ---- ---- ---- ---- "I..." 0x00080000 'I' ' ' 'c' 'a' 0x00008004 'n' ''' 't' ' ' 0x00008008 'u' 'n' 'd' 'e' 0x0000800C 'r' 's' 't' 'a' 0x00008010 'n' 'd' ' ' 'w' 0x00008014 'h' 'y' 0x00 0x?? 

A string literal is also an array expression, and in most contexts, an expression of the type "N-element array of T " will be converted to a type "pointer to T ", and its value will be the address from the first element of the array (exceptions are that the array expression is the operand of the sizeof or unary & or operators is the string literal used to initialize the array in the declaration).

So when you write

 char* charPtr = "I can't understand why"; 

you copy the address of the string literal to charPtr :

 Item Address 0x00 0x01 0x02 0x03 ---- ------- ---- ---- ---- ---- charPtr 0xffbe4000 0x00 0x08 0x00 0x00 

Please note that if the ad was

 char str[] = "I can't understand why"; 

str would be allocated as a char array long enough to hold the string, and the contents of the string would be copied to it:

 Item Address 0x00 0x01 0x02 0x03 ----- ------- ---- ---- ---- ---- str 0xffbe4000 'I' ' ' 'c' 'a' 0xffbe4004 'n' ''' 't' ' ' 0xffbe4008 'u' 'n' 'd' 'e' 0xffbe400C 'r' 's' 't' 'a' 0xffbe4010 'n' 'd' ' ' 'w' 0xffbe4014 'h' 'y' 0x00 0x?? 

When you write

 int* intPtr = 60; 

you initialize the value of the pointer to 60 without setting it to point to an anonymous integer with a value of 60:

 Item Address 0x00 0x01 0x02 0x03 ---- ------- ---- ---- ---- ---- intPtr 0xffbe4004 0x00 0x00 0x00 0x3C 

Address 60 is most likely not a valid address, so trying to dereference intPtr will most likely result in undefined behavior.

If you wrote something like

 int x = 60; int *intPtr = &x; 

then you will have this situation:

 Item Address 0x00 0x01 0x02 0x03 ---- ------- ---- ---- ---- ---- x 0xffbe4004 0x00 0x00 0x00 0x3C intPtr 0xffbe4008 0xff 0xbe 0x40 0x04 

In this case, the value of intPtr is the address of x .

Finally, note that initialization and assignment are not the same thing.

 T *x = value; 

does not invoke x and assigns the result value ; it assigns value directly to x . The value type is treated as T * . Please note that you should receive alerts on

 int *intPtr = 60; 

along the lines of "creating a pointer from a whole without a cast".

+4
source

How can we directly access the contents of the addressee when it points to nothing or it looks like a char, the pointer stores data other than addresses!

I think confusion is the initialization syntax. It:

 char* charPtr="I cant understand why"; 

does not cause a charPtr break. This is equivalent to:

 char* charPtr; charPtr = "I cant understand why"; 

Both code snippets store the string literal address "I cant understand why" in charPtr . There is no dereferencing of a pointer indicating that nothing is happening. A pointer variable of any type can only store an address.

It:

 int* intPtr=60; 

stores address 60 in intPtr : no int assignment or deferment. At this point, there is an int variable. The compiler should have issued a warning on this line. Any attempt to respect intPtr will most likely fail.

+6
source

When you write:

  char* charPtr = "I can't understand why"; 

This means that the base address of the string "I can’t understand why" is assigned

charPtr because the string literal is also a pointer to this string.

It can be considered as:

the concept of string stored in an char array

This means that the base address of the entire string is stored in charPtr. Now this is what you did in your code.

  char *charPtr="i cant understand why"; 

Add to this if you print statements like:

 printf("%c","i cant understand why"[0]);//prints i printf("%c","i cant understand why"[2]);//prints c 

These two printfs justify my concepts that the string “I can’t understand why” is itself a pointer to the char array in which the string is stored.

+3
source

Your intPtr initialized, indicating an absolute memory address of 60 . There is no backup storage.

Since you are not looking for the pointer itself, you never try to read address 60 , which is likely to cause your program to crash depending on the environment.

Instead, you pass the pointer value to printf , which takes almost anything as arguments and interprets the values ​​you specify in the format string. In your case, it will interpret the address of the pointer instead of the value of the pointer. Address 60 so that it is displayed. If you were to use *intPtr , it would probably crash instead.

+1
source

Source: https://habr.com/ru/post/924044/


All Articles