sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
PeterOut's Avatar

Passing a 2D array as a pointer to a pointer


Question posted by: PeterOut (Guest) on August 27th, 2008 11:55 PM
Say I have a function like this.

int Func(float **fppArg);

and I have a variable defined thus.

float faa2DArray[3][3];

How would I pass faa2DArray to Func()?

Many thanks in advance,
Peter.
3 Answers Posted
Ben Bacarisse's Avatar
Guest - n/a Posts
#2: Re: Passing a 2D array as a pointer to a pointer

PeterOut <MajorSetback@excite.comwrites:
Quote:
Originally Posted by
Say I have a function like this.
>
int Func(float **fppArg);
>
and I have a variable defined thus.
>
float faa2DArray[3][3];
>
How would I pass faa2DArray to Func()?


You can't, at least not directly. You can write:

float *tmp[] = { faa2DArray[0], faa2DArray[1], faa2DArray[2] };
Func(tmp);

or even:

Func((float *[]){faa2DArray[0], faa2DArray[1], faa2DArray[2]});

if you don't mind straying into C99. This is more universal:

float *tmp[3];
tmp[0] = faa2DArray[0];
tmp[1] = faa2DArray[1];
tmp[2] = faa2DArray[2];
Func(tmp);

However, the fact that you need these gymnastics suggests that
something has gone wrong. Can't you start with the right shape of
array in the first place, or change Func to take the array you have?

--
Ben.
Default User's Avatar
Guest - n/a Posts
#3: Re: Passing a 2D array as a pointer to a pointer

PeterOut wrote:
Quote:
Originally Posted by
Say I have a function like this.
>
int Func(float **fppArg);
>
and I have a variable defined thus.
>
float faa2DArray[3][3];
>
How would I pass faa2DArray to Func()?


You wouldn't. You have to change the declaration of either fppArg or
faa2DArray.


One way would be:

int Func(float fppArg[][3]);



Brian
Nick Keighley's Avatar
Guest - n/a Posts
#4: Re: Passing a 2D array as a pointer to a pointer

On 27 Aug, 23:53, PeterOut <MajorSetb...@excite.comwrote:
Quote:
Originally Posted by
Say I have a function like this.
>
int Func(float **fppArg);
>
and I have a variable defined thus.
>
float faa2DArray[3][3];
>
How would I pass faa2DArray to Func()?


http://c-faq.com/

FAQ 6.18 "My compiler complained when I passed a two-dimensional
array to a function expecting a pointer to a pointer. "

then read all of section 6. Then read the rest of the FAQ.

--
Nick Keighley

"Resistance is futile. Read the C-faq."
-- James Hu (c.l.c.)
 
Not the answer you were looking for? Post your question . . .
197,038 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,038 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors