Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

 Dev Shed Forums Sponsor:
#1
April 16th, 2008, 02:54 PM
 scotland87
Choose Life...

Join Date: Oct 2006
Location: Scotland
Posts: 461
Time spent in forums: 4 Days 15 h 7 m 26 sec
Reputation Power: 511
Returning an Array?

Well i want to return an array from my function. I understand that you cant actually return an array, but a pointer to an array after assigning memory or something.

Im confused however and any time i try to modify my already written code to allow me return a pointer to an array i get errors relating to the fact "that you cant use arithmetic on a pointer".

Heres my function as it is and running.

Code:
```
Node* current= list.getFirst();
Node* nodeArray[63];           //creates Hash Table of 63 elements

for(int i=0;i<63;i++)  //set each array value to null, allows to check for
{                            //empty spaces. Helps linear probing.
nodeArray[i]= 0;
}

int noOfNodes= list.countNodes();

for(int i=0; i<noOfNodes; i++)
{
int index= ((current->gridReference %1000)%63); //folding &
//modular arithmetic
if(nodeArray[index]==0)
{
nodeArray[index]=current;
}
else
{
while(nodeArray[index]!=0)   //heres where linear probing occurs.
{
index++;
index= index%63;  //used to make the array circular
//no array out of bounds errors.
}
nodeArray[index]=current;
}
}

for(int j=0;j<63;j++)  //prints the hash table
{
if(nodeArray[j]!=0)
{
cout<<"Height: "<<nodeArray[j]->height<<endl;
cout<<"Distance from Home: "<<nodeArray[j]->distanceFromHome<<endl;
cout<<"Climbed: "<<nodeArray[j]->climbed<<endl;
cout<<"Grid Ref: "<<nodeArray[j]->gridReference<<endl;
cout<<"-------------------"<<endl;
cout<<endl;
}
}

}
```

As mentioned i would like to return nodeArray, as i need to use it in another function to allow users to search for a specific Node within the hash table.

Cheers

#2
April 16th, 2008, 03:13 PM
 Ramihg

Join Date: Jan 2004
Posts: 182
Time spent in forums: 3 Days 10 h 45 m 58 sec
Reputation Power: 73
In C++ there's a very thin line between array and pointer.
Basically a Type* [x] variable can be treated as a Type** variable.
Here's an example to show what I mean ( I'm too tired to explain )
Code:
```class Foo {
public:
int value;
};

Foo** test() {
// Totally forgot about that
Foo* bar = new Foo[50];

Foo* first = new Foo();
first->value = 1337;
bar[0] = first;

return bar;
}

int main(){
Foo** foobar = test();

printf("It's %i\n", foobar[0]->value );
return 0;
}```

A Type** is just a pointer, to an array of pointers, which is exactly what you want.
Note: This applies to C too.

Good luck. Please correct me if I made any mistakes.

Last edited by Ramihg : April 16th, 2008 at 05:26 PM.

#3
April 16th, 2008, 03:15 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,136
Time spent in forums: 2 Months 2 Weeks 3 Days 21 h 7 m 7 sec
Reputation Power: 1974
Big problem: nodeArray is a local variable. When you return from the method, it will no longer exist. You see, local variables are allocated on the stack at the start of a function/method call and are removed from the stack when you exit that function/method.

As much as the academics hate the idea, you could make it a global. Or you could make it static, in which case you could return a reference or pointer to it. Or you could have declared it outside of the method and pass it in, in which case the method would only modify it.

#4
April 16th, 2008, 03:26 PM
 jwdonahue
Bellevue WA, USA

Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
Or you could allocate the array on the heap.
__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon!
http://www.odonahue.com/

#5
April 16th, 2008, 04:02 PM
 scotland87
Choose Life...

Join Date: Oct 2006
Location: Scotland
Posts: 461
Time spent in forums: 4 Days 15 h 7 m 26 sec
Reputation Power: 511
Quote:
 Originally Posted by jwdonahue Or you could allocate the array on the heap.

How do you allocate the array on the heap??

#6
April 16th, 2008, 04:08 PM
 jwdonahue
Bellevue WA, USA

Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
Use the new operator.

Code:
```Node* pNodes = new Node[NUM_NODES] ;
// do whatever you must to contents of pNodes, then delete it when you're done.
delete[] pNodes ;```

#7
April 16th, 2008, 05:02 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,136
Time spent in forums: 2 Months 2 Weeks 3 Days 21 h 7 m 7 sec
Reputation Power: 1974
NOTE:
If you do new that array and return it to the caller, then it's up to the caller to delete it.

