Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Passing a 2D array as a pointer to a pointer

Reply
Thread Tools

Passing a 2D array as a pointer to a pointer

 
 
PeterOut
Guest
Posts: n/a
 
      08-27-2008
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.
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-27-2008
PeterOut <(E-Mail Removed)> writes:

> 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.
 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      08-27-2008
PeterOut wrote:

> 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
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-28-2008
On 27 Aug, 23:53, PeterOut <(E-Mail Removed)> wrote:
> 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.)
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Passing a pointer to pointer as a multi-dimensional array entitledX C Programming 4 01-12-2006 01:22 AM
Re: Passing pointer to array from C to Python..and modifying same array in python? J W Python 0 07-14-2003 02:59 PM
Passing pointer to array from C to Python..and modifying same array in python? JW Python 1 07-12-2003 10:45 PM



Advertisments