6
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by Ali_2101
0

A tricky way, may not be the best way. Here it is:

#include <iostream>

using namespace std;

int main()
{
    int a[2] = {1,5};
    long addr1 = (long)(&a[0]);
    long addr2 = (long)(&a[1]);
    cout<<(addr2 - addr1)<<endl;
    return 0;
}

take absolute value of addr2-addr1 inorder to avoid a negative answer.

Edited by subith86: n/a

-2

hi all,
How to find size of structure without size off operator..using c
thanks in advance

Votes + Comments
Agreed
There's no need for 3 separate threads on this
0
#include<iostream>
using namespace std;
union test
{
        int i;
        float j;
        double k;
};

int main()
{
        test obj1;
        cout<<sizeof(obj1)<<endl;
        cout<<(int)(&obj1 + 1) - (int)(&obj1)<<endl;
        return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.