# Link a Struct to Array?

Hi,

I have a struct called 'Names' with only two properties. I need to declare a two dimensional array of X width and Y height, and link the array to the struct so that i can use the struct's properties with the array.

So something like...

Array[x][y].FName = "Johnson";

Cheers
###### Who is Participating?

Commented:
>> Yes, the array needs to be declared as the same type as the struct.

Then indeed do what abith said :

typedef struct Name {
std::string firstName;
std::string lastName;
} Name;

Name names[ROWS][COLS];

and you can access it like this :

names[i][j].lastName = "Doe";

>> ok now if i wanted to pass this array to another function, how do i do that with the * and & types?

You can define you function like this :

void fun(Name ns[ROWS][COLS]) {
ns[0][2].lastName = "Doe";   // <--- just an example
}

and call it like this :

Name names[ROWS][COLS];
fun(names);

And the modifications done by the fun function will be done to the names array.

If you're interested, there are some nice tutorials on pointers and function parameters here (and a lot more) :

http://www.cplusplus.com/doc/tutorial/
0

Commented:
You can declare this as two dimensional array of strings.
X -> number of struct objects.
Y -> no. of properties in the struct.

something s like :
string strArray[5][2];
...
...
strArray[4][0] -> This would denote the first property of the 5th struct.
strArray[4][1] -> This would denote the second property of the 5th struct.

Another approach can be to use an array to save the pointers to these structs.
all you need to do is to get that pointer and get the property from it.

But then this approach would not include any two dimensional approach as:
Array[x][y].FName = "Johnson";

Let me know if you have any doubt.
0

Author Commented:
Hi James,

Yeah i already have the declaration down, however im just having problems trying to link the struct to the array.

This is what i have:

struct Names
{
string FName;
string LName;;
};

........

string NameBase[10][10];         // Just throwing in some values

........

NameBase[2][3].FName = "John";
//End

Now im just having errors all over the shop. For starters im getting a "error C2372: 'NameBase" : redefinition; different types of indirection" error thats pointing me to my array declaration. And also i dont even have the struct linked to the array :S

Any ideas?
0

Commented:
>>>>>NameBase[2][3].FName = "John";
here NameBase[2][3] would contain a string. NameBase[2][3] wont be a Names struct. So, plz dont put '.FName'.

try it as :
struct Names
{
string FName;
string LName;
};

string NameBase[10][10];

NameBase[2][0] = "John"; // it is FName
NameBase[2][1] = "LastName"; // it is LName

Let me know if you have doubts.
0

Commented:
>>I need to declare a two dimensional array
array of which data type?, same struct or anyother?

if same struct,
STRUCTNAME stArray[X][Y];
now you can access like
stArray[0][0].Property1 = ".....";
if anyother pls, let us know which data type for array and struct properties.
0

Commented:
continuing from my previous comment.....
you can further trim down the memory by using it wisely.

if you know you need data for 'n' number of persons, then
make use of array as:
string NameBase[n][2];

here
NameBase[x][0] would be the FName of (x+1)th person.
and NameBase[x][1] would be the LName of (x+1)th person.

Hope this helps.

0

Author Commented:
duh, yeah sorry guys my heads all over the shop today.

Yes, the array needs to be declared as the same type as the struct. ok now if i wanted to pass this array to another function, how do i do that with the * and & types? (im useless with pointers)

Thanks
0

Commented:
you can pass it as &arrname
this will pass the pointer to first memory location. then you can make use of  it on other side.

0

Commented:
else if you dont want to make use of pointers at all......you can just pass them by value.

check out:
http://www.fredosaurus.com/notes-cpp/arrayptr/17array-parameters.html

Hope this helps.
0

Author Commented:
awesome, thanks a LOT guys :)
0

Commented:
>>>>       typedef struct Name {
as it is posted in C++ TA only and the members are class members, you don't need the 'typedef' which is for C compatibility only.

struct Name
{
std::string FName;
std::string LName;
};

You may consider to call the struct 'Person' or 'Fullname' cause 'Name' easily can give a conflict with variable names.

>>>>        Name names[ROWS][COLS];

That only works with VC compiler if ROWS and COLS were const integers (or macros). If you need a dynamic 'table' you may consider using std::vector:

std::vector< std::vector<Name> > names(ROWS, std::vector<Name>(COLS));

A std::vector is a dynamic array for any type which here was used as a 2D array ( an array of 'rows').  Here you could use typedef's to make the definition easier:

typedef std::vector<Name> NameRow;
typedef std::vector<NameRow> NameTable;

what allows to define the 'names' tables like

NameTable names(ROWS, NameRow(COLS));

After that you could access it like a fixed-sized array:

names[0][2].FName = "John";
names[0][2].LName = "Dow";

or (as long as struct Name has no constructor).

You would define a function argument like

void func(NameTable& names);

what means that you could pass the 2D array by reference. You don't need to pass the dimensions as you could get them in the function with

void func(NameTable& names)
{
int rows = (int)names.size();
int cols  = (int)names[0].size();

}

(the second statement only should be made unconditioned if you never pass an empty table).

Regards, Alex
0

Commented:
>> you don't need the 'typedef' which is for C compatibility only.

It doesn't hurt either ;)

>> That only works with VC compiler if ROWS and COLS were const integers (or macros).

I thought that would be clear by the use of capitals :)
0

Commented:
>>>> It doesn't hurt either ;)
Don't think you are right. In C++ a struct and a class are equivalent and the only difference is that members in a struct were public if not specified differently and in a class the default is private members. But, I think you never would do

typedef class Name {
public:
std::string firstName;
std::string lastName;
} Name;

though it would compile. It was simply not senseful.

Another reason is that the typedef definition in C is not really the same as it would be with C++. If for example you have

typedef struct Name
{
char* FName;
char* LName;
Name* pNext;
} Name;

it would not compile in C cause Name*  isn't a valid type at that point. To make it compile you need to make

typedef struct Name
{
char* FName;
char* LName;
struct Name* pNext;
} Name;

what would compile in C++ as well but makes it not better. IMO, if programming in C++ and a struct never has a chance to be used as a C interface, you should omit the typedef's as it  makes things circumstantial where it was not needed.

0

Commented:
>> Another reason is that the typedef definition in C is not really the same as it would be with C++.

It is the same. The difference is that C++ allows you to refer to struct Name as simply Name. In other words, the typedef is redundant in C++. Adding the typedef is entirely valid and will not break your C++ code, plus it has the added advantage that it's more compatible with C (for whatever reason you might need that).

I agree that you CAN just leave out the typedef for C++, but I don't agree that you SHOULD leave it out. Personally, I think adding it can avoid confusion, especially for people with a C background.

Oh well : in the end, it's just a matter of preference. :)
0

Commented:
>>>> Oh well : in the end, it's just a matter of preference. :)
Yes, to that I can agree cause the typedef makes only sense if there is any C issue to consider (even if the only issue is that the developer is a C programmer).

But it makes no sense to use C syntax in C++ modules if we want to make C++ and nothing else. Then, you should throw all shortcomings of C away (including unnecessary typedefs) and develop classes, classes and classes.
0

Commented:
There's a reason that there's a C in C++ ;) And I wouldn't call a typedef like that a shortcoming.

But I guess that ultimately you're right when you speak about C++ "philosophy".
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.