Re: writing library functions and pointers?



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
.



Relevant Pages

  • Re: private variable ?
    ... If you declared a PRIVATE variable in the calling function, ... passing an (Array) to the called function, ... ASIZE(aArray_, 2) ... FUNCTION MyNameIs(aName_) ...
    (comp.lang.clipper)
  • Re: passing parameters by reference
    ... Try thinking of it this way: everything in Lisp is passed ... If you passed in an array, any changes you make to array elements ... and see that it does not necessarily modify its argument. ... A macro call looks syntactically ...
    (comp.lang.lisp)
  • Re: Any way to take a word as input from stdin ?
    ... know whether it modifies the original array or not. ... It does modify the original array, but ... and it will form the basis of get_words function which will store all ... you intend to modify the pointer (by calloc ...
    (comp.lang.c)
  • Re: SSCANF
    ... This gives undefined behavior. ... You're trying to modify ... in this context will evalutate to pointers to their ... array of three char). ...
    (comp.lang.c)
  • Re: Problem in writing to a property node of a cluster
    ... I took the liberty to modify your VI for an alternative approach. ... You should keep your array of 32 clusters in a shift register and show only a single cluster as a front panel control. ... - Selecting a different transducer from the listbox on the left will load its settings into that control via a local variable. ...
    (comp.lang.labview)