Passing Pointer Array Structure

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 2882 Views - Last Post: 01 February 2010 - 09:45 AM Rate Topic: -----

#1 khizaraq  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Passing Pointer Array Structure

Posted 01 February 2010 - 08:38 AM

Hello guys. I got a little problem. I am a newbie to pointers, and trying out pointer array structure, not sure what to call it :D. Anyway here is my code. I am not sure how to pass the declared array of 20 pointers pointing to the struct contact.

Ok first Question:

How to pass an array of 20 pointers, pointing to a strucutre (I think that is what I have declared). Would like to know what would be written in the function prototype and the function definition header

Second Question

I see that I have declared array of 20 pointers, each pointing to the structure I created. Now is it possible I use just one pointer which points to an Structure array of say 20.

Third Question

Which way is better. Basically what I am thinking is declaring an array of 20 pointers to the contact, i am not really using up much memory but if I declare an structure array of 20 I would be declaring memory, and then if I use a pointer to point to it, it wouldn't be pretty much foo. But thats just what I am thinking :D. If someone cal correct me would be grateful and give me the syntax too. Thank You.

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

struct contact
{
	char firstName[20];
	char lastName[20];
	char phoneNum[20];
	char emailAdd[80];
};

void getInput(contact *, ifstream);

int main(void)
{
	contact *myContacts[5]; //I am not sure whether this is the correcet way
	
	ifstream fin;
	ofstream fout;

	getInput(myContacts, fin)
}

void getInput(contact *myContacts, ifstream fin)
{
	fin.open("file.txt");

	for(int i = 0; fin.eof(); i++)
	{
		myContacts[i]->firstName;
		myContacts[i]->lastName;
		myContacts[i]->phoneNum;
		myContacts[i]->emailAdd;
	}
}





Is This A Good Question/Topic? 0
  • +

Replies To: Passing Pointer Array Structure

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:43 AM

please tell me what are your errors exactly
Was This Post Helpful? 0
  • +
  • -

#3 khizaraq  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:44 AM

I think the error is merely in the passing of the array of pointers in the headings

void getInput(contact *, ifstream);

This post has been edited by khizaraq: 01 February 2010 - 08:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:48 AM

>>void getInput(contact *myContacts, ifstream fin)

should be (note that ifstream must be passed by reference): void getInput(contact *myContacts[], ifstream& fin)

Q2: Not sure what you are asking that is any different from the above

Q3: Use a <vector>, which is an array that will auto expand to find however many objects you want to put into it. You do not have to tell vector how many objects will be in the array, it will expand as necessary. You can declare it like this: std::vector<contact> myContacts, or if you still want to use pointers std::vector<contact*> myContacts;

Than you would pass it by reference to getInput() like this: void getInput(std::vector<contact>& myContacts, ifstream& fin);

This post has been edited by Ancient Dragon: 01 February 2010 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#5 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:48 AM

I think we should pass the address of the pointer to the array and not the pointer array itself
check if this works
void getInput( &contact , ifstream);

This post has been edited by pdkharkar: 01 February 2010 - 08:49 AM

Was This Post Helpful? 0
  • +
  • -

#6 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:51 AM

If you remove the asterisk in
void getInput(contact, ifstream);


and in main
contact myContacts[5];


your code will compile.

The reason for this is the fact that contact myContacts[5] is a pointer when you refer to myContacts. myContacts[0] is a structure.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#7 khizaraq  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:52 AM

How do I use a vector to declare an array of 5 of contact type strucutre

And how do I use pointers to do the same, using an array of structures
Was This Post Helpful? 0
  • +
  • -

#8 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:52 AM

I have guessed few things while looking at your code. Check my comments inside.
Plus, it would be easier to help you if you give proper description of what you are trying to do there. (comments)

 

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

struct contact
{
	char firstName[20];
	char lastName[20];
	char phoneNum[20];
	char emailAdd[80];
};

void getInput(contact *, ifstream);

int main(void)
{
	//AmitTheInfinity - You created array of 5 pointers of type contact there and not array of 5 structures, remove star.
	contact *myContacts[5]; //I am not sure whether this is the correcet way
   
	ifstream fin;
	ofstream fout;

	getInput(myContacts, fin)
}

// AmitTheInfinity - can have parameter as 'contact *myContacts' or maybe 'contact myContacts[]'
void getInput(contact *myContacts, ifstream fin)
{
	fin.open("file.txt");

	for(int i = 0; fin.eof(); i++)
	{
// AmitTheInfinity - You have just written some values here, what you want to do with them? print them or save in file?
// decide and do something with them.  This is bound to give error as you are doing nothing in those statements.
		myContacts[i]->firstName;
		myContacts[i]->lastName;
		myContacts[i]->phoneNum;
		myContacts[i]->emailAdd;
	}
}





