How do pointers know the base size of an element?

As specified in the section How does incrementing a pointer work? I have the following question.

How does a pointer know the base size of the data it points to? Pointers have a base type size so they can know how to increase?

I expect the following code to move the pointer forward one byte:

int intarr[] = { ... };
int *intptr = intarr;
intptr = intptr + 1;
printf("intarr[1] = %d\n", *intptr);

In accordance with the accepted answer on a linked site with an increment of the pointer by bytes rather than the base sizeof, the pointed element will cause mass hysteria, confusion and chaos.

Although I understand that this is likely to be an inevitable result, I still don't understand how pointers work in this regard. It was not possible to declare a pointer voidfor some type array struct[], and if I did, how would the pointer voidknow to increment by sizeof(struct mytype)?


Edit: I believe that I have worked with most of the difficulties that I have, but I'm not quite there while this is being demonstrated in the code.

See here: http://codepad.org/0d8veP4K

#include <stdio.h>

int main(int argc, char *argv[])
{ 
    int intarr[] = { 0, 5, 10 };
    int *intptr = intarr;

    // get the value where the pointer points
    printf("intptr(%p): %d\n", intptr, *intptr);
    printf("intptr(%p): %d\n", intptr + 1, *(intptr + 1));
    printf("intptr(%p): %d\n", intptr + 2, *(intptr + 2));

    // the difference between the pointer value should be same as sizeof(int)
    printf("intptr[0]: %p | intptr[1]: %p | difference: %d | expected: %d",
        intptr, intptr + 1, (intptr + 1) - intptr, sizeof(int));

    return 0;
}
+4
source share
5 answers

Pointers retain the size of the base type so they can know how to increase?

, , , . . .

, : .

:

int a,b,c;
float x,y,z;

void f(void)
{
  c = a+b*3;
  z = x+y*3;
}

( float vs. int , , , . , , .)

f , . (+ *) C, . , , , . 3 ​​- , .

, a, b, c, x, y z, , . , a, b c.

C- . . ( " " ++ C - , . , "ADD" () "FADD" ( ) +. )

. - . a=a+1 , a int float, , a int *, , a struct tm * ..?

C . . .

+2

. p1 , sizeof(*p1) sizeof(int). p2 , sizeof(void) .

int *p1; 
void *p2;

p1++;  // OK
p2++;  // Not defined behavior in C
+5

, , , C, . , , - , , , , int. .

, , , , :

int z = x + y;

, , , , , , x y z, ints?

/ , , . , , , .

:

int intarr[] = { ... };
int *intptr = intarr;

- intptr + 1 , sizeof(int). , , , , , C-. , :

int intarr[] = { ... };
void *voidptr = intarr;

... voidptr , , , , .

void struct [], , void , sizeof(struct mytype)?

. void , . , , . , void memcpy, . ​​, , , .

+1

:

  • sizeof (pointer) - 4
  • sizeof (int) - 4

   Output:
    intptr(0xffcbf5dc): 0
    intptr(0xffcbf5e0): 5
    intptr(0xffcbf5e4): 10
    intptr[0]: 0xffcbf5dc | intptr[1]: 0xffcbf5e0 | difference: 1 | expected: 4

: 0xffcbf5e0 - 0xffcbf5dc = 4 (hex sub), sizeof (int).

  • void *: void *
  • about your structure: you can do sizeof (yourStructre)
0
source

the moment you initialize the pointer as int *, the compiler knows that it is a pointer to int, and each jump must match sizeof (int).

about your void question * if you do something like:

int a[4] = {0,1,2,3}
void *b = a;
int c = a[1]; //a will point to the 1 so c will be 1
int d = b[1]; //it depends on the compiler where is b going to end
              //and which value will c take, some compilers may not accept it

that is, reason functions that need void * as a parameter, for example fwrite (), request the size of the structure and the size of the array

-2
source

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


All Articles