Re: writing library functions and pointers?
- From: Barry Schwarz <schwarzb@xxxxxxxx>
- Date: Fri, 03 Jul 2009 10:05:47 -0700
On Fri, 3 Jul 2009 02:15:49 -0700 (PDT), Martin <mdekauwe@xxxxxxxxx>
wrote:
Hi,
I have been trying to construct my own library of commonly used C
functions and have got a bit stuck and wonder if anyone could give me
any pointers?
I have set up a generic read function...
void read_file_float(FILE **fp, float **array, int size, char **argv)
{
if (fread(*array, sizeof (float), size, *fp) != size) {
fprintf(stderr, "Error reading in file: %s\n", *argv);
exit(1);
}
return;
}
which I call using the following in my code for example
FILE *fp;
float *array (which I allocate);
int numrows = something;
int numcols = something
read_file_float(&fp, &array, numrows * numcols, argv);
The unnecessary & applied to fp is discussed elsethread.
array is an object. A such, it is located in memory and has an
address. It also contains a value. Since it is a pointer object,
that value happens to be the address of an area of memory suitable for
holding a number of floats. &array evaluates to the address of the
object.
In your function, you immediately dereference this value. The result
of this operation is the contents of the pointer object which is the
location where you want to store data. Note that nowhere in your
function do you attempt to modify the contents of the pointer object
itself. Consequently, there is no need to pass the address of the
pointer to the function. Thus, the & applied to array is just as
unnecessary as the one applied to fp.
To answer your response in a subsequent message, yes you have
misunderstood slightly. Every argument passed to a function is passed
by value (as if a copy of the argument is passed to the function).
This is true whether the object is a number (integer or floating) or
an address. As a result of this, when the argument is a scalar
object, the function is incapable of modifying the value of the object
***in the calling function***. It is capable of modifying the local
copy that was passed but this is not the same as the original object.
Therefore, when the function needs to modify the object in the calling
function, one common technique is to pass the address of the object
(using the & operator). The function can then dereference this
address to get access to the object in the calling function.
However, this does not apply to your function because it us not
attempting to modify any of the arguments it is passed. fread never
updates the FILE* you pass it; it only updates the FILE the pointer
points to. Your code does not attempt to modify either copy of array,
only the memory that array in the calling function points to. While
the code you have will work, because you added the necessary
dereference operators, it is unnecessary. You can remove the &'s from
the calling statement and remove one * from every use of fp and array.
This works fine.
The problem comes when I do something similar but instead use fseek -
so in this instance i only want to read part of the file into the
array e.g. array[counter]
Now my call to the function would be
read_file_float(&fp, &array[some_counter]), 1, argv);
But I don't know how to declare this correctly.
If you fix your function to expect a simple float* as described above,
this will magically be correct. If for some reason you have not
disclosed to us you really need the extra level of indirection, then
one method for reading a single value would be
float *x;
x = &array[some_counter];
read_file_float(&fp, &x, 1, argv);
--
Remove del for email
.
- Follow-Ups:
- Re: writing library functions and pointers?
- From: Martin
- Re: writing library functions and pointers?
- References:
- writing library functions and pointers?
- From: Martin
- writing library functions and pointers?
- Prev by Date: Re: writing library functions and pointers?
- Next by Date: Re: cast from int** to int*
- Previous by thread: Re: writing library functions and pointers?
- Next by thread: Re: writing library functions and pointers?
- Index(es):
Relevant Pages
|