How to pass an array of struct as a function parameter?

This is not working:

struct Person
{
    char* name;
    int age;

    Person(char* name, int age)
    {
        this->name = name;
        this->age = age;
    }
}

struct XYZ
{
    int count;
    Person* people[];

    XYZ(int count, Person* people[])
    {
       this->count = count;
       this->people = people; // << Error here.
    }
}

void ABC()
{
    Person* people[] = { new Person("Tom", 18), new Person("Jacob", 25) };
    XYZ* xyz = new XYZ(2, people); // << Error
}

Recommended Answers

All 6 Replies

Oh... small piece of code, lots of problems:

struct Person
{
    char* name; //this is a pointer to a char (or array of char, with null-termination)
    int age;

    //For good practice, use parameter names that don't conflict with data member names.
    Person(const char* aName, int aAge) //notice the "const" on aName to make it illegal for this constructor to change aName.
    {
        //this->name = name; //This is memory-horror. Setting the pointer name to aName does not copy the memory content of aName (i.e. the string).
        //This will copy the name: begin{
        int length = strlen(aName);
        name = new char[length+1];
        strcpy(name,aName);
        //}end
        age = aAge; //notice, no need for this-> when names are not ambiguous.
    }
    //It is unacceptable for a class or struct to hold a pointer without cleaning its memory upon destruction, with this:
    ~Person() {
      if(name)
        delete[] name;
    }
}

struct XYZ
{
    int count;
    //Person* people[]; //Cannot reseat an array [] type, or at least not on standard compilers. That's why "this->people = people;" caused an error. 
    Person** people;

    XYZ(int aCount, const Person* aPeople[]) //notice it is fine to send it as array[] in parameter list (but add const).
    {
       count = aCount;
       //this->people = people; // << Error here. //Again, same memory-horror as above.
       if(count) {
         people = new People*[count];
         for(int i = 0; i < count ; ++i)
           people[i] = aPeople[i];
       } else {
         people = NULL;
       }
    }
    //Again, ALWAYS CLEAN THE MEMORY!
    ~XYZ() {
      if(count) {
        for(int i = 0; i < count ; ++i)
          delete people[i];
        delete[] people;
      }
    }
}

void ABC()
{
    Person* people[] = { new Person("Tom", 18), new Person("Jacob", 25) };
    XYZ* xyz = new XYZ(2, people); // << Error... Now this should work just fine.
}

Well, I'm a noobie but when I have to send an entire structure array, I just send the pointer to the 1st position of the array. Here is a sample code

# include <iostream>
using namespace std;

typedef struct AA
{
    int a;
}A;

void print (A *struc_point)
{
    cout << struc_point -> a << "\n";
}

int main()
{
    int i;
    A new_struc[5];

    new_struc[0].a = 1;             //  GIVING SOME RANDOM DATA
    new_struc[1].a = 2;
    new_struc[2].a = 3;
    new_struc[3].a = 4;
    new_struc[4].a = 5;

    A *pointer = &(new_struc[0]);   //  PUTTING THE ADDRESS OF THE
                                    //  1ST STRUCTURE VARIABLE IN 'pointer'

    for(i = 0; i < 5; i++)
    {
        print(pointer);
        pointer ++;                 //  GOING TO THE NEXT STRUCTURE VARIABLE
    }

    return 0;
}

Damn! Mike was too fast.
Nice one mike_2000_17!

Thanks for your help.

@mike: I am getting these errors now:

cannot convert from 'const Person *[]' to 'Person **'	
cannot convert parameter 1 from 'Person *[2]' to 'const Person *[]'

Ok, well change the constructor parameter aPeople to the type Person** instead:

XYZ(int aCount, Person** aPeople)

The [] notation of arrays is a remnant of C, and C++ compilers don't deal well with it.
And I was wrong about the const there, if a const should be any where, it's at the end to indicate the pointer is constant, "Person** const aPeople", but don't worry about that.

Thanks a lot. It is working now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.