S
subramanian100in
Is it possible to measure the size of an array without using the
sizeof operator ?
sizeof operator ?
Why would you want to? That's what sizeof is for.Is it possible to measure the size of an array without using the
sizeof operator ?
Is it possible to measure the size of an array without using the
sizeof operator ?
[email protected] said:Is it possible to measure the size of an array without using the
sizeof operator ?
Why would you want to? That's what sizeof is for.
I saw this question in one of the earlier posts but the answer was not
given.
[email protected] said:.... snip ...
Suppose I have, for some specific Type,
Type a[size];
I calculate
sprintf(str1, "%p", (void *)(a+1));
sprintf(str2, "%p", (void *)a);
.... snip ...
Correct me for all errors.
I saw this question in one of the earlier posts but the answer was not
given.
I am learning C from home. I have a PC at home. I am reading
K & R second edition. I post my doubts in this forum to get clarified.
I am preparing for interview. That is why I am asking.
I thought of doing it in one way without using sizeof. But it
ASSUMES that sizeof pointer should not be bigger than maximum
value of unsigned long or unsigned long long. Before writing the
code, let me explain it in plain English because, in this forum I wil
get to know if the assumption is correct.
In the following, assume that size, str, etc exist.
Suppose I have, for some specific Type,
Type a[size];
I calculate
sprintf(str1, "%p", (void *)(a+1));
sprintf(str2, "%p", (void *)a);
Then I have my own routine to convert str1 and str2 to unsigned
long or unsigned long long based on the ASSUMPTION mentioned
above. I find the difference of these converted numbers and then
multiply the difference by size.
This is what strikes me.
Correct me for all errors.
Suppose I have, for some specific Type,
Type a[size];
I calculate
sprintf(str1, "%p", (void *)(a+1));
sprintf(str2, "%p", (void *)a);
Then I have my own routine to convert str1 and str2 to unsigned long
or unsigned long long based on the ASSUMPTION mentioned above. I find
the difference of these converted numbers and then multiply the
difference by size.
This is what strikes me.
Correct me for all errors.
I thought of doing it in one way without using sizeof. But it ASSUMES
that sizeof pointer should not be bigger than maximum value of
unsigned long or unsigned long long. Before writing the code, let me
explain it in plain English because, in this forum I wil get to know
if the assumption is correct.
In the following, assume that size, str, etc exist.
Suppose I have, for some specific Type,
Type a[size];
I calculate
sprintf(str1, "%p", (void *)(a+1));
sprintf(str2, "%p", (void *)a);
Then I have my own routine to convert str1 and str2 to unsigned long
or unsigned long long based on the ASSUMPTION mentioned above. I find
the difference of these converted numbers and then multiply the
difference by size.
This is what strikes me.
Is it possible to measure the size of an arraywithoutusingthesizeofoperator ?
Yes, its possible.
I am explaining it below with an example.
int a[10];
printf("total size occupied by the array is %d\n", ( ((&a+1)-&a) *
((char*)(a+1)-(char*)a) );
this will yield the same size as sizeof(a) yeilds.
Is that the answer that you are expecting?..
(e-mail address removed) said:
Yes, its possible.
Albeit silly.
I am explaining it below with an example.int a[10];printf("total size occupied by the array is %d\n", ( ((&a+1)-&a) *
((char*)(a+1)-(char*)a) );
this will yield the same size as sizeof(a) yeilds.
Well, it's hard to see how, since it won't actually compile. (Check your
parentheses.)
When I fix that, and add a printf of the result of sizeof, I get the
following code:
#include <stdio.h>
int main(void)
{
int a[10];
printf("total size occupied by the array is %d\n",
(((&a + 1) - &a) * ((char *)(a + 1) - (char *)a)));
printf("sizeof a = %d\n", (int)sizeof a);
return 0;
}
When I run this code, I get the following output:
total size occupied by the array is 4
sizeof a = 40
Is that the answer that you are expecting?..
Sure as eggs as eggs it isn't the answer *you* were expecting!
Yes you are right.
the code need to be changed like this.
int main()
{
int a[10];
printf("total size =%d\n",(char*)(&a+1)-(char*)(&a));
printf("sizeof value=%d\n",sizeof(a));
}
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.