473,385 Members | 1,732 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,385 software developers and data experts.

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

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

Nov 15 '05 #1
12 1728
ju**********@yahoo.co.in wrote on 17/09/05 :
How can I declare a function that returns a pointer
to one dimensional array ?


Do your best effort nd post it. It's basic.

(hint : malloc())

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair
Nov 15 '05 #2
(supersedes <mn***********************@YOURBRAnoos.fr>)

ju**********@yahoo.co.in wrote on 17/09/05 :
How can I declare a function that returns a pointer
to one dimensional array ?


Do your best effort and post it. It's basic.

(hint : malloc())
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
Nov 15 '05 #3
ju**********@yahoo.co.in wrote:
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

--
If you would be a real seeker after truth, it is necessary that at least
once in your life you doubt, as far as possible, all things."
-- Rene Descartes
Nov 15 '05 #4
ju**********@yahoo.co.in writes:
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)?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5

Keith Thompson wrote:
ju**********@yahoo.co.in writes:
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)?

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

Nov 15 '05 #6
ju**********@yahoo.co.in wrote:

Keith Thompson wrote:
ju**********@yahoo.co.in writes:
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)?

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 */
--
pete
Nov 15 '05 #7
pete wrote on 17/09/05 :
int (*f(void))[11];


Scary !

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
Nov 15 '05 #8
ju**********@yahoo.co.in writes:
Keith Thompson wrote:
ju**********@yahoo.co.in writes:
> 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)?

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


Ok. Why?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #9

pete wrote:
ju**********@yahoo.co.in wrote:

Keith Thompson wrote:
ju**********@yahoo.co.in writes:
> 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)?

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 ...

Nov 15 '05 #10

Keith Thompson wrote:
ju**********@yahoo.co.in writes:
Keith Thompson wrote:
ju**********@yahoo.co.in writes:
> 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)?

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


Ok. Why?


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 ...

Nov 15 '05 #11
ju**********@yahoo.co.in wrote:
Keith Thompson wrote:
[snip]
Ok. Why?

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

--
If you would be a real seeker after truth, it is necessary that at least
once in your life you doubt, as far as possible, all things."
-- Rene Descartes
Nov 15 '05 #12
ju**********@yahoo.co.in writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #13

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
2
by: Chris Haynes | last post by:
Hello all, I have a structure: typedef struct UVstruct { float u, v; } uv; Inside a function (A) i declare a pointer to an instance of this structure:
8
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If...
9
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
8
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to...
5
by: shanfeng | last post by:
such as a function: f(double v, int a) could this function return both a integer and array? Thank you very much. I have confused on this for a long time~
1
by: Josuan | last post by:
It seems that a variable can be declared with parenthesis. For example: int main() { int (x); x = 5; } In this case it is useless. But is there any circumstances where parenthesis are...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.