Is there a standard pointer size declaration?

I have a structure with padding in char(oops, my bad). I would like to subtract the size of the pointer. Do you know the standard pointer size declaration or the standard macro for this?

+3
source share
4 answers

sizeof (void *)

+8
source

Do you need an answer to the C-standard or an answer that works almost all the time?

Usually, all data pointers are the same size, which is equal sizeof(void*).

"C" "", , C. , POSIX, Win32, . , , - "" "" , , , "" C . , int 2 char, int 4-. , , 64 , , int* 2 , char* void* 3. , C , sizeof(int*) < sizeof(char*).

, , , p , sizeof p.

, , , . , .

+8

, offsetof(), stddef.h:

#include <stdio.h>
#include <stddef.h>

int main(void)
{
    struct s {
        int i;
        char c;
        double d;
        char a[];
    };

    /* Output is compiler dependent */

    printf("offsets: i=%ld; c=%ld; d=%ld a=%ld\n",
            (long) offsetof(struct s, i),
            (long) offsetof(struct s, c),
            (long) offsetof(struct s, d),
            (long) offsetof(struct s, a));
    printf("sizeof(struct s)=%ld\n", (long) sizeof(struct s));

    return 0;
}

$ ./a.out
offsets: i=0; c=4; d=8 a=16
sizeof(struct s)=16
+2

sizeof (void *).

+1

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


All Articles