Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Which are the best manner to return a pointer to a pointer of two-dim array?

Reply
Thread Tools

Which are the best manner to return a pointer to a pointer of two-dim array?

 
 
decker
Guest
Posts: n/a
 
      11-08-2007
People,

In the below code, I would like to know how I can return correctly the
two-dim array. Anyone can help me?

/*************/
int **ReturnIntPointer(int **array);

int main(){
int **vetor;
**ReturnIntPointer((int **) vetor);
printf("array[0][0]=%d\n", vetor[0][0])); //for test
return 0;
}

int **ReturnIntPointer(int **array) {

int i, j;
int nrows = 4;
int ncolumns = 4;
array = malloc(nrows * sizeof(int *));
for(i = 0; i < nrows; i++)
{ array[i] = malloc(ncolumns * sizeof(int)); }
for(i = 0; i < nrows; i++)
{
for(j = 0; j < ncolumns; j++)
{ array[i][j] = 1+j; }
}
return array;
}
/**********************/
Thanks!

 
Reply With Quote
 
 
 
 
Brian Szmyd
Guest
Posts: n/a
 
      11-08-2007
On Thu, 08 Nov 2007 14:50:53 +0000, decker wrote:

> People,
>
> In the below code, I would like to know how I can return correctly the
> two-dim array. Anyone can help me?
>
> /*************/
> int **ReturnIntPointer(int **array);
>
> int main(){
> int **vetor;
> **ReturnIntPointer((int **) vetor);
> printf("array[0][0]=%d\n", vetor[0][0])); //for test
> return 0;
> }
>
> int **ReturnIntPointer(int **array) {
>
> int i, j;
> int nrows = 4;
> int ncolumns = 4;
> array = malloc(nrows * sizeof(int *)); for(i = 0; i < nrows; i++)
> { array[i] = malloc(ncolumns * sizeof(int)); }
> for(i = 0; i < nrows; i++)
> {
> for(j = 0; j < ncolumns; j++)
> { array[i][j] = 1+j; }
> }
> return array;
> }
> /**********************/
> Thanks!


If you are allocating the array inside the function that fills the array,
why would you need to pass the dumb-dumb point in? It should be
sufficient to do:

/* in main() */
int** vetor = NULL;
vetor = ReturnIntPointer();

if (vetor)
{
...
}


/* in ReturnIntPointer() */
int** ret_val = (int**) malloc(nrow*sizeof(int *));
....
return ret_val


Needless to say you need to free vetor in your main(), otherwise you'll
have a leak.

 
Reply With Quote
 
 
 
 
loudking
Guest
Posts: n/a
 
      11-08-2007
