Given an arbitrary type (I’ve chosen char
here, but that is for sake of concrete example):
char *p;
You can use either of these expressions:
sizeof(p) sizeof(char *)
Leading to a malloc()
call such as:
char **ppc = malloc(sizeof(char *)); char **ppc = malloc(sizeof(p)); char **ppc = malloc(sizeof(*ppc));
The last version has some benefits in that if the type of ppc
changes, the expression still allocates the correct space.
Related Posts:
- Arrow operator (->) usage in C
- Passing by reference in C
- Why does the arrow (->) operator in C exist?
- expression must have integral type
- Is the sizeof(some pointer) always equal to four?
- How to printf a memory address in C
- How to convert const char* to char* in C?
- Invalid type argument of -> C structs
- What does ** mean in C?
- C: pointer to array of pointers to structures (allocation/deallocation issues)
- Casting a pointer to an int
- Initialization makes pointer from integer without a cast – C
- lvalue required as left operand of assignment error when using C++
- Pointer Arithmetic
- What does “dereferencing” a pointer mean?
- What does “dereferencing” a pointer mean?
- char *array and char array[]
- Are the C++ & and * operators inverses in all contexts?
- C pointers and arrays: [Warning] assignment makes pointer from integer without a cast
- What is the difference between const int*, const int * const, and int const *?
- how does the ampersand(&) sign work in c++?
- What is the difference between const int*, const int * const, and int const *?
- Difference between char* and const char*?
- What does ** do in C language?
- What’s the difference between char and char* in C++?
- #31 expression must have integral type
- How do you pass a function as a parameter in C?
- Invalid pointer error on invoking free() after malloc in C
- sizeof float (3.0) vs (3.0f)
- When should I use the new keyword in C++?
- expression must have integral type
- How do you pass a function as a parameter in C?
- How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?
- How do you pass a function as a parameter in C?
- C free(): invalid pointer
- What’s wrong with my code? What is argv[1]?
- Valgrind: Invalid read of size 1
- Constant pointer vs Pointer to constant
- Does sizeof return the number of bytes or the number of octets of a type in C?
- Reversing a string in C
- Warning: return from incompatible pointer type in C
- Warning: assignment from incompatible pointer type
- strcmp giving segmentation fault
- Why am I getting this error: “data definition has no type or storage class”?
- Initialization from incompatible pointer type warning when assigning to a pointer
- Typedef function pointer?
- What is the use of intptr_t?
- Returning an array using C
- warning: passing argument ’from incompatible pointer type [enabled by default]’
- char pointers: invalid conversion from ‘char*’ to ‘char’?
- What is the difference between char array and char pointer in C?
- Why do I get “cast from pointer to integer of different size” error?
- Why use pointers?
- Difference between sizeof(char) and sizeof(char *)
- What’s the difference between * and & in C?
- Excess elements of scalar initializer for pointer to array of ints
- What exactly is meant by “de-referencing a NULL pointer”?
- Why is the sizeof(int) == sizeof(long)?
- Allocating char array using malloc
- typedef struct pointer definition
- Difference between char *argv[] and char **argv for the second argument to main()
- Difference between char* and char** (in C)
- Warning: comparison of distinct pointer types
- Realloc Invalid Pointer in C
- Dereference void pointer
- The difference between char * and char[] [duplicate]
- What does ‘return *this’ mean in C++?
- Difference between the int * i and int** i
- Pointer to 2D arrays in C
- Pointer to a string in C?
- Using pointer to char array, values in that array can be accessed?
- C – Error is “free(): invalid next size (normal) “
- Meaning of *& and **& in C++
- lvalue required as increment operand
- What is newline character — ‘\n’
- Why does ENOENT mean “No such file or directory”?
- What does the question mark character (‘?’) mean?
- What does `*&` in a function declaration mean?
- (->) arrow operator and (.) dot operator , class pointer
- No Symbol Table using GDB on Compiled Programs
- What is char ** in C?
- What is the difference between a static and const variable?
- Why is volatile needed in C?
- error: indirection requires pointer operand (‘int’ invalid)
- What is the difference between exit(0) and exit(1) in C?
- Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) standard C memory issue
- C char* to int conversion
- expected expression before ‘{‘ token
- C++ – include unistd.h: why not cunistd?
- Simple way to check if a string contains another string in C?
- How to convert integer to char in C?
- Does C have a string type?
- Difference between fgets and fscanf?
- How can I find the number of elements in an array?
- Array type char[] is not assignable
- What are “prototypes” in a C program?
- What’s the difference between “mod” and “remainder”?
- undefined reference to `std::ios_base::Init::Init()’
- Converting an integer to binary in C
- previous declaration of ‘function’ was here in C [duplicate]