I hope this will help you. :)

This post has been edited by AmitTheInfinity: 01 February 2010 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#9 khizaraq  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:53 AM

How do I use a vector to declare an array of 5 of contact type strucutre

And how do I use pointers to do the same, using an array of structures
Was This Post Helpful? 0
  • +
  • -

#10 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:56 AM

View Postkhizaraq, on 1 Feb, 2010 - 07:53 AM, said:

How do I use a vector to declare an array of 5 of contact type strucutre

And how do I use pointers to do the same, using an array of structures



See my previous post -- You probably posted this before I was finished editing it. I added the info you are looking for.
Was This Post Helpful? 0
  • +
  • -

#11 khizaraq  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:57 AM

View PostAmitTheInfinity, on 1 Feb, 2010 - 07:52 AM, said:

I have guessed few things while looking at your code. Check my comments inside.
Plus, it would be easier to help you if you give proper description of what you are trying to do there. (comments)

 

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

struct contact
{
	char firstName[20];
	char lastName[20];
	char phoneNum[20];
	char emailAdd[80];
};

void getInput(contact *, ifstream);

int main(void)
{
	//AmitTheInfinity - You created array of 5 pointers of type contact there and not array of 5 structures, remove star.
	contact *myContacts[5]; //I am not sure whether this is the correcet way
   
	ifstream fin;
	ofstream fout;

	getInput(myContacts, fin)
}

// AmitTheInfinity - can have parameter as 'contact *myContacts' or maybe 'contact myContacts[]'
void getInput(contact *myContacts, ifstream fin)
{
	fin.open("file.txt");

	for(int i = 0; fin.eof(); i++)
	{
// AmitTheInfinity - You have just written some values here, what you want to do with them? print them or save in file?
// decide and do something with them.  This is bound to give error as you are doing nothing in those statements.
		myContacts[i]->firstName;
		myContacts[i]->lastName;
		myContacts[i]->phoneNum;
		myContacts[i]->emailAdd;
	}
}





I hope this will help you. :)


Well about the thing you said, about my not doing anything with the values I forgot it was suppose to be fin>>
:)
And still is not working

View PostAncient Dragon, on 1 Feb, 2010 - 07:56 AM, said:

View Postkhizaraq, on 1 Feb, 2010 - 07:53 AM, said:

How do I use a vector to declare an array of 5 of contact type strucutre

And how do I use pointers to do the same, using an array of structures



See my previous post -- You probably posted this before I was finished editing it. I added the info you are looking for.


Oh yeah just saw your Edit

But thats ok.

But with my current code how to rectify it.

Function Prototype

Function Definition

How to change them to work accorindg to my code?
Was This Post Helpful? 0
  • +
  • -

#12 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:58 AM

View PostAmitTheInfinity, on 1 Feb, 2010 - 07:52 AM, said:

I hope this will help you. :)


Sorry, but that does not help either. contact* myContact; is NOT the same as contact* myContact[] The first is a pointer to an array of contact structures, while the second is a pointer to an array of pointers to contact structures.
Was This Post Helpful? 0
  • +
  • -

#13 khizaraq  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 08:59 AM

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

struct contact
{
	char firstName[20];
	char lastName[20];
	char phoneNum[20];
	char emailAdd[80];
};

void getInput(contact *, ifstream);

int main(void)
{
	contact *myContacts[5];
	
	ifstream fin;
	ofstream fout;

	getInput(myContacts, fin);
}

void getInput(contact *myContacts[], ifstream fin)
{
	fin.open("file.txt");

	for(int i = 0; fin.eof(); i++)
	{
		fin>>myContacts[i]->firstName;
		fin>>myContacts[i]->lastName;
		fin>>myContacts[i]->phoneNum;
		fin>>myContacts[i]->emailAdd;
	}
}



OK now with this code there are two errors

error C2664: 'getInput' : cannot convert parameter 1 from 'contact *[5]' to 'contact *'

ast, C-style cast or function-style cast

This post has been edited by khizaraq: 01 February 2010 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#14 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 09:00 AM

[quote name='khizaraq' date='1 Feb, 2010 - 07:57 AM' post='911641']

View PostAmitTheInfinity, on 1 Feb, 2010 - 07:52 AM, said:

How to change them to work accorindg to my code?


Read again, I gave you that too.
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 09:01 AM

just a passing note:

void getInput(contact *myContacts, ifstream fin)

the first argument should probably be
contact &myContacts in C++ - try to avoid using pointers if you can use a reference.

Secondly ifstream should be passed by reference. these objects (the i/o streams) do not copy well (I have crashed many a program over trying to pass cin/cout by value -- and it takes FOREVER to figure out what is wrong).

so that function really should be declared as:
void getInput(contact &myContacts, ifstream &fin)


Edit well -- at least pass fin by reference...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2