declaring a function that returns a pointer to 1-d array

J

junky_fellow

How can I declare a function that returns a pointer
to one dimensional array ?
 
V

Vimal Aravindashan

How can I declare a function that returns a pointer
to one dimensional array ?
Like you declare any other funtion, just remember that the scope of
local variables are restricted to within the function. So you have to
return a pointer to dynamically allocated memory, or to a static variable.

cheers,
forayer
 
K

Keith Thompson

How can I declare a function that returns a pointer
to one dimensional array ?

Do you really want a pointer to an array, or do you want a pointer to
its first element (which you can then use to access all the elements)?
 
J

junky_fellow

Keith said:
Do you really want a pointer to an array, or do you want a pointer to
its first element (which you can then use to access all the elements)?
I want a pointer to an array. (not the pointer to the first element).
 
P

pete

I want a pointer to an array. (not the pointer to the first element).

/* BEGIN new.c */

#include <stdio.h>

int (*f(void))[11];

int main(void)
{
printf("f()[0][2] is %d.\n", f()[0][2]);
putchar('\n');
return 0;
}

int (*f(void))[11]
{
static int a[11] = {1, 3, 5, 7};

return &a;
}

/* END new.c */
 
J

junky_fellow

pete said:
I want a pointer to an array. (not the pointer to the first element).

/* BEGIN new.c */

#include <stdio.h>

int (*f(void))[11];

int main(void)
{
printf("f()[0][2] is %d.\n", f()[0][2]);
putchar('\n');
return 0;
}

int (*f(void))[11]
{
static int a[11] = {1, 3, 5, 7};

return &a;
}

/* END new.c */

Thanx a lot ...
 
J

junky_fellow

Keith said:

There's no specific reason for this. I am learning C and this came to
my mind but I was not able to figure it out. Anyway thanx to pete
for the answer.

I also want to know how can we return a pointer to first element
of a 2-d array ? what would be the declaration for such a function
(the function that returns the pointer to the first element of
a 2d array whose elements are of type int) ?
For eg. suppose I have a 2d array
int arr[3][4];
I want to return the address of first element of arr.
Can I do
return(arr);
But then what would be the decalaration of such a function (I mean the
return type ?) will it be "int *" or "int **" ?

Thanx in advance for any help ...
 
V

Vimal Aravindashan

Keith Thompson wrote:
[snip]


There's no specific reason for this. I am learning C and this came to
my mind but I was not able to figure it out. Anyway thanx to pete
for the answer.

I also want to know how can we return a pointer to first element
of a 2-d array ? what would be the declaration for such a function
(the function that returns the pointer to the first element of
a 2d array whose elements are of type int) ?
For eg. suppose I have a 2d array
int arr[3][4];
I want to return the address of first element of arr.
Can I do
return(arr);
But then what would be the decalaration of such a function (I mean the
return type ?) will it be "int *" or "int **" ?

Thanx in advance for any help ...
The return type will be "int **" but returning 'arr' when 'arr' is
declared as arr[3][4] won't do. Read the first couple of replies to your
post. And when asking new questions, please do so in a separate thread.

cheers,
forayer
 
K

Keith Thompson

I also want to know how can we return a pointer to first element
of a 2-d array ? what would be the declaration for such a function
(the function that returns the pointer to the first element of
a 2d array whose elements are of type int) ?
For eg. suppose I have a 2d array
int arr[3][4];
I want to return the address of first element of arr.
Can I do
return(arr);
But then what would be the decalaration of such a function (I mean the
return type ?) will it be "int *" or "int **" ?

What exactly do you mean by the "first element" of a 2d array?

Strictly speaking, C doesn't have 2-dimensional arrays; it has arrays
of arrays. Given
int arr[3][4];
the first element of arr is an array, not an int.

A two-dimensional array can also be implemented as an array of
pointers, or as a pointer-to-pointer, rather than as an array of
arrays. This is more flexible, since it allows you to vary the size
of the subarrays (but you have to keep track of it).

Confusingly, given:

int x[10][10];
int (*y)[10];
int **z;

and assuming memory has been allocated properly, all of the following
are valid expressions of type int:

a[0][0]
b[0][0]
c[0][0]

but they all have different meanings.

If you want to return a pointer to its first element, use

return &arr[0];

The type is pointer-to-array-of-3-int

If you want to return a pointer to the first element of the first
element, which is of type int, use:

return &arr[0][0];

Both of these can be transformed if you like, since x[y] is equivalent
to *(x+y).

But in general, pointers to arrays are rarely useful. The pointed-to
array type must be of fixed size, the syntax is confusing, and
whatever you're trying to do can usually be done more easily with a
pointer to the first element, rather than a pointer to the entire
array.

Another thing: given "int arr[3][4];", the type int** isn't going to
be useful. int** is a pointer to a pointer; the array declaration
doesn't create any pointer objects for an int** to point to.

Arrays are not pointers. Pointers are not arrays. C's syntax almost
seems designed to fool you into forgetting this.

If you haven't read section 6 of the C FAQ (Arrays and Pointer), read
it. If you have, read it again.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top