DIGIT-ARC read-only archive · snapshot: July 23, 2026

If there is no sizeof operator in C

10 posts · originally from https://geek.digit.in/community/threads/if-there-is-no-sizeof-operator-in-c.156851/

S
sumanish Original post #1
May 11, 2012 at 5:05 PM
I do not have much knowledge about compilers and all.

I have one simple question about C programming.

If there is no sizeof operator in C then does those pointer arithmetic work or not ?

I am very much confused about it. :-?

Please somebody help.
nbaztec
nbaztec #2
May 14, 2012 at 9:11 PM
sizeof operator is present in C.
Vyom
Vyom #3
May 14, 2012 at 11:39 PM
If there is no sizeof operator in C then does those pointer arithmetic work or not ?

I think you need to read some books. Some good books. Non-Indian authors.
tkin
tkin #4
May 15, 2012 at 1:57 AM
I learned about this operator when learning C, when doing dynamic memory allocation, read up on malloc, calloc.
Liverpool_fan
May 17, 2012 at 1:38 AM
sizeof - Wikipedia, the free encyclopedia
T
Tachy1 #6
Jun 25, 2012 at 11:37 PM
yes there is ....if u r having probs using calloc,malloc etc maybe u forgot to use type casting..
audiophilic
Jun 26, 2012 at 8:27 PM
It will greatly depend on the compiler you have :D
rijinpk1
Jun 29, 2012 at 9:07 AM
sizeof() is put by the compiler itself when running pointer arithmatic. Without that, pointer arithmatic wont takeplace. That is what I understand as of now.
K
Jul 7, 2012 at 10:35 PM
hii there i learned this trick back in my college days to find the size of a variable without the use of sizeof() operator .

#define find_size(x) (((char *)(&x+1))-((char *)(&x)))

int main(){
int i;
char c;
double d;
printf("%d\n",find_size(i));
printf("%d\n",find_size(c));
printf("%d",find_size(d));
return 0;
}

It works in almost all cases .Although sizeof () is a operator i think in assembly code it comes down to some piece of more basic code .

don't ask how to find size() if we don't have # define :oops:
That i don't know
nbaztec
nbaztec #10
Jul 8, 2012 at 12:24 PM
hii there i learned this trick back in my college days to find the size of a variable without the use of sizeof() operator .



It works in almost all cases .Although sizeof () is a operator i think in assembly code it comes down to some piece of more basic code .

don't ask how to find size() if we don't have # define :oops:
That i don't know

As cool and hacky as the trick may sound, it's bad code. And not because it does what sizeof() does, but because it is misleading and counter-intuitive. One should take some time out to understand what exactly is happening:

A pointer is nothing but a location to memory, and on a x86 systems it holds a 32-bit address. So effectively it is of type long (4 bytes) = int on x86 architecture.
Code:
        int i = 1337;
	printf("i(%d) is located at %d, next location is %d\n", i, &i, &i+1); 
	printf("Number of memory locations: %d - %d = %d\n", &i+1, &i, &i+1 - &i);
	printf("Number of bytes           : %d - %d = %d\n", &i+1, &i, (long)(&i+1) - (long)&i);

     /**
        * Output
	* i(1337) is located at 1834544, next location is 1834548
	* Number of memory locations: 1834548 - 1834544 = 1
	* Number of bytes           : 1834548 - 1834544 = 4
        */

So without typecasting, the difference between 2 pointers yields the no. of elements or memory locations. But once you typecast the pointer to a long, it is treated as a interger type memory location upon which you can perform the usual arithmetic operations. And what better way to get the number of bytes that to get the difference between 2 consecutive addresses.

So you can ditch that hacky macro, which I'm sure originated from some Indian authored book.