By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 992 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Pass pointer to double array as func. parameter

P: n/a
I'm having trouble figuring out how to pass a
pointer to a double array (1 dimensional)
to a C function.

Declaring array as: double xx[100];

Declaring func. int process( double *input[] )

Calling func. as one of the following:

process ( xx );
process ( &xx[0] );

I get various "can't convert, can't recast"
error messages from the compiler.

I have also tried declaring a double pointer and
pointing address of index zero of the array to the
double pointer - no luck either.

Using Visual Studio/Visual C 6.0

Many thanks,
Robert
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Robert Palma wrote:
I'm having trouble figuring out how to pass a
pointer to a double array (1 dimensional)
to a C function.

Declaring array as: double xx[100];
Type of xx: array of 100 double
decays into pointer to double if passed to a function
Declaring func. int process( double *input[] )
Type of input: pointer to pointer to double
Calling func. as one of the following:

process ( xx );
You are passing a pointer to double instead of a pointer
to pointer to double.
I.e. you want
process(&xx);
process ( &xx[0] );
Same as above.
I get various "can't convert, can't recast"
error messages from the compiler.
Luckily -- everything else would be an error.
I have also tried declaring a double pointer and
pointing address of index zero of the array to the
double pointer - no luck either.


This is wrong. If you want to use an additional variable,
then you need one of the appropriate type:

double **pparr = &xx;
process(pparr);
or
double *parr = xx;
process(&parr);

Note that passing &xx makes no sense at all if xx is an array as
you usually pass the address of something in order to change this
something but in the case of an array, you only want to change its
contents.

You probably want either
int process (double *input);
/* equiv: int process (double input[]); */
....
process(xx);
....
or
int process (double **input);
/* equiv: int process (double *input[]); */
....
double *parr = NULL;
process(&parr);
/* if everything went well, parr now is no longer NULL */
....

See also the comp.lang.c FAQ section on pointers and arrays.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2

P: n/a
Michael Mair wrote:
Robert Palma wrote:
I'm having trouble figuring out how to pass a pointer to a double
array (1 dimensional)
to a C function.

Declaring array as: double xx[100];

Type of xx: array of 100 double
decays into pointer to double if passed to a function

Declaring func. int process( double *input[] )

Type of input: pointer to pointer to double

Calling func. as one of the following:

process ( xx );

You are passing a pointer to double instead of a pointer
to pointer to double.
I.e. you want
process(&xx);


However, this will usually produce a warning, too;
i.e.

double *parr = xx;
process(&parr);

is a better way to do this.
process ( &xx[0] );

Same as above.
I get various "can't convert, can't recast"
error messages from the compiler.

Luckily -- everything else would be an error.
I have also tried declaring a double pointer and
pointing address of index zero of the array to the
double pointer - no luck either.

This is wrong. If you want to use an additional variable,
then you need one of the appropriate type:

double **pparr = &xx;
process(pparr);
or
double *parr = xx;
process(&parr);

Note that passing &xx makes no sense at all if xx is an array as
you usually pass the address of something in order to change this
something but in the case of an array, you only want to change its
contents.

You probably want either
int process (double *input);
/* equiv: int process (double input[]); */
....
process(xx);
....
or
int process (double **input);
/* equiv: int process (double *input[]); */
....
double *parr = NULL;
process(&parr);
/* if everything went well, parr now is no longer NULL */
....

See also the comp.lang.c FAQ section on pointers and arrays.
Cheers
Michael


For illustration:

int process1 (double **foo);
int process2 (double *const *bar);
int process3 (double *baz);

int main (void)
{
double xx[10] = {0.0};
double *parr, **pparr;

parr = xx;
pparr = &parr;

(void)process2(pparr);

(void)process3(xx);

parr = 0;

(void)process1(&parr);

return 0;
}

int process1 (double **foo)
{
static double qux[10] = {0.0};
*foo = qux;
return 0;
}

int process2 (double *const *bar)
{
return process3(*bar);
}

int process3 (double *baz)
{
*baz = 42.0;

return 0;
}
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3

P: n/a


Robert Palma wrote:
I'm having trouble figuring out how to pass a
pointer to a double array (1 dimensional)
to a C function.
Declaring array as: double xx[100];

Declaring func. int process( double *input[] )
While it is possible to declare a pointer to
an entire array, it is usually not neccessary.
Just pass a pointer to the first element. Using
this pointer, you have access to all elements
in the array. You can do this by making the
parameter:
double *input.

Calling func. as one of the following:

process ( xx );
This would be a proper call, since xx decays into
a pointer to the first element.
Read over Section 6(Arrays and Pointers) of the clc FAQ:
Pay heed to question 6.13 located at:
http://www.eskimo.com/~scs/C-faq/q6.13.html
I get various "can't convert, can't recast"
error messages from the compiler.

Example:

#include <stdio.h>

void InitArray( double *input, size_t elements, double value)
{
size_t i;

for(i = 0; i < elements; i++) input[i] = value;
return;
}

