Pointers to array of structs

Hi everyone, I have an assignment where I need to use pointers to do a few things and I am a little confused on the syntax of it all. My question is how do you use a pointer to point to an array of structs.

For example


1
2
3
4
5
6
7
8
9
10
11
12
13
struct info
{
   char firstName[15];
   char lastName[15];
};

main()
{
   info person[4];


   cout << "The third letter of the second persons first name is: "; // ?????
}


how would I define a pointer to point to the third letter of first name the second person in the "person" array. I hope that wasn't to confusing.
pointer to ... the third letter of first name the second person in the "person" array

The first thing to determine is the type of the thing you want to point to. I this case you need to point to a letter, which is type ... So your pointer will be a ...* Clearly you'll have to fill in the actual type here, I can't do the whole thing for you.

Next, you need to point to letter at: person[2].firstname[3].

That should be enough for you to understand how to declare the pointer, how to assign the pointer the right value and how to do it yourself.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct info
{
   char firstName[15];
   char lastName[15];
};

main()
{

   info person[4];
   //...
   //Person array initialization here
   //...
   info x = person[1];//Second person, note that array indexes starts from 0;
   char y = x.firstName[2]//Third letter of first name.
   cout << "The third letter of the second persons first name is: " << y;
  //or you can combine three previous lines and do:
   cout << "The third letter of the second persons first name is: " << person[1].firstName[2];

   cout << "The third letter of the second persons first name is: "; // ?????
}
Last edited on
Topic archived. No new replies allowed.