Connecting Tech Pros Worldwide Help | Site Map

How to pass a pointer to an unknown-size array?

  #1  
Old July 23rd, 2005, 05:50 AM
nospam@nospam.com
Guest
 
Posts: n/a

Hello!

I can pass a "pointer to a double" to a function that accepts
double*, like this:

int func(double* var) {
*var=1.0;
...
}

double var;

n=func(&var);

---

Now I want to pass a pointer to an array of doubles, the size
of the array must not be fixed though:

int func(double[]* array) {
int index;
index=3;
array[index]=1.0;
...
}

double array[100];

n=func(&array);

with the above code the compiler gives me an error. The only
solution that I found so far is this very inelegant one:

int func(void* array) {
int index;
index=3;
*((double*)(array)+index)=1.0;
...
}

double array[100];

n=func(&array);

---

There must be a cleaner way.. but what is it?

I am interested in both C and "C++ only" solutions.

Thanks!
Mike

  #2  
Old July 23rd, 2005, 05:50 AM
Victor Bazarov
Guest
 
Posts: n/a

re: How to pass a pointer to an unknown-size array?


nospam@nospam.com wrote:[color=blue]
> I can pass a "pointer to a double" to a function that accepts
> double*, like this:
>
> int func(double* var) {
> *var=1.0;
> ...
> }
>
> double var;
>
> n=func(&var);
>
> ---
>
> Now I want to pass a pointer to an array of doubles, the size
> of the array must not be fixed though:
>
> int func(double[]* array) {
> int index;
> index=3;
> array[index]=1.0;
> ...
> }
>
> double array[100];
>
> n=func(&array);
>
> with the above code the compiler gives me an error. The only
> solution that I found so far is this very inelegant one:
>
> int func(void* array) {
> int index;
> index=3;
> *((double*)(array)+index)=1.0;
> ...
> }
>
> double array[100];
>
> n=func(&array);
>
> ---
>
> There must be a cleaner way.. but what is it?[/color]

What book are you reading that doesn't explain passing arrays to
functions?

int func(double array[], int size)
{
int index;
/* calculate index somehow */
if (index > 0 && index < size)
array[size]; /* do something with the double */
return 42;
}

V
  #3  
Old July 23rd, 2005, 05:51 AM
E. Robert Tisdale
Guest
 
Posts: n/a

re: How to pass a pointer to an unknown-size array?


> cat func.c
#include <stdlib.h>
#include <stdio.h>

int func(size_t size, const double array[]) {
size_t index;
// Calculate index somehow.
if ((0 <= index) && (index < size)) {
// Do something with the double.
fprintf(stdout, "array[%u] = %f\n", index, array[index]);
}
return 42;
}
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to qualified poitner to qualified object Szabolcs Borsanyi answers 14 June 27th, 2008 08:40 PM
Reference to an Array of some dimension arnuld answers 7 August 14th, 2007 05:05 PM
function returning array of strings shyam answers 5 November 29th, 2005 03:55 AM
How to pass by reference a dinamic array to a C routine Morgan answers 7 November 14th, 2005 03:02 AM
Passing an array of chars to a function jr answers 58 July 22nd, 2005 06:36 AM