LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-23-2005, 11:48 AM   #1
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272

Rep: Reputation: 30
pass pointer,, return pointer???


Hey all,
I am tryin to cut down the size of my program by creating a new class to perform certain operations.
I would like to declare an array in my main, pass the array to EACH function in the new class, and have some value be inserted into the same array and be saved.

My main is such:
Code:
SoldItem *sold = new SoldItem[5];
int main()
{  some code }

void mainFunction()
{
  OperateSold *OS = new OperateSold();  // My helper function

  OS->amountSold(sold);
   sold[i].getAmountSold();   // will this return the same aount set?
}
THe OperateSold class would have many functions, such as:
Code:
OperateSold::OperateSold(){    }
OperateSold::~OperateSold() {   }

void OperateSold::amountSold(SoldItem *sold)
{
    char amount;
    cout<< "Enter amount: ";
    cin >> amount;
    sold[i].setAmountSold(amount);
  ....code.....


}
Will this properly insert the char amount into the sold array, and if the sold array is reference in main after the amountSOld function call, will the amount be in the same sold array, or somewhere else in memory.

DO i need to return a pointer to the array??
Or is the passing SoldItem pointer going to insert the char amount in the proper memory location for the array.

THanks very much
 
Old 07-23-2005, 11:55 AM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
You don't need to return the pointer; the way you're doing it should work.

It might be cleaner to have the sold array be a member of the OperateSold object and only access it though that class. That way you don't end up with a global array that magically changes when you call functions on the OS object (counterintuitive).
 
Old 07-23-2005, 01:28 PM   #3
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272

Original Poster
Rep: Reputation: 30
HMM interesting point,


THe only issue is, that i need to referende the sold array in other functions in the main class.

Can i have a function in OperateSold, that will return the array so i can use sold[i] in other functions???
 
Old 07-23-2005, 01:36 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
of course. Another way to do it would be a function like
Code:
SoldItem* OperateSold::getSoldItem(int i) { return &sold[i]; }
(and for people anal about effeciency, make it an inline )


Doesn't really matter though. whatever looks right to the maintainer (you!) is best
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass pointer of struct to function? jinxcat Programming 2 09-01-2005 09:29 AM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
why malloc() return different pointer value? hddd Programming 1 05-23-2005 03:38 AM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
pointer to pointer question in c lawkh Programming 2 01-29-2004 10:26 AM


All times are GMT -5. The time now is 08:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration