There's a simple question?
how to print the size of a variable without using functions like sizeof() ?
for example if you were asked to write a program that prints size of int without using sizeof() with a resault as below:
cout<<sizeof(int);
soroush68
0
Newbie Poster
Recommended Answers
Jump to PostOh good, another one of those pointless "can you do... without using the obvious... " exercises which some tutors seem to enjoy giving, but which have absolutely no use outside the classroom.
Print the address of two elements of an array of them.
Jump to Postdeclare two variables a and b then subtract their addresses. That is really a terrible way to do it because there is no guarentee that the two variables will be next to each other -- the compiler is free to put anything it wants between those variables. Using the sizeof …
Jump to PostI ran this for fun.
Is it necessary to cast the addresses as ints before subtracting? I printed both addresses, and saw that they were 4 apart, but
&x[1] - &x[0]
came out as 1. But when I cast them to ints, it was 4. Why is the cast …
Jump to Post"&x[1]" doesn't the compiler uses sizeof to evaluate this expression?
Since I am not sure of this. I think unions can be used to find the answerI wrote this program & it seems to work.
#include<stdio.h> int main () { union findsize { unsigned char …
Jump to PostWhy is the cast necessary?
Because without the cast the difference is 1 integer. If you take the difference between &x[2] - &x[0] the difference is 2 integers.
All 19 Replies
Salem
5,138
Posting Sage
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
soroush68
0
Newbie Poster
CoolGamer48
65
Posting Pro in Training
Prabakar
77
Posting Whiz
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
Salem
5,138
Posting Sage
Prabakar
77
Posting Whiz
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
CoolGamer48
65
Posting Pro in Training
Salem
5,138
Posting Sage
ArkM
1,090
Postaholic
soroush68
0
Newbie Poster
ArkM
1,090
Postaholic
Prabakar
77
Posting Whiz
ArkM
1,090
Postaholic
Salem
5,138
Posting Sage
Prabakar
77
Posting Whiz
bharath404
-4
Newbie Poster
Salem
commented:
Bumping a thread to simply repeat answers given 18 months ago
-4
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.