Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:50 AM
nospam@nospam.com
Guest
 
Posts: n/a
Default How to pass a pointer to an unknown-size array?


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, 04:50 AM
Victor Bazarov
Guest
 
Posts: n/a
Default 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, 04:51 AM
E. Robert Tisdale
Guest
 
Posts: n/a
Default 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;
}
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.