Vocabulary Term of the Day: memory leak
Happens when you allocate memory from the heap and forget to put it back.
Most common cause, besides just plain forgetting to delete it when you're done with it, is to "drop" it by reusing the pointer without deleting first or by letting the pointer fall out of scope without having deleted first.
Effect: eats away at the heap until the program reaches the point where new's start to fail, resulting in the program crashing. Normally observed when the program runs for a long time with no problem and then "suddenly crashes for no reason".
Propects of finding the memory leak: Virtually nil. Memory leaks are notoriously insidious and difficult to track down.

#8
April 16th, 2008, 08:12 PM
 sizablegrin

Join Date: Jun 2005
Posts: 5,964
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 47 m 19 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 4850
I'm going to suggest that you search this forum for the term, "help vampire". If the foo ****s, wear it.
__________________
Write no code whose complexity leaves you wondering what the hell you did.
Politically Incorrect DaWei on Pointers Grumpy on Exceptions

#9
April 17th, 2008, 11:19 AM
 scotland87
Choose Life...

Join Date: Oct 2006
Location: Scotland
Posts: 461
Time spent in forums: 4 Days 15 h 7 m 26 sec
Reputation Power: 511
Ok when i do this my program then throws the following error;

Code:
```267 C:\Documents and Settings\Scott\Desktop\Objects and Algorthms Project\LinkedList.cpp no match for 'operator=' in '*((+(((unsigned int)i) * 24u)) + nodeArray) = 0'
```

Its then highlighting the first for loop, where i initialise all elements to 0. Because its nodes that the nodeArray array is now populised by, i though mibi it would require each element to be "nullified" however that didnt work either.

P.S. Didnt understand your last post sizablegrin...vampires? Apologies to my ignorance of the C++ programming language btw.

#10
April 17th, 2008, 01:05 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,136
Time spent in forums: 2 Months 2 Weeks 3 Days 21 h 7 m 7 sec
Reputation Power: 1974
Quote:
 Ok when i do this my program then throws the following error;

When you do what?

Are you talking about:
*((+(((unsigned int)i) * 24u)) + nodeArray) = 0;
?

What are you trying to do there? Superficially, it looks like you're trying to emulate array indexing with pointer arithmetic, but then why that 24u (a pointer would only be 4 bytes long on a Win32 system)? I assume that the plus sign was thrown in simply to generate further confusion, since I don't think that it should have any effect.

If you are indeed trying to index the i'th element in the array nodeArray and set it to NULL, then why not simply write:
nodeArray[i] = NULL;
?

If that is not what you are trying to do, then what is it?

BTW, the error itself says that the compiler couldn't figure out the datatype of the lvalue so it didn't know what assignment operation to use (the lvalue of an assignment statement is the expression that's on the left side of the assignment operator, =).

Last edited by dwise1_aol : April 17th, 2008 at 01:14 PM.

#11
April 17th, 2008, 01:29 PM
 rsk00
Registered User

Join Date: Apr 2008
Posts: 1
Time spent in forums: 1 h 11 m 20 sec
Reputation Power: 0
Quote:
 Originally Posted by P to the H Submission Details: This coursework must be your own work and should be handed in to me (in room ) on or before Tuesday 29th April 2008.

....

#12
April 17th, 2008, 01:46 PM
 scotland87
Choose Life...

Join Date: Oct 2006
Location: Scotland
Posts: 461
Time spent in forums: 4 Days 15 h 7 m 26 sec
Reputation Power: 511
Quote:
 Originally Posted by rsk00 ....

Lol...

Well it is, i simply require some further understanding and help, since *Remove Lecturers Name* is absolutely murder at explaining anything!

I also feel that the current state of our country's education system is absolutely atrocious and without forums such as this or other web based help i would not have managed to get through college and university.

I aint asking for the answer, i just need help to further understand.

P.S. Yeah all im trying to do is set every element in the array to NULL.

However wen i do the for loop and attempt this it throws the previous error i just showed you.

Last edited by scotland87 : April 17th, 2008 at 01:51 PM.

#13
April 17th, 2008, 02:30 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,136
Time spent in forums: 2 Months 2 Weeks 3 Days 21 h 7 m 7 sec
Reputation Power: 1974
Quote:
 Originally Posted by scotland87 P.S. Yeah all im trying to do is set every element in the array to NULL. However wen i do the for loop and attempt this it throws the previous error i just showed you.

So why the attempt at pointer arithmetic instead of just simply indexing into the array? Has your code changed since the original posting? [ insert standard comment that we are not mind-readers ]

#14
April 18th, 2008, 06:22 AM
 terminator5002
Registered User

Join Date: Feb 2006
Posts: 5
Time spent in forums: 1 h 19 m 50 sec
Reputation Power: 0
please stop answering people with questions of linked list this is affecting other people doing a university coursework. it is unfair for other students in bsc computing.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Returning an Array?

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |