The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Returning an Array?
Discuss Returning an Array? in the C Programming forum on Dev Shed. Returning an Array? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 16th, 2008, 02:54 PM
|
|
Choose Life...
|
|
Join Date: Oct 2006
Location: Scotland
|
|
|
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:
void LinkedList::convertToHash(LinkedList list){
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;
current= current->link;
}
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;
current= current->link;
}
}
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
|

April 16th, 2008, 03:13 PM
|
 |
unlink /usr/bin/*
|
|
Join Date: Jan 2004
Location: Vancouver, Canada
|
|
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.
|

April 16th, 2008, 03:15 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

April 16th, 2008, 03:26 PM
|
 |
Bellevue WA, USA
|
|
Join Date: May 2004
Location: Bellevue Washington, USA
|
|
|
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/
|

April 16th, 2008, 04:02 PM
|
|
Choose Life...
|
|
Join Date: Oct 2006
Location: Scotland
|
|
Quote: | Originally Posted by jwdonahue Or you could allocate the array on the heap. |
How do you allocate the array on the heap??
|

April 16th, 2008, 04:08 PM
|
 |
Bellevue WA, USA
|
|
Join Date: May 2004
Location: Bellevue Washington, USA
|
|
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 ;
|

April 16th, 2008, 05:02 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

April 16th, 2008, 08:12 PM
|
|
|
|
I'm going to suggest that you search this forum for the term, "help vampire". If the foo ****s, wear it.
|

April 17th, 2008, 11:19 AM
|
|
Choose Life...
|
|
Join Date: Oct 2006
Location: Scotland
|
|
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.
|

April 17th, 2008, 01:05 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

April 17th, 2008, 01:29 PM
|
|
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. |
.... 
|

April 17th, 2008, 01:46 PM
|
|
Choose Life...
|
|
Join Date: Oct 2006
Location: Scotland
|
|
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.
|

April 17th, 2008, 02:30 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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 ]
|

April 18th, 2008, 06:22 AM
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|