On Nov 8, 3:50 pm, decker <(E-Mail Removed)> wrote:
> People,
>
> In the below code, I would like to know how I can return correctly the
> two-dim array. Anyone can help me?
>
> /*************/
> int **ReturnIntPointer(int **array);
>
> int main(){
> int **vetor;
> **ReturnIntPointer((int **) vetor);

Funny, can your code be compiled?

I never tried to put a "**" in front of a function call

.....

 
Reply With Quote
 
decker
Guest
Posts: n/a
 
      11-08-2007

> Funny, can your code be compiled?
>


Yes, the funny thing that it can be compiled. Strange... Well, I put
"**" to see how it runs, a curiosity of my part. I will remove so.

Really I forget to free memory, a basic error. Thanks for the
reminder!

 
Reply With Quote
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      11-08-2007

loudking wrote:
> On Nov 8, 3:50 pm, decker <(E-Mail Removed)> wrote:
> > People,
> >
> > In the below code, I would like to know how I can return correctly the
> > two-dim array. Anyone can help me?
> >
> > /*************/
> > int **ReturnIntPointer(int **array);
> >
> > int main(){
> > int **vetor;
> > **ReturnIntPointer((int **) vetor);

> Funny, can your code be compiled?
>
> I never tried to put a "**" in front of a function call


There's nothing inherently wrong with the **. In context, it's pretty
pointless, but in a different context it might even be a reasonable
thing to do. The function returns a pointer to a pointer to int.
Assuming that both the pointer to the int and the pointer to the
pointer to int are valid, the return value can be dereferenced twice,
resulting in the value of the int that was pointed at. This code then
completely ignores that value, which is why I described it as pretty
pointless.

 
Reply With Quote
 
decker
Guest
Posts: n/a
 
      11-08-2007

> There's nothing inherently wrong with the **. In context, it's pretty
> pointless, but in a different context it might even be a reasonable
> thing to do. The function returns a pointer to a pointer to int.
> Assuming that both the pointer to the int and the pointer to the
> pointer to int are valid, the return value can be dereferenced twice,
> resulting in the value of the int that was pointed at. This code then
> completely ignores that value, which is why I described it as pretty
> pointless.


So... you say that the return of the function are incorrect? Which
manner I can to do to the code don't ignore the values?


 
Reply With Quote
 
decker
Guest
Posts: n/a
 
      11-08-2007

> There's nothing inherently wrong with the **. In context, it's pretty
> pointless, but in a different context it might even be a reasonable
> thing to do. The function returns a pointer to a pointer to int.
> Assuming that both the pointer to the int and the pointer to the
> pointer to int are valid, the return value can be dereferenced twice,
> resulting in the value of the int that was pointed at. This code then
> completely ignores that value, which is why I described it as pretty
> pointless.


So you say that the return of the function is incorrect?



 
Reply With Quote
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      11-08-2007

decker wrote:
> > There's nothing inherently wrong with the **. In context, it's pretty
> > pointless, but in a different context it might even be a reasonable
> > thing to do. The function returns a pointer to a pointer to int.
> > Assuming that both the pointer to the int and the pointer to the
> > pointer to int are valid, the return value can be dereferenced twice,
> > resulting in the value of the int that was pointed at. This code then
> > completely ignores that value, which is why I described it as pretty
> > pointless.

>
> So... you say that the return of the function are incorrect? Which
> manner I can to do to the code don't ignore the values?


Your code completely ignores the value of the argument that is passed
in to ReturnIntPointer(), so there's no reason to pass in that
argument. Change the appropriate lines as indicated below, and the
function interface will make a lot more sense. I haven't bothered
testing your code, so I can't be sure this will fix all of it's
problems, but it will fix the function interface problem.

int **ReturnIntPointer(void);

int main(){
int **vetor = ReturnIntPointer();
....
}

int **ReturnIntPointer(void) {
...
int **array = malloc(nrows * sizeof(int *));
....
}

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      11-08-2007
decker wrote, On 08/11/07 14:50:
> People,
>
> In the below code, I would like to know how I can return correctly the
> two-dim array. Anyone can help me?
>
> /*************/
> int **ReturnIntPointer(int **array);


Decide on whether you want to return a pointer to the array or pass in a
pointer to the variable you want to point to it. Then see the
comp.lang.c FAQ at http://c-faq.com/ which will answer most of your
questions.

> int main(){
> int **vetor;
> **ReturnIntPointer((int **) vetor);


Specifically remember that C passes variables by value, to
ReturnIntPointer (which is a bad name) cannot change the value of vetor.
Also the cast is not needed and had you made a mistake would have hidden
it rather than fixing it.

> printf("array[0][0]=%d\n", vetor[0][0])); //for test
> return 0;
> }
>
> int **ReturnIntPointer(int **array) {
>
> int i, j;
> int nrows = 4;
> int ncolumns = 4;
> array = malloc(nrows * sizeof(int *));


Not the best way to use malloc in my opinion. It is easier to maintain
array = malloc(nrows * sizeof *array);

You need to check if malloc succeeds.

> for(i = 0; i < nrows; i++)
> { array[i] = malloc(ncolumns * sizeof(int)); }


Similarly here.

> for(i = 0; i < nrows; i++)
> {
> for(j = 0; j < ncolumns; j++)
> { array[i][j] = 1+j; }
> }
> return array;
> }

--
Flash Gordon
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      11-10-2007
On Thu, 08 Nov 2007 14:50:53 -0000, decker <(E-Mail Removed)>
wrote:

>People,
>
>In the below code, I would like to know how I can return correctly the
>two-dim array. Anyone can help me?
>
>/*************/
>int **ReturnIntPointer(int **array);
>
>int main(){
> int **vetor;
> **ReturnIntPointer((int **) vetor);


What was the point of the ** in front of the function name?

Since vetor is an int**, what is the point of the cast?

Since vetor was never given a value, its value is indeterminate. The
function call attempts to evaluate it which invokes undefined
behavior.

> printf("array[0][0]=%d\n", vetor[0][0])); //for test


Since C passes by value, vetor is still indeterminate and this also
invokes undefined behavior.

You could avoid both UBs with
vetor = ReturnIntPointer(NULL);
though the preferred approach (tm) would be to change the function to
not have a parameter.

> return 0;
>}
>
>int **ReturnIntPointer(int **array) {
>
> int i, j;
> int nrows = 4;
> int ncolumns = 4;
> array = malloc(nrows * sizeof(int *));
> for(i = 0; i < nrows; i++)
> { array[i] = malloc(ncolumns * sizeof(int)); }
> for(i = 0; i < nrows; i++)
> {
> for(j = 0; j < ncolumns; j++)
> { array[i][j] = 1+j; }
> }
> return array;
>}
>/**********************/
>Thanks!



Remove del for email
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT:best manner to browse a file naima.mans@gmail.com XML 6 12-21-2006 10:32 AM
How do I use the Java API in a Thread-Safe Manner? Al Koch Java 50 07-01-2005 02:12 PM
BoundColumn - DataFormatString in a Multicutural Manner cntams@gmail.com ASP .Net 0 03-31-2005 08:16 PM
running command line progs in a controlled manner kevin bailey ASP .Net 1 11-24-2004 05:10 PM
Cast object from long in safe manner? Jamie Burns C++ 31 02-01-2004 03:31 AM



Advertisments