Connecting Tech Pros Worldwide Help | Site Map

Passing a 2D array as a pointer to a pointer

  #1  
Old August 27th, 2008, 11:55 PM
PeterOut
Guest
 
Posts: n/a
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.
  #2  
Old August 28th, 2008, 12:45 AM
Ben Bacarisse
Guest
 
Posts: n/a

re: Passing a 2D array as a pointer to a pointer


PeterOut <MajorSetback@excite.comwrites:
Quote:
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.
  #3  
Old August 28th, 2008, 12:45 AM
Default User
Guest
 
Posts: n/a

re: Passing a 2D array as a pointer to a pointer


PeterOut wrote:
Quote:
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
  #4  
Old August 28th, 2008, 09:45 AM
Nick Keighley
Guest
 
Posts: n/a

re: Passing a 2D array as a pointer to a pointer


On 27 Aug, 23:53, PeterOut <MajorSetb...@excite.comwrote:
Quote:
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.)
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i get each coloumn of a 2D array as 1D arrays?? Renjini answers 2 January 28th, 2006 10:25 PM
How to pass pointer to pointer to c function from c# Pravin answers 0 November 15th, 2005 08:09 AM
passing 2d array and realloc Newsgroup Posting ID answers 2 November 13th, 2005 07:05 PM
passing a 2d array as a paramater laclac01@yahoo.com answers 18 September 1st, 2005 12:45 PM