int main(void)
{
double xx[10];
int i;

InitArray( xx, 10, 2.11);
for(i = 0; i < 10; i++)
printf("xx[%d] = %.2f\n",i,xx[i]);
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4

P: n/a
Robert Palma wrote on 05/06/05 :
I'm having trouble figuring out how to pass a
pointer to a double array (1 dimensional)
to a C function.

Declaring array as: double xx[100];

Declaring func. int process( double *input[] )


Wrong. Make int simple.

The array :

T x[SIZE];
The function :

void f (T a[SIZE])
{
}

works for any T (type) except void.

Note that in the function prototype, SIZE being useless, it can be
removed.

void f (T a[])
{
}

or

void f (T *a)
{
}

note also that is is generally a godd idea to pass the size of the arry
as a parameter (I meant the number of elements) ...

void f (T *a, size_t nb_elem)
{
}

.... it helps control and flexibility...

--
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 14 '05 #5

P: n/a
Robert Palma wrote:
I'm having trouble figuring out how to pass a
pointer to a double array (1 dimensional)
to a C function.

Declaring array as: double xx[100];

Declaring func. int process( double *input[] )

Calling func. as one of the following:

process ( xx );
process ( &xx[0] );

I get various "can't convert, can't recast"
error messages from the compiler.

I have also tried declaring a double pointer and
pointing address of index zero of the array to the
double pointer - no luck either.

Using Visual Studio/Visual C 6.0

Many thanks,
Robert
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


/*coded by c_learner, released under GPL v2
http://www.gnu.org/copyleft/gpl.html*/

#include <stdio.h>

void process(double *b)
{
int i;
for(i=0; i<10; i++)
{
*(b+i)=i;
}
}

int main(void)
{
double a[10]={0};
process(a);
printf("%lf\n",a[3]);
return 0;
}

Nov 14 '05 #6

P: n/a
Michael Mair wrote:
Robert Palma wrote:
I'm having trouble figuring out how to pass a
pointer to a double array (1 dimensional)
to a C function.

Declaring array as: double xx[100];
Type of xx: array of 100 double
decays into pointer to double if passed to a function

Declaring func. int process( double *input[] )


Type of input: pointer to pointer to double

Calling func. as one of the following:

process ( xx );


You are passing a pointer to double instead of a pointer
to pointer to double.
I.e. you want
process(&xx);


&xx has type 'pointer to array of 100 double'. This is
NOT compatible with 'pointer to pointer to double'.
I have also tried declaring a double pointer and
pointing address of index zero of the array to the
double pointer - no luck either.


This is wrong. If you want to use an additional variable,
then you need one of the appropriate type:

double **pparr = &xx;


Same error again. Pointer to array of 100 double, cannot be
converted to pointer to pointer to double.
process(pparr);
or
double *parr = xx;
process(&parr);


This is correct and is what the OP needs to do.

Nov 14 '05 #7

P: n/a
c_learner wrote:

/*coded by c_learner, released under GPL v2
http://www.gnu.org/copyleft/gpl.html*/

#include <stdio.h>

void process(double *b)
{
int i;
for(i=0; i<10; i++)
{
*(b+i)=i;
}
}

int main(void)
{
double a[10]={0};
process(a);
printf("%lf\n",a[3]);
return 0;
}


Your code contains undefined behaviour in C89.
(I choose not to fix the error due to your restrictive
licence agreement).

Nov 14 '05 #8

P: n/a
Old Wolf wrote:
Michael Mair wrote:
Robert Palma wrote:
I'm having trouble figuring out how to pass a
pointer to a double array (1 dimensional)
to a C function.

Declaring array as: double xx[100];


Type of xx: array of 100 double
decays into pointer to double if passed to a function
Declaring func. int process( double *input[] )


Type of input: pointer to pointer to double
Calling func. as one of the following:

process ( xx );


You are passing a pointer to double instead of a pointer
to pointer to double.
I.e. you want
process(&xx);

&xx has type 'pointer to array of 100 double'. This is
NOT compatible with 'pointer to pointer to double'.

I have also tried declaring a double pointer and
pointing address of index zero of the array to the
double pointer - no luck either.


This is wrong. If you want to use an additional variable,
then you need one of the appropriate type:

double **pparr = &xx;

Same error again. Pointer to array of 100 double, cannot be
converted to pointer to pointer to double.

process(pparr);
or
double *parr = xx;
process(&parr);

This is correct and is what the OP needs to do.


Thanks for correcting me. I don't know what I thought at the time :-/
Even no excuse like fresh out of bed...
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #9

P: n/a
On Mon, 06 Jun 2005 21:20:18 -0700, Old Wolf wrote:
Michael Mair wrote:
Robert Palma wrote:
> I'm having trouble figuring out how to pass a
> pointer to a double array (1 dimensional)
> to a C function.
>
> Declaring array as: double xx[100];


Type of xx: array of 100 double
decays into pointer to double if passed to a function
>
> Declaring func. int process( double *input[] )

....
process(pparr);
or
double *parr = xx;
process(&parr);


This is correct and is what the OP needs to do.


What the OP really needs to do is fix the definition of process().
It takes a 1D array then it should be

int process( double *input )

then process(xx) is correct.

Lawrence
Nov 14 '05 #10

P: n/a
On Sun, 05 Jun 2005 12:28:34 -0500, Robert Palma <rp****@mrtel.com>
wrote:
I'm having trouble figuring out how to pass a
pointer to a double array (1 dimensional)
to a C function.

Declaring array as: double xx[100];
This is indeed an array of double.

Declaring func. int process( double *input[] )
This, on the other hand, is an array of pointers to double. And since
this is not one of the exceptions to THE RULE, it will be converted
internally by the compiler to pointer to pointer to double.

Calling func. as one of the following:

process ( xx );
process ( &xx[0] );

I get various "can't convert, can't recast"
error messages from the compiler.
Correctly so as neither is compatible with the function declaration.

I have also tried declaring a double pointer and
pointing address of index zero of the array to the
double pointer - no luck either.


Try either

double *ptr;
ptr = xx;
process(&ptr);

or

double *ptr_array[1];
ptr_array[0] = xx;
process(ptr_array);

Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.