Hi I've just started out in c++, can anyone tell me how to use pointers which hold addresses of objects ?

How do you access the object fields from this type of pointer, especially strings (array of chars)...

I'm using a Dev c++ 4.9.9.2 compiler....

Hi I've just started out in c++, can anyone tell me how to use pointers which hold addresses of objects ?

How do you access the object fields from this type of pointer, especially strings (array of chars)...

I'm using a Dev c++ 4.9.9.2 compiler....

you might want to post some code of your attempts so far...

When I try to compile this prog., it says that age and name were not declared and 'request for member of non-aggregate type before '<<' token '.....

Thanks...

Attachments
#include<iostream>
#include<conio.h>
#include<windows.h>
#include<string.h>

struct Person
 {
	char name[20];
	int age;
 };


void printData(Person *objPtr);

//***********************************

int main()
 {
	using namespace std;

	Person objMe;

	strcpy(objMe.name, "Dean Montanaro");
	objMe.age = 15;

	printData(&objMe);

	getch();
 }

//************************************

void printData(Person *objPtr)
 {
	using namespace std;

	cout<<"AGE : "<<*objPtr.age<<endl
	 <<"NAME : "<<*objPtr.name<<endl;
 }

Its the same as holding a address of a pointer to primitive data type.

Object* obj, obj1;
obj = &obj1;

An agregate type is a structure or class or union that holds (possibly) an agregate of serveral members of other types.

In you print function you do not have an agregate type you have a pointer to an aggreagate type. You can get the agregate type by dereferencing the pointer so for a structure type T

T obj;
T* pointer = &obj;  // Always initialise pointers

obj.member;  // Access a member of an agregate type

pointer.member;  // Error pointer is not an agregate type it is a pointer to an agregate type

(*pointer).member;  // OK pointer is derefernced to the agregate type 

pointer->member;   // The -> operator is a shortcut for above syntax

Edited 6 Years Ago by Banfa: Corrected tags

I closed the * and the object pointer name in parentheses and it worked!

thanks....

This question has already been answered. Start a new discussion instead.