• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Sizeof Operator

char *p1;  // or int *p1 or long *p1 or double *p1
printf("\n Sizeof P1 :: %d \n", sizeof(p1));

O/p ::  I always get 4 bytes .. Why all the times it gives 4 bytes
0
gauravflame
Asked:
gauravflame
  • 2
  • 2
  • 2
2 Solutions
 
ozoCommented:
apparently all pointers on your machine take 4 bytes.
did you want sizeof(*p1)?
0
 
gauravflameAuthor Commented:
I am here looking for concept ,why 4 bytes for char ,int or long? what is the theory ?

printf("\n Sizeof P1 :: %d \n", sizeof(p1));
0
 
gauravflameAuthor Commented:
I am  not looking for
sizeof(*p1)
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Infinity08Commented:
>> I am here looking for concept ,why 4 bytes for char ,int or long? what is the theory ?

It's for bytes for a POINTER TO char, int and long. It's the pointer that is 4 bytes long, not the value that it's pointing to.

A pointer is basically a memory address, and your system seems to use 4-byte memory addresses.
0
 
ozoCommented:
On many machines, chars, ints and longs share the same address space, so it could make sense for all pointers into that address space to be the same size.

There is also the requirement that
A pointer to void may be converted to or from a pointer to any incomplete or object
type. A pointer to any incomplete or object type may be converted to a pointer to void
and back again; the result shall compare equal to the original pointer.
0
 
Infinity08Commented:
>> It's for bytes for a POINTER TO char, int and long.

That should have been 4 instead of for of course :

"It's 4 bytes for a POINTER TO char, int and long."
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Simple Misconfiguration =Network Vulnerability

In this technical webinar, AlgoSec will present several examples of common misconfigurations; including a basic device change, business application connectivity changes, and data center migrations. Learn best practices to protect your business from attack.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now