NO

Author Topic: pointer to pointer troubles  (Read 2579 times)

Alessio

  • Guest
pointer to pointer troubles
« on: January 16, 2008, 11:47:45 am »
Hi,

I've a function that returns a pointer of a bidimensional array.

Code: [Select]
int **foo(int *iSize); // iSize returns number of rows
I use it in this way...

Code: [Select]
int i = 0;
int **lpiArray = foo(&i);

...and it runs perfectly.
The code that foo function uses for allocate memory for the array is this:

Code: [Select]
int **lpiResult = malloc(10*sizeof(int*));
if ( NULL == lpiResult )
{
return NULL;
}

for ( i = 0; i < 10; i++ )
{
lpiResult[i] = malloc(2*sizeof(int));
if ( NULL == lpiResult[i] )
{
free(lpiResult);
return NULL;
}
}

My question is, why I can't pass a pointer for the bidimensional array instead of get it from function return ?
Something like:

Code: [Select]
int foo(int **lpiArray); // function returns number of rows
and used in this way:

Code: [Select]
int **lpiResult = NULL;
int iRows = foo(lpiResult);

Thanks!

Offline DMac

  • Member
  • *
  • Posts: 258
Re: pointer to pointer troubles
« Reply #1 on: January 16, 2008, 04:39:22 pm »
Good News, you can.

Here is an example to Illustrate how it is done.

Code: [Select]

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/****************************************************************************
 *                                                                          *
 * Function: Split (Perl's "split" function for C)                          *
 *                                                                          *
 * Purpose: Given a string and delimiter of any length returns an array of  *
 * strings which are tokens that are separated by the delimiter in the      *
 * original string. The original string is not touched. This emulates the   *
 * functionality of Perl's "split" function (minus the use of regular       *
 * expressions).                                                            *
 *                                                                          *
 ****************************************************************************/

char** Split(char *Input, char *Delim, char ***List, int *TokenCount)
{
int Found, Length, DelimLen = strlen(Delim);
char *Remain, *Position;

if ((List == NULL) || (Input == NULL) || (Delim == NULL))
{
*TokenCount=-1;
return NULL;
}

//first pass -- count number of delimiters
for (Found = 1, Remain = Input;
(Position = strstr(Remain, Delim));
Found++) Remain = Position + DelimLen;

//create array based on number of delimiters
*List = (char **)malloc((Found+1) * sizeof(char *));

//second pass -- populate array
for (Found = 0,Remain = Input;
(Position = strstr(Remain, Delim),1);
Remain = Position + DelimLen)
{
Length = Position? Position - Remain : strlen(Remain);
(*List)[Found] = (char *)malloc(sizeof(char)*(Length+1));
strncpy((*List)[Found], Remain, Length);
(*List)[Found++][Length] = 0;
if(!Position){(*List)[Found] = Position; break;}
}

*TokenCount = Found;

return *List;
} /* Split() */


/* Destroys the array of strings structure returned by Split() */
void FreeSplitList(char **List)
{
int Count = 0;
while(List[Count]) free(List[Count++]);
free(List);

} /* FreeSplitList() */

/****************************************************************************
 *                                                                          *
 * Function: main                                                           *
 *                                                                          *
 * Purpose : Main entry point.                                              *
 *                                                                          *
 * History : Date      Reason                                               *
 *           00/00/00  Created                                              *
 *                                                                          *
 ****************************************************************************/

int main(int argc, char *argv[])
{
char *str = "The Quick Brown Fox Jumpped Over The Lazy Dogs";
char **strList;
int iCount;

Split(str," ",&strList,&iCount);

for(int i=0; i<iCount;i++)
{
printf("%s \n", strList[i]);
}

FreeSplitList(strList);

    return 0;
}

No one cares how much you know,
until they know how much you care.

Alessio

  • Guest
Re: pointer to pointer troubles
« Reply #2 on: January 16, 2008, 05:54:54 pm »
Thank you, but this applies to one-dimensional arrays.
I would to understand how it can works with bidimensional arrays.

Offline DMac

  • Member
  • *
  • Posts: 258
Re: pointer to pointer troubles
« Reply #3 on: January 16, 2008, 06:41:52 pm »
In the example the array of Tokens is in fact an array of character arrays.  You could apply this method to an array of Int arrays or whatever.
No one cares how much you know,
until they know how much you care.

Alessio

  • Guest
Re: pointer to pointer troubles
« Reply #4 on: January 16, 2008, 09:05:50 pm »
Quote
In the example the array of Tokens is in fact an array of character arrays.  You could apply this method to an array of Int arrays or whatever.

Holy crap! What a shame!

Thanks :D!