Pass pointer to double array as func. parameter

R

Robert Palma

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
 
M

Michael Mair

Robert said:
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
 
M

Michael Mair

Michael said:
Robert said:
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;
}
 
A

Al Bowers

Robert said:
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 = 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);
return 0;
}
 
E

Emmanuel Delahaye

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
 
C

c_learner

Robert said:
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

/*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;
}
 
O

Old Wolf

Michael said:
Robert said:
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'.
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.
 
O

Old Wolf

c_learner said:
/*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).
 
M

Michael Mair

Old said:
Michael said:
Robert said:
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'.

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
 
L

Lawrence Kirby

Michael said:
Robert said:
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
 
S

schwarzb

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top