Reputation: 517
I am new to C. I saw this line in a program:
grid = calloc(nx,sizeof(int**));
I read that int**
means a pointer to a pointer, but what is meant by sizeof(int**)
??
Upvotes: 2
Views: 3890
Reputation: 501
The sizeof
expression is just the number of bytes needed to represent a pointer-to-pointer-to-int
. Presumably, whoever wrote the code is looking to allocate enough memory to store nx
such pointers.
Upvotes: 2
Reputation: 123458
The sizeof
operator yields the number of bytes of storage required by its operand. The operand is either an expression or a type enclosed in parentheses. In this case, the operand is the type int **
, which is "pointer to pointer to int
".
Assuming grid
has been declared as
int ***grid;
then that can be rewritten as
grid = calloc(nx, sizeof *grid);
Upvotes: 3
Reputation: 223872
sizeof(int **)
tells you how many bytes an int **
is, similarly to how sizeof(int *)
tells you how many bytes an int *
is. They just have different levels of indirection.
Upvotes: 1