Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 970
  • Last Modified:

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:http://rafb.net/p/mBNuU019.html

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);
               //cout<<endl<<"iter"<<i;
               // to check whether 19 * 91 = 1729 or not
               if ( sum * revNo == i ){
                    
                    ar.a[ len ] = i;
                    //cout<<"in here"<<i<<endl;  
                    //cout<<"arrayLenght:"<<ar.arrayLength; 
                    //cout<<"Lenght:"<<len; 
    
                    len++;
                    
               }
           }
           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 ){
                 //cout<<"i"<<i<<endl;
                 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: ";
        cin>>startNo;
        
        cout<<"Enter end no.: ";
        cin>>endNo;
        
        IntArray *arr = getHarshadNumbers(startNo, endNo);
        
        // to display the returned struct array
        for ( int i = 0 ; i < arr->arrayLength ; i++){
            
            cout<<endl<<arr->a[i];
        }
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

Open in new window

0
shilpi84
Asked:
shilpi84
  • 2
1 Solution
 
rendaduiyanCommented:

your returned structure has not been alloacted yet.
a static array is a simple solution for your case.
0
 
prakash2007Commented:
Hi,
    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

i.e
IntArray ar* = new IntArr();

Open in new window

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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now