Connecting Tech Pros Worldwide Help | Site Map

Passing a 2D array as a pointer to a pointer

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 27th, 2008, 10:55 PM
PeterOut
Guest
 
Posts: n/a
Default Passing a 2D array as a pointer to a pointer

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 27th, 2008, 11:45 PM
Ben Bacarisse
Guest
 
Posts: n/a
Default 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 27th, 2008, 11:45 PM
Default User
Guest
 
Posts: n/a
Default 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, 08:45 AM
Nick Keighley
Guest
 
Posts: n/a
Default 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.)
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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 220,989 network members.