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

Posted on 2008-11-04
Last Modified: 2013-12-14
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

Question by:shilpi84
    LVL 5

    Expert Comment


    your returned structure has not been alloacted yet.
    a static array is a simple solution for your case.
    LVL 2

    Accepted Solution

        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

    LVL 2

    Expert Comment

    And also you are not allocating the memory for ar.a[] . Please do that also

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now