On 02/01/2012 00:09, Ernst wrote:
> Hello and thanks for reading.
> I like to write C programs as my hobby and I rarely use functions for
> my projects simply because most of my efforts are workable in the
> domain of main().
> I have read much in the past two days and have tried changing my code
> but I am not getting it so even though I have searched and read a
> bunch I need to have a little help with understanding passing and
> using an array of structures in a function..
> So, please forgive this Umpteenth million post on this subject but at
> this time a voice of experience is what I need. I am confused.
> This is my scenario.
> I declare a pointer to a structure and use calloc() to allocate N-
> I wish to have a function access that array so I try to send the
> pointer to that array to that function (which is the pointer I
> assigned to from calloc() ) and use it but I cannot get past errors
> and warnings in GCC so I know I have forgotten something and reading
> and changing code has simply frustrated me since I am not remembering
> or understanding what I am reading.
> I understand how to allocate the array of structures and using
> pointers to access the elements of the array I can do in my main().
> I seem to not know how to properly define a function that will
> transfer access to an array of structures to a called function using
> the pointer I created and calloc() assigned to.
> I need guidance in remembering or learning what the defines must be
> for both the Main() and the called FN().
> I am allocating such as:
> struct my_struct *struct_array_ptr ;
> struct_array_ptr = calloc(......)
> Function_Call( struct_array_ptr );
The first thing I note is that this function declaration does not
include a parameter to pass the size of the array. In C number of
elements in an array is only implicitly available in the scope where the
array is created, elsewhere all you have is a pointer (effectively to
the first element)
> What is the right way to do this please?
There are two common techniques. The first is to have a special value
for the last element of an array. An example of this is the way C uses
an array of char for a string. The commoner technique is to pass the
number of elements as an argument in a function call.
Arrays if arrays add extra complexity but that is another topic.
comp.lang.c.moderated - moderation address: firstname.lastname@example.org
-- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.