473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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!

Nov 8 '07 #1
9 1367
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.

Nov 8 '07 #2
On Nov 8, 3:50 pm, decker <giseli.ra...@gmail.comwrote:
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

.....

Nov 8 '07 #3
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!

Nov 8 '07 #4

loudking wrote:
On Nov 8, 3:50 pm, decker <giseli.ra...@gmail.comwrote:
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.

Nov 8 '07 #5
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?
Nov 8 '07 #6
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?

Nov 8 '07 #7

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 *));
....
}

Nov 8 '07 #8
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
Nov 8 '07 #9
On Thu, 08 Nov 2007 14:50:53 -0000, decker <gi**********@gmail.com>
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
Nov 10 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Jamie Burns | last post by:
Hello, I am writing a client / server application. There is 1 server, and many clients. The server processes requests from each client, and typically creates and manipulates C++ objects on their...
15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
8
by: Zheng Da | last post by:
I don't know where should I ask the question, so send the email to this group. I choose this group, because I want to write the program with c++ :) I want to write a program which support...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
17
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything...
12
by: subramanian | last post by:
I have taken the following prototype from K & R. void *realloc(void *p, size_t size); Suppose p was earlier allocated by malloc. Suppose I am calling realloc with larger size value. If...
17
by: Summercool | last post by:
I wonder which language allows you to change an argument's value? like: foo(&a) { a = 3 } n = 1 print n
7
by: ManWithNoName | last post by:
If you have a form with x input and two or more buttons, how do you control which button has preceding when the user hits the keyboard key "enter"? Currently it seems to be determined in a...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.