Character pointers and integer pointers (++)

I have two pointers,

char *str1; int *str2; 

If I look at the size of both pointers, let's say

 str1=4 bytes str2=4 bytes 

str1 ++ will increment by 1 byte, but if str2 ++ will increment 4 bytes.

What is the concept of this?

+4
source share
10 answers

Simple in the above scenario:

  • char lasts 1 byte
  • int (on your platform) is 4 bytes long

The ++ operator increments the pointer to the size of the pointed type.

+16
source

When performing arithmetic on a pointer, it always refers to the objects that it points to, and not in bytes.

Thus, a pointer whose target is, for example, four bytes, will increase its actual numerical value by four when you add it.

It is much more convenient and makes much more sense than all arithmetic of the pointer in bytes.

+9
source

A char is 1 byte, and int is (usually) 4 bytes. When you increase the pointer, you increase the size of the data pointers. So, when you increase the char* value, you increase by 1 byte, but when you increase int* , you increase 4 bytes.

+4
source

The pointer actually contains the address of the memory cell, which is 4 bytes. str1 points to a location that contains 1 byte, so if you increment the address of str1, it goes to the next address from 1 byte of data. But in another case, str2 points to 4-byte data, so if you increment this address, it must jump over that data to go to the next 4-byte data, so it increments by 4.

This is how 1 byte data sequence is stored in memmory:

 ADDRESS: FF334400 FF334401 FF334402 FF334403 DATA (1BYTE): 1 2 3 4 

So, if str1 wants to point to number 2, it must contain its address, which is FF334401. If you increase str1, it must jump over the 2s address and go to 3, and for this it must be increased by 1.

Otherwise:

 ADDRESS: FF334400 FF334401 FF334402 FF334403 FF334404 ... FF334407 DATA (4BYTE): 0 0 0 1 0 2 

Now, if str2 points to the number 1, which is an integer, and it is relevant, it is 4 bytes of data, it indicates the beginning of this data, which is the address of FF334400. When you increment it, it must jump over all 4 bytes of 1s data to get 2s data, so it increments by 4 and its address becomes FF334404, which is the first data byte of 4 bytes of number 2.

+3
source

Hint: p[i] is short for *(p + i) .

+1
source

Since this behavior is more useful than an alternative, and allows you not to worry about how large a particular data type is.

Consider an array and an integer pointer to this array:

 int p[10]; int *q = p; 

Then * (q + 1) coincides with p [1], that is, next int in memory. It would be much less useful if he just pointed one byte forward.

0
source

The increment of the pointer always increases the address to which its size indicates the type represented by it. Thus, for the char pointer, it increases by 1 and for integers by 4. But for the pointer variable itself, 4 bytes are required to store the address.

You might think about how array indexing works. Inca of an integer array a [0] will point to the first element, and [1] will point to the second. In this case, for increment 1, it must be incremented by 4 bytes to access the next integer. In the case of characters, this should be 1. The same concept works for all arithemtic pointers.

0
source

Simple It depends on the compiler.

If int has 4 bytes of size, when you add 1 to its pointer, it will add its size to it, that is, for and if int has 2 bytes, then it will add 2 whose size to the pointer. For example, in Turbo C ++

 int *str = NULL; str + 1; //It will add 2 as Turbo C++ has int size 2 bytes 

In Visual C ++ , str + 1; // It will add 4 since Visual C ++ has an int size of 4 bytes.

And the same thing happens with char.

0
source

This is according to pointer arithmetic. Here it is.

As you said, the memory allocated for all pointers is the same. But when you use the increment operator with a pointer variable, it means that a pointer must be made to indicate (increment) the next place in memory.

So, if you use a pointer to a character, then if you increment, you must point to the next character one byte wide. Similarly, if you want to increment a numeric pointer, it looks like it points to the next integer, which is four bytes wide.

I think this is enough to clarify your question :)

0
source

A pointer is an abstraction that allows you to reference data in memory so that access to raw memory is used as an atomic block to ensure that it is interpreted appropriately for the selected type.

The pointer itself is represented by its own machine word size. In your example, you have two pointers to different types, but they are still pointers to addresses in memory, and therefore they are the same size. As mentioned in other answers, in order to get the size of the data type that the pointer points to, you must dereference it in a sizeof operation, for example. SizeOf (* p).

The ++ operator allows you to get a pointer that refers to the next address in memory for the corresponding type. If he simply increased the address by one byte for all types, you could get an address in memory that points to the middle of the data view

eg. for two unsigned 4-byte integers representing decimal values ​​1 and 4 278 190 080, respectively, starting from the address 0x00 in memory (note that the address here is just for illustration and does not represent a real system, since the OS reserves them for its own goals) / p>

 address 0x00 0x01 0x02 0x03 | 0x04 0x05 0x06 0x07 data value (4 byte integer) 0x00 0x00 0x00 0x01 | 0xFF 0x00 0x00 0x00 

If the pointer to the integer has a reference to the address 0x00, and the ++ operator simply increased the address of the pointer by 1 byte, you would have a pointer to the address 0x01, which, if you accessed this address (plus the following 3 bytes), as an integer , you get the integer represented by the data bytes 0x00 0x00 0x01 plus the value of the address 0x04, which in this case is the value 0xFF. This will result in an integer with a decimal value of 511, which is not one of the two integers stored in memory.

For proper access to the next integer in memory, the ++ operator must increment the pointer byte address by 4 bytes.

0
source

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


All Articles