Difference between sizeof(*p) and sizeof(p)?
I am using code below and getting different values.
int *p; printf("Size of *p = %d", sizeof(*p)); // Here value is 4 printf("Size of p = %d", sizeof(p)); // Here value is 8
Can any one please explain, what exactly is the reason behind this?
For any pointer variable
p, the variable
p itself is the pointer and the size of it is the size of the pointer.
*p is what
p is pointing to, and the size of
*p is the size of what is being pointed to.
8 then you know that a pointer on your system is
8 bytes, and that you're probably on a 64-bit system.
4 then you know that the size of
int (which is what
p is pointing to in your case) is
4 bytes, which is normal on both 32 and 64 bit systems.
You would get the same result by doing
Oh and a last note: To print the result of
sizeof (which is of type
size_t) then you should really use the
"z" prefix, and an unsigned type specifier (since
size_t is unsigned). For example
"%zu". Not doing that is technically undefined behavior.
What's the different between *p++,* ++p,++*p in C , classes, structures, unions and any other user defined data type. If you see a “P” in front of the tire size displayed on a tire’s sidewall it is a P-metric tire. If there’s no P, it’s a Metric tire (also called Euro-metric or Hard-Metric).
sizeof(p) is the size of the pointer itself. It depends on the size of the address bus. Which means for a 64-bit system, the address bus size will be 64-bit (8 bytes) so pointer will be 8 bytes long (that shows your system is 64-bit). And on a 32-bit system, it's size will be 32-bit(4 bytes).
sizeof(*p) is the size of pointer type i.e.
int here. So usually
int is of 32-bit long that is 4 bytes.
C++ sizeof Operator, is 32 bits ( 4 bytes), while on 64 bit machine it's 8 byte. Regardless of what data type they are pointing to, they have fixed size. "p" serves to output a pointer. It may differ depending upon the compiler and platform. One specifier is often used instead of another on 32-bit systems, but it is a mistake.
sizeof(*p) returns size of type what the pointer points to while
sizeof(p) returns size of pointer itself.
In your case
*p = int and
sizeof(int) = 4 on your machine, while you need
8 bytes to store memory address (address where
p points to).
What is the size of a pointer in C?, preprocessor expressions, such as #if, because the preprocessor has no data types. sizeof(p) is the size of the pointer itself. It depends on the size of the address bus. It depends on the size of the address bus. Which means for a 64-bit system, the address bus size will be 64-bit (8 bytes) so pointer will be 8 bytes long (that shows your system is 64-bit).
sizeof, *p++ vs (*p)++ ? • x = *p++. ⇒ x = *p ; p = p + 1;. • x = (*p)++ ⇒ x = *p ; *p = *p + 1; (objects)?. • C takes care of it: In reality, p+1 doesn't add. 1 to the memory address, C knows the size of the thing a pointer There is a difference between. What is strlen() strlen() is a function to find the length of a string. It counts total characters which are presented in a string, eliminating the null character.
[PDF] C Reference Slides, Do not use sizeof for array parameters · Difference between strlen() and sizeof() for string in C · How to find size of array in C/C++ without using sizeof ? dot ( The expression *p++ is treated as * (p++) as the precedence of postfix ++ is higher than *. Therefore the output of second program is “ arr  = 10, arr  = 20, *p = 20 “. The expression *++p has two operators of same precedence, so compiler looks for assoiativity. Associativity of operators is right to left.
sizeof operator in C, if p is a reference typed variable, then: p++ means: 1. p = p + 1 and will store p + 1*sizeof( (*p) ) into p 2. returns the OLD value (before the increment) in p p-- In char* p = “hello”, the string “hello” goes into the data segment while the pointer p in itself is stored in the stack. In char p = “hello”, the entire thing is stored in the stack.