Problem with returning pointer to a struct from a function in c++

Hi i'm making a program to find Harshad Nos b/w two given numbers,
i.e., A number that is divisible by the sum of its own digits. For example, 1729 is a Harshad number because 1 + 7 + 2 + 9 = 19 and 1729 = 19 × 91.

Now, i've made the program, but i'm getting weird output due to a problem in pointers i'm sure..
i've forgotten pointers in c++. Can someone please help ...

here is the no paste url to the code:

or otherwise below is the code:

#include <cstdlib>
    #include <iostream>
    using namespace std;
    struct IntArray{
           int a[];
           int arrayLength;// have to use this otherwise how would we know the length of array for displaying it.
    int reverseNumber(int);
    struct IntArray* getHarshadNumbers(int startno, int endno)
           IntArray ar; // to store the results
           ar.arrayLength = 0;
           int len = 0;
           int sum;
           //main loop to iterate b/w the start and end nos.
           for ( int i = startno ; i < endno ; i++ ){
               //loop to split the integer into single digits and simultaneously keep adding the no.
               //i.e. we keep on taking out the last digit and add it to the previous digit retrieved.
               //e.g. 1729
               //1st iteration : sum += 9 => 9
               //2nd iteration : sum += 2 => 11
               //3rd iteration : sum += 7 => 18
               //4th iteration : sum += 1 => 19
               sum = 0;
               for ( int number = i ; number >0 ; number /= 10){
                   sum += number%10;
               // this gets reverse of 19 = 91
               int revNo = reverseNumber(sum);
               // to check whether 19 * 91 = 1729 or not
               if ( sum * revNo == i ){
                    ar.a[ len ] = i;
                    //cout<<"in here"<<i<<endl;  
           ar.arrayLength = len;
           return &ar;
    // this function returns the reverse of a no.
    // we need this for getting the reverse of the sum we calculated in getHarshadNumbers()
    // e.g. for 1729 
    // sum = 19
    // now to find whether 19 x 91 = 1729 or not 
    // we must have a function to calculate the reverse of 19.
    int reverseNumber(int no){
             int revNo = 0;
             for ( int i = no ; i>0 ; i /= 10 ){
                 revNo += i % 10;
                 revNo *= 10;
                 //cout << revNo<<endl;
             revNo /= 10;
             return revNo;
    int main(int argc, char *argv[])
        int startNo,endNo;
        cout<<"Enter start no: ";
        cout<<"Enter end no.: ";
        IntArray *arr = getHarshadNumbers(startNo, endNo);
        // to display the returned struct array
        for ( int i = 0 ; i < arr->arrayLength ; i++){
        return EXIT_SUCCESS;

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


your returned structure has not been alloacted yet.
a static array is a simple solution for your case.
    you have declared IntArray ar; as local. the local variable scope ends with the function scope. so  sending back the address of a local variable is pointless.

declare dynamic memory

IntArray ar* = new IntArr();

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
And also you are not allocating the memory for ar.a[] . Please do that also
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.