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*)(arr ay)+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 10 9160 no****@nospam.c om wrote: 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);
The standard way of doing it is like this:
int func(double* array){
array[3]=1.0;
...
}
double array[100];
n=func(array);
The last line is equivalent to this:
n=func(&array[0]);
The reason is that the name of an array is a synonym for the location of
the initial element.
Espen
--
Numbers are the free creation of the human mind.
Julius Wilhelm Richard Dedekind
> Now I want to pass a pointer to an array of doubles, the size of the array must not be fixed though:
Try this:
int func(double *array, int num_elements)
{
int i;
for(i = 0; i < num_elements; i++)
{
array[i] = 1.0;
}
}
int main()
{
double array[100];
func(array, 100);
}
The single pointer will point to the first element of the array. You do
not need double indirection. You need to explicitly pass the size of
the array, as C does not keep that information store.
Jon
----
Learn to program using Linux assembly language http://www.cafeshops.com/bartlettpublish.8640017 no****@nospam.c om wrote: 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*)(arr ay)+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
int func(double *array, size_t arrsize)
{
int index = 3;
if (index < arrsize)
{
array[index]=1.0;
}
...
}
....
double array[100];
int n = func(array, sizeof array);
....
Remember that in C, the subscripting operation a[i] is *defined* as
*(a+i). Therefore, in *most* expression contexts (sizeof() being one
of two exceptions IINM), the type of a is converted from array of T to
pointer to T, and its value is set to the address of the first element
in the array (&a[0]), so the expression *(a+i) yields the correct
result. Because of this conversion, when you pass an array as a
parameter to a function, what you wind up passing is a pointer to the
base type, and its value is the address of the first element (this is
why I didn't use the address operator & in the function call above).
If you need to know the size of the array in the function, you need to
specify the array size as a separate parameter; a pointer doesn't know
the size of the chunk of memory it's pointing to.
If you wished to pass a pointer to the array object (not just a pointer
to the first element), the code would look like this:
int func(double (*array)[100]) // size must match original
{
int index=3;
(*array)[index] = 1.0;
...
}
....
double array[100];
n = func(&array);
This time, we are passing a pointer to a 100-element array of double.
Since 100-element array of double is a distinct type, this approach
won't work for arrays of different sizes. no****@nospam.c om wrote: 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*)(arr ay)+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
To pass a pointer to the array, not to the first element do:
int func(double **array) { or func(double **array, unsigned size) }
then you can code:
double array[100];
n = func(&array); { or n = func(&array, 100); }
But why do you require a pointer to the array, and not pointer to first
element??
For a detailed discussion on passing arrays to funtions in C, I
recommend you refer to Expert C Programming by Peter van der Linden
which has an entire chapter discussing arrays & their relationships
with pointers. This will certainly clarify any issues you may have.
Aaron Gage wrote: no****@nospam.c om wrote: Hello!
I can pass a "pointer to a double" to a function that accepts double*, like this:
[snip] To pass a pointer to the array, not to the first element do:
int func(double **array) { or func(double **array, unsigned size) }
then you can code: double array[100]; n = func(&array); { or n = func(&array, 100); }
This is not correct. The type of &array is "pointer to 100-element
array of double", *not* "pointer to pointer to double". The correct
prototype is
int func(double (*array)[100]);
You would pass a pointer to pointer to double if you were passing a
pointer to the first element of a 2-d array of double, like so:
double array[5][10];
n = func(array, 5, 10);
int func(double **a, size_t d0, size_t d1);
Since the type "100-element array of double" is a distinct type from
"101-element array of double", the type "pointer to 100-element array
of double" is a distinct type from "pointer to 101-element array of
double", so you don't have to pass the size as a separate parameter;
the array size is specified in the prototype:
int func(double (*array)[100])
{
size_t count = sizeof *array / sizeof (*array)[0];
...
}
This means func() can *only* be called with 100-element arrays of
double.
However, this defeats the OP's purpose, which is to be able to deal
with arrays of any size; this is why he needs to pass a pointer to the
first element and the array size as parameters. jo*******@my-deja.com wrote:
[snip] double array[100]; int n = func(array, sizeof array);
Dammit, that's a bug. That last line *should* read:
int n = func(array, sizeof array / sizeof array[0]);
Back to your regularly scheduled programming...
> cat main.c
#include <stdlib.h>
#include <stdio.h>
int doubleArray_fpr intf( // total number of characters
FILE* fp, // file pointer
const
char* format, // format for elements
size_t columns, // number of columns
const
size_t n, // array size
const
double array[n] // the array to be printed
) {
columns = (0 < columns)? columns: 4;
int total = 0; // total number of characters
for (size_t j = 0; j < n; ++j) {
int characters = fprintf(fp, " ");
if (0 < characters) {
total += characters;
characters = fprintf(fp, format, array[j]);
if (0 < characters) {
total += characters;
size_t k = j + 1;
if (0 == k%columns && k < n) {
characters = fprintf(fp, "\n");
if (0 < characters) {
total += characters;
}
else {
total = characters;
break;
}
}
}
else {
total = characters;
break;
}
}
else {
total = characters;
break;
}
}
return total;
}
int main(int argc, char* argv[]) {
const
size_t n = 10;
double array[n];
for (size_t j = 0; j < n; ++j) {
array[j] = j;
}
doubleArray_fpr intf(stdout, "%g", 5, n, array);
fprintf(stdout, "\n");
return 0;
} gcc -Wall -std=c99 -pedantic -o main main.c ./main
0 1 2 3 4
5 6 7 8 9 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Nelson |
last post by:
Hi,
Can pointers be passed by reference to functions? If so, how to do it?
Or is there any other way to change the pointers in the caller by the
called function?
Thanks,
Nelson
|
by: DamonChong |
last post by:
Hi,
I am still struggling to master C++ and is trying to understand how to
achieve passing arguments using pointers. I got some questions that I
like to post to the experts here, hope you can help to clarify my
doubts. I'm using g++ version 3.3.4. I created 3 classes as below for
testing some concepts. The questions are written as comments in
Bclass.h file. Thank you for your time and tips!
------------runtime errors------------
|
by: mandatory |
last post by:
hi,
i would like to pass a pointer to a thread function to a class.
The function in my code that i would like to get the address of, and pass it
to a class function is:
DWORD ServiceExecution(LPDWORD param)
And i would like to somehow send the address to a class function, similar to
|
by: CViniciusM |
last post by:
Hello,
The output of the code below is:
number = 10
number = 0 (or other number except 15)
Why the output of the second 'number' is not 15? Why the 'number' address is
not changed?
Thanks in advance, Vinicius.
|
by: Laszlo Fust |
last post by:
Hello!
I have a problem with C#.
I have a method in an activeX:
func(long FAR* pRes, long lBufSize, long FAR* lpNeeded)
This activeX have written in VC 6.
I should call this method in a C# application, but I don't know
the type of the first parameter. I tried many ways, but I always
| |
by: brckcc |
last post by:
I have a function in C which takes a pointer to a pointer to a structure. It
then returns a linked list.
How do I call, via P/Invoke, from C# to C.
struct LinkedList
{
char *value;
struct LinkedList *next;
}
|
by: Naveen koul |
last post by:
Sir,
I am facing an certain problem , i am trying to convert a piece of code in
VisaualC# .hope some body will helpme out about this how can it be done
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class grip: public wait<grip>
{
|
by: siegfried |
last post by:
The std::lower_bound function requires a function pointer as its last argument. This is simple if you don't overload.
How do I I call std::lower_bound with the last argument a function pointer when I need to pass a certain instance of an overloaded function?
Thanks,
Siegfried
|
by: mo |
last post by:
I have a C++.net wrapper class to a C++ library... A function takes in
a pointer in the wrapper... How do I pass that in C#??? Any help would
be appreciated, Thank you.
// functions in original c++ library
prCAPI PR_StartSDK( prVoid );
prCAPI PR_FinishSDK( prVoid );
prCAPI PR_GetDeviceList( prUInt32* pBufferSize, prDeviceList*
pDeviceList );
|
by: HSeganfredo |
last post by:
Folks,
I want to write a init string function that mallocs an area, fills it
with a char, sticks a NUL char in the last position and returns it to
the user.
So far I noticed that my implementation always works over a NEW memory
area, and not over the ORIGINAL pointer area, so the initial string
area is untouched, the pointer is not update to refer to the new
initiated area...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |