Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between sizeof(x) and sizeof(p_x)

Tags:

c

Can u tell me what is the difference between sizeof(x) and sizeof(p_x) in the code below?

int x[10], *p_x;
p_x = (int*)malloc(10 * sizeof(int));
like image 253
Tiestik Avatar asked Dec 03 '22 17:12

Tiestik


1 Answers

sizeof(x)

is giving the number of bytes used by the array x.

sizeof(p_x)

is giving the number of bytes used by a pointer.

#include<stdio.h>

int main() {
    int x[10], *p_x;
    printf ("%lu %lu\n", (unsigned long)sizeof(x), (unsigned long)sizeof(p_x));
    return 0;
}

Program output:

40 4

My MSVC uses 32-bit pointers and 32-bit ints.

EDIT improved number formatting following below comments, thanks.

like image 80
Weather Vane Avatar answered Dec 14 '22 22:12

Weather Vane