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

D

decker

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 = malloc(ncolumns * sizeof(int)); }
for(i = 0; i < nrows; i++)
{
for(j = 0; j < ncolumns; j++)
{ array[j] = 1+j; }
}
return array;
}
/**********************/
Thanks!
 
B

Brian Szmyd

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 = malloc(ncolumns * sizeof(int)); }
for(i = 0; i < nrows; i++)
{
for(j = 0; j < ncolumns; j++)
{ array[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.
 
L

loudking

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

.....
 
D

decker

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!
 
J

jameskuyper

loudking said:
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.
 
D

decker

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?
 
D

decker

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?
 
J

jameskuyper

decker said:
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 *));
....
}
 
F

Flash Gordon

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 = malloc(ncolumns * sizeof(int)); }


Similarly here.
for(i = 0; i < nrows; i++)
{
for(j = 0; j < ncolumns; j++)
{ array[j] = 1+j; }
}
return array;
}
 
B

Barry Schwarz

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 = malloc(ncolumns * sizeof(int)); }
for(i = 0; i < nrows; i++)
{
for(j = 0; j < ncolumns; j++)
{ array[j] = 1+j; }
}
return array;
}
/**********************/
Thanks!



Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top