473,499 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass pointer to double array as func. parameter

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
10 8631
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
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


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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7515
by: Yangang Bao | last post by:
I have a simple test program. It doesn't work. I want to know what is the reason and how to fix it. Maybe I should use template function to do it. But I don't know how. Here is the simple...
4
2228
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
110
9809
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
7
25146
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
10
9107
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
21
3253
by: vmsgman | last post by:
Here is a code sample ... int blah = ReadFile( defArray, defFileName, w, h); // Read File Contents into memory array and return for processing public int ReadFile( ref ushort nArray, string...
17
3215
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
42
5271
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
29
38657
by: Why Tea | last post by:
Suppose you have a 2-dimensional array (matrix) in main() and you want to pass it to a function to do some processing, you usually pass it as a pointer to the first element. But, from the function,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.