Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help with Pointers to Array of Struct in C++ Posted by DrewB on 22 Apr 2011 at 1:15 PM
First let me start out that saying Pointers are probly my least favorite part of programming. I can get pointers to work with single variables and I think I can as well for single structures, when it comes to pointers and arrays I understand slightly but not fully.

Now when it comes to making a pointer to an Array of structures I feel dumbfounded and have been struggling over this for a few days.

I am trying to make a pointer to the array of Structures and using the pointer send the array to the display function showCarInfo().

I have looked through just about every book I can at my college library and none of them have any information on using Pointers and Structure Arrays. I know that people will not do my homework for me here and I have what i've been trying posted below, this is part of my final Exam and any help would be appreciated.

#include <cstring>
#include <iomanip>
#include <iostream>
using namespace std;

const int SIZE = 3;

struct CarInfo
{
       string carMake;
       string carModel;
       int carYear;
       string carColor;
};

void getCarInfo(CarInfo [], int);
void showCarInfo(CarInfo [], int);

int main()
{
    CarInfo cars[SIZE], *carsPtr; 
    carsPtr = cars;  
      
    getCarInfo(cars, SIZE);
    
    showCarInfo(cars, SIZE);
    
    system("PAUSE");
    return 0;
}    

void getCarInfo(CarInfo cars[], int SIZE)
{
   for ( int i = 0 ; i < SIZE ; i++ )
   {   
       cout << "Enter the Car Make: ";
       cin >> cars[i].carMake;
   
       cout << "Enter the Car Model: ";
       cin >> cars[i].carModel;

       cout << "Enter the Car Year: ";
       cin >> cars[i].carYear;

       cout << "Enter the Car Color: ";
       cin >> cars[i].carColor;
       
       cout << endl;
   }
}

void showCarInfo(CarInfo cars[], int SIZE)
{
     cout << "Car #:" << setw(10) << "Make"
                      << setw(10) << "Model"
                      << setw(10) << "Year"
                      << setw(10) << "Color"
                      << endl;
     cout << "----------------------------------------------" << endl;
     for ( int j = 0 ; j < SIZE ; j++) 
     {

         cout << "Car " << (j + 1) << ":" << setw(10) << cars[j].carMake
                                          << setw(10) << cars[j].carModel
                                          << setw(10) << cars[j].carYear
                                          << setw(10) << cars[j].carColor
                                          << endl;
     }
}


If what I understand is correct I have properly assigned carsPtr the memory address of cars[0]. However whenever I try to display anything ,after having recieved the input, using the code:

cout << *(carsPtr+1).carMake;


I get the following errors:
H:\COMS-171 Winter\Projects\FE1.cpp In function `int main()':
27 H:\COMS-171 Winter\Projects\FE1.cpp `carMake' has not been declared
27 H:\COMS-171 Winter\Projects\FE1.cpp request for member of non-aggregate type before ';' token

My book did not have more than a few sentences on Pointers to Arrays of Structures and using multiple books this is the best I have been able to come up with so far. If I can just get the pointer to work I'm fairly certain I can get it to the function with ease.

Report
Re: Help with Pointers to Array of Struct in C++ Posted by pseudocoder on 25 Apr 2011 at 7:25 PM
As I recall, you can use

cout << ( *(carsPtr + 1) ).carMake;
or
cout << (carsPtr + 1)->carMake;

dunno which is prefered though... it's been way too long since I coded anything on a regular basis.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.