Help with Pointers to Array of Struct in C++ - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help with Pointers to Array of Struct in C++

DrewBDrewB Posts: 1Member
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.

[code]
#include
#include
#include
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;
}
}
[/code]

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:

[code]
cout << *(carsPtr+1).carMake;
[/code]

I get the following errors:
H:COMS-171 WinterProjectsFE1.cpp In function `int main()':
27 H:COMS-171 WinterProjectsFE1.cpp `carMake' has not been declared
27 H:COMS-171 WinterProjectsFE1.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.

Comments

  • pseudocoderpseudocoder Posts: 688Member ✭✭✭
    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.


  • anthonyzzanthonyzz Posts: 1Member
    [link=http://www.keepautomation.com/how_to/visual_cplusplus/]C++[/link] is a programming language that is general purpose, statically typed, free-form, multi-paradigm and compiled. It is regarded as an intermediate-level language, as it comprises both high-level and low-level language features.[3] Developed by Bjarne Stroustrup starting in 1979 at Bell Labs, C++ was originally named C with Classes, adding object oriented features, such as classes, and other enhancements to the C programming language. The language was renamed C++ in 1983,[4] as a pun involving the increment operator. C++ is one of the most popular programming languages[5][6] and is implemented on a wide variety of hardware and operating system platforms. As an efficient compiler to native code, its application domains include systems software, application software, device drivers, embedded software, high-performance server and client applications, and entertainment software such as video games.[7] Several groups provide both free and proprietary C++ compiler software, including the GNU Project, LLVM, Microsoft and Intel. C++ has greatly influenced many other popular programming languages, most notably C#[2] and Java. Of course, follow the guide, you can find more information about c++,
    hope my reply is helpful, good luck.
Sign In or Register to comment.