Passing Pointer Array Structure

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 - 09:04 AM

[quote name='Ancient Dragon' date='1 Feb, 2010 - 08:00 AM' post='911652']

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

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.


I saw the post, this is my definition prototype

void getInput(contact *, ifstream);

Now you are saying I should use

void getInput(contact *myContacts[], ifstream);

Right?

But what if I do not wish to put a variable name there?

View PostNickDMax, on 1 Feb, 2010 - 08:01 AM, said:

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...


Ok wil be sure to pass fin by reference :D
Was This Post Helpful? 0
  • +
  • -

#17 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 - 09:08 AM

You are accessing members of the structure as myContacts[i]->firstName;. You use -> operator when the memory of members is dynamically allocated. As you have all static allocations (arrays) inside your structure I think you should access them as myContacts[i].firstName;

View PostAncient Dragon, on 1 Feb, 2010 - 08:28 PM, said:

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.


Please check it again... I said contact* myContact; or contact myContact[]; (missing star?). Which are, indeed same.

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

Was This Post Helpful? 0
  • +
  • -

#18 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 - 09:14 AM

Ok I think I am going to cry lolz.

I am really confused guys :(. I mean let me rephrase

According to this code:

#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 *myContacts[], 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;
	}
}




My declarion of the array of 5 pointers to the structure is:
contact *myContacts[5];

Acording to this declaration waht should be my function prototype and function heading be, in order for a new function to use it?
Was This Post Helpful? 0
  • +
  • -

#19 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 - 09:22 AM

Hmmm... you have array of pointers of structure contacts. And in that case it's correct. The mistake I can see in your prototype right now is
void getInput(contact *myContacts[], ifstream &fin).

That missing & for fin.
Was This Post Helpful? 0
  • +
  • -

#20 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 - 09:26 AM

YAY finally worked:

#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 *myContacts[], 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 one last question

I am passing the array of pointers is that correct. I wish to make sure I am using pointers so that I am not really allocating a bunch of memory on mere declaration
Was This Post Helpful? 0
  • +
  • -

#21 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 - 09:29 AM

Yes, you are using array of pointers to a structure.

But the problem is that you are not allocating memory to them at all! :)
Was This Post Helpful? 0
  • +
  • -

#22 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 - 09:30 AM

View PostAmitTheInfinity, on 1 Feb, 2010 - 08:29 AM, said:

Yes, you are using array of pointers to a structure.

But the problem is that you are not allocating memory to them at all! :)


Is that a problem or a good thing?
Was This Post Helpful? 0
  • +
  • -

#23 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 09:40 AM

View Postkhizaraq, on 1 Feb, 2010 - 03:38 PM, said:

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.
The question you need to answer yourself is why you need an array of pointers. I can't think of any good reason, from looking at your code; it seems to be more a result of confusion than design; Have you tried creating a simple array of contact objects without messing around with pointers?

e.g
void getinput(contact myContacts[], ifstream& fin);

int main()
{
    contact myContacts[5];
    ifstream fin("file.txt");

    getinput(myContacts, fin);
}

void getinput(contact myContacts[], ifstream& fin)
{
    contact temp;
    int i = 0;

    while( fin >> temp.firstName >> temp.lastName >> temp.phoneNum >> temp.emailAdd )
        myContacts[i++] = temp;
} 
Also be careful of using 'eof' in a loop (don't do it!); you have a strong chance of overflowing your loop with "ghost" data, since eof doesn't mean the same as 'no more data', it means that a failed attempt to read beyond the end of the file has already occurred.

This post has been edited by Bench: 01 February 2010 - 09:41 AM

Was This Post Helpful? 0
  • +
  • -

#24 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 - 09:43 AM

YEah I know, this is a small program merely to practice and see how it works. I know in this particular situation using it is probably of no use
Was This Post Helpful? 0
  • +
  • -

#25 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Passing Pointer Array Structure

Posted 01 February 2010 - 09:44 AM

View Postkhizaraq, on 1 Feb, 2010 - 04:30 PM, said:

View PostAmitTheInfinity, on 1 Feb, 2010 - 08:29 AM, said:

Yes, you are using array of pointers to a structure.

But the problem is that you are not allocating memory to them at all! :)


Is that a problem or a good thing?

Its a problem which is associated with storing pointers in arrays. it opens up a huge can of worms called explicit memory management when you try to deal with it. explicit memory management involves using new/delete at the appropriate time so to avoid problems with dangling pointers, and with memory leaks (And in some cases with exception handling aswell)

Remember that an array storing pointers is just an array of numbers whose value represents a memory address. If you attempt to access a memory address which doesn't belong to your program (a "dangling pointer"), then your program will crash, or do something else weird.
the new operator allocates memory to your program, and returns a number representing the address of that allocated memory; the general idea is that you store numbers in your pointer array which each represent valid memory addresses (for memory belonging to your program).

memory acquired in this way is not automatically released back to your OS (unlike normal variables which have so-called "automatic" storage); you need to tell it directly with delete when you're finished with new'ed memory. if you don't do this, but you overwrite the address data which records that memory's position, your program will still be able to grab more memory using 'new', until your computer runs out of memory, and then it will crash. This is a memory leak.

reference on new: http://www.cpprefere...ki/keywords/new

This post has been edited by Bench: 01 February 2010 - 09:52 AM

Was This Post Helpful? 0
  • +
  • -

#26 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 - 09:45 AM

Currently do I need to delete anything?

As I understood that when we use the new only then we need to delete

I have this confusion as to using this method to declare the array:

contact *myContacts;

myContacts = new contact[5];



I am not sure if this is a correct way or not

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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2