Function returning 2d array?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 9526 Views - Last Post: 13 May 2011 - 11:27 AM Rate Topic: -----

#1 sidebrok  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 61
  • Joined: 28-October 10

Function returning 2d array?

Posted 13 May 2011 - 07:36 AM

Hey guys! I am making my own custom library for C and i have tried to create a function wich sorts out all the words in a string, but i that function would have to return a 2d arrays of words. So i was wondering if someone know how i can do this? or is it possible at all?
Is This A Good Question/Topic? 0
  • +

Replies To: Function returning 2d array?

#2 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 251
  • Joined: 22-April 11

Re: Function returning 2d array?

Posted 13 May 2011 - 07:39 AM

I'm not sure that's possible. But you can do something like:
void myFunc(int someArgs, string[][] &arrayToModify)
{
}



Then you just modify &arrayToModify. Since it's being passed in by reference (the &), it will modify the actual array passed into it.
Was This Post Helpful? 1
  • +
  • -

#3 zbora23  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 57
  • Joined: 26-February 09

Re: Function returning 2d array?

Posted 13 May 2011 - 07:52 AM

Hey

Of course it is possible.

What is your knowledge in C?
You can return it as a pointer.

"The name of the array is a pointer to the first element of the array"
e.g.

string* function(string text){
	string* return_data_ptr;
	string  array[2][100];
	return_data_ptr = array;
	
	//Do Something With array
	
	return return_data_ptr;
	
}


Was This Post Helpful? -1
  • +
  • -

#4 sidebrok  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 61
  • Joined: 28-October 10

Re: Function returning 2d array?

Posted 13 May 2011 - 07:54 AM

thanks!:D I didnt think of thatXD

This post has been edited by sidebrok: 13 May 2011 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: Function returning 2d array?

Posted 13 May 2011 - 08:03 AM

@zbora23
You sample code is returning a pointer to storage local to the function so when the function ends that pointer is pointing to nowhere.

To return a pointer from any function you must either use static duration (not recommended), dynamically allocate the memory using malloc, or pass the array to be returned into the function. If you dynamically allocate the memory with malloc then you must insure that you delete the memory with free.

I would recommend that you pass the array into your function as a parameter. This is the cleanest, least problematic way to pass arrays to and from functions.


Jim

This post has been edited by jimblumberg: 13 May 2011 - 08:05 AM

Was This Post Helpful? 1
  • +
  • -

#6 zbora23  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 57
  • Joined: 26-February 09

Re: Function returning 2d array?

Posted 13 May 2011 - 08:11 AM

jimblumberg
You are right. My bad, I didn't realise what I typed there.

sidebrok
You would have to create dyniamic array and then play around with it

In C++ it would look like
string* function(string text){
	string* array = new string[10];
	
	array[2] = "Hello";
	array[7] = "Good";
	//Do Something
	
	return array;
	
}


Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: Function returning 2d array?

Posted 13 May 2011 - 08:20 AM

Also remember that C does not have a datatype of type string.

As I said the best way to return arrays from a function is to pass them in the parameter list.

void mfunct(char marray[100][100])
{ /* Do something here with marray */ }

void callingFunction(void)
{
   char yourArray[100][100];
   mfunct(yourArray);
   /* Now use the array */
}



Jim
Was This Post Helpful? 0
  • +
  • -

#8 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 251
  • Joined: 22-April 11

Re: Function returning 2d array?

Posted 13 May 2011 - 08:42 AM

View Postjimblumberg, on 13 May 2011 - 08:20 AM, said:

Also remember that C does not have a datatype of type string.

As I said the best way to return arrays from a function is to pass them in the parameter list.

void mfunct(char marray[100][100])
{ /* Do something here with marray */ }

void callingFunction(void)
{
   char yourArray[100][100];
   mfunct(yourArray);
   /* Now use the array */
}



Yes, but you have to pass in by reference or marray is just gonna be a copy and won't have any effect in callingFunction.

void mfunct(char &marray[100][100])

Jim

This post has been edited by RevTorA: 13 May 2011 - 08:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4025
  • View blog
  • Posts: 12,423
  • Joined: 25-December 09

Re: Function returning 2d array?

Posted 13 May 2011 - 09:05 AM

Quote

Yes, but you have to pass in by reference or marray is just gonna be a copy and won't have any effect in callingFunction.



No, this is an array, the name of the array is the address of the first element so you do not have to deference this pointer. See this link on arrays look at the section titled Arrays as parameters.


Jim
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Function returning 2d array?

Posted 13 May 2011 - 09:06 AM

In C, any dynamic 2D array is usually going to be an array of arrays. Or, rather, an array of pointers, that then point to an array. Also, arrays degrade to pointers. If you're going to work in C ( not C++ ), an intimate understanding of pointers is vital.

Here's a quick and dirty example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char **getSomeWords() {
	char **words = malloc(sizeof(char *) * 4);
	
	words[0] = malloc(10); strcpy(words[0], "Alpha");
	words[1] = malloc(10); strcpy(words[1], "Bravo");
	words[2] = malloc(10); strcpy(words[2], "Charlie");
	words[3] = NULL;
	
	return words;
}

void freeSomeWords(char **words) { /* your code here */ }

void print(char **words) {
	while(*words!=NULL) {
		printf("%s\n",*words);
		words++;
	}
}

int main(){
	char **words = getSomeWords();
	print(words);
	freeSomeWords(words);
	return 0;
}



In a perfect world, you'll only malloc strlen+1. I'll leave that as an exercise for you.

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

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Function returning 2d array?

Posted 13 May 2011 - 09:14 AM

> I am making my own custom library for C and i have tried to create a function wich sorts out all the words in a string but i that function would have to return a 2d arrays of words

You've got a few options:

1. You're already passing in an array that should be sorted. You can sort that.
2. You are dynamically allocating the array in the function. You have a pointer to return. This is going to be of type char**.
3. You wrap this array in a struct, so you can just return that struct.
4. You use the C++ standard library, so you return a type of std::vector<std::vector<std::string> > . Of course you could also pass this type in.

RevTorA said:

Yes, but you have to pass in by reference or marray is just gonna be a copy and won't have any effect in callingFunction.
That doesn't make sense. You don't care about the pointer itself. You dereference to get the array elements, so whether the pointer is a reference to the original or not doesn't matter.
Was This Post Helpful? 0
  • +
  • -

#12 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 251
  • Joined: 22-April 11

Re: Function returning 2d array?

Posted 13 May 2011 - 09:18 AM

Quote

RevTorA said:

Yes, but you have to pass in by reference or marray is just gonna be a copy and won't have any effect in callingFunction.
That doesn't make sense. You don't care about the pointer itself. You dereference to get the array elements, so whether the pointer is a reference to the original or not doesn't matter.


If you pass a variable to a function, a copy of that variable is created. Any modification to said variable won't show up in the calling function since you're working with a copy. To pass the actual variable, you pass by reference. If he's passing the POINTER to the array then fine, dereferencing said pointer will give you the variable, but his code wasn't passing a pointer, it was passing a static 100x100 array.

This post has been edited by RevTorA: 13 May 2011 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5805
  • View blog
  • Posts: 12,643
  • Joined: 16-October 07

Re: Function returning 2d array?

Posted 13 May 2011 - 09:30 AM

View PostRevTorA, on 13 May 2011 - 12:18 PM, said:

If you pass a variable to a function, a copy of that variable is created. Any modification to said variable won't show up in the calling function since you're working with a copy.


True, in all cases. A pointer is just a copy of an address...

View PostRevTorA, on 13 May 2011 - 12:18 PM, said:

To pass the actual variable, you pass by reference.


Incorrect. To change the value in a stored location, you pass a pointer to that location. The "pass by reference" idea doesn't really exist in C, in C++ it's safety mechanism. In C, you pass pointers. Pointers are just memory addresses. A C++ reference is just a stealth pointer, really.

View PostRevTorA, on 13 May 2011 - 12:18 PM, said:

If he's passing the POINTER to the array then fine, dereferencing said pointer will give you the variable, but his code wasn't passing a pointer, it was passing a static 100x100 array.


Um, arrays degrade to pointers in C. You can't actually pass the value of an array. This char marray[100][100] will be passed as char *marray[100]... Actually, there's a little more going on behind the scenes. However, be aware that you can always change the contents of a passed array in C; unless it references static memory.
Was This Post Helpful? 0
  • +
  • -

#14 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Function returning 2d array?

Posted 13 May 2011 - 09:31 AM

Quote

If he's passing the POINTER to the array then fine, dereferencing said pointer will give you the variable, but his code wasn't passing a pointer, it was passing a static 100x100 array.


You understand the signature void mfunct(char marray[100][100]) is equivalent to void mfunct(char (*marray)[100]) right?

Also, how is that array static?
Was This Post Helpful? 0
  • +
  • -

#15 RevTorA  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 76
  • View blog
  • Posts: 251
  • Joined: 22-April 11

Re: Function returning 2d array?

Posted 13 May 2011 - 09:35 AM

I'll shut up then, I'm not a C programmer. I failed to realize there were that many differences between C++ and C.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2