Struct & Arrays & passing to function, and the resulting array of confusion

45 views
Skip to first unread message

Ernst

unread,
Jan 1, 2012, 7:09:16 PM1/1/12
to
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-
structures.

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 );

What is the right way to do this please?

It has been several years since I studied this and I am sure I have
forgotten something.

Ernst
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- 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.

James Kuyper

unread,
Jan 2, 2012, 12:51:13 AM1/2/12
to
On 01/01/2012 07:09 PM, Ernst wrote:
...
> This is my scenario.
>
> I declare a pointer to a structure and use calloc() to allocate N-
> structures.
>
> 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 );
>
> What is the right way to do this please?

Everything you've told us so far about how your program works (which
isn't very much) seems fine. That means that the problem would have to
be in the parts you haven't told us about - which is virtually the
entire program.

Create a simple program that demonstrates the problem you're having.
Give us the FULL text of that program - not the part you think is
relevant. If you knew enough to determine what part was relevant, you
wouldn't need to ask this question.

Also, please tell us precisely which compiler you're using, and what
options you have specified for that compiler. Give us the FULL text of
the error messages it produced. Don't just give us the part that seems
relevant.
--
James Kuyper

Francis Glassborow

unread,
Jan 4, 2012, 7:17:07 PM1/4/12
to
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.

Jasen Betts

unread,
Jan 4, 2012, 7:16:52 PM1/4/12
to
On 2012-01-02, Ernst <Ernst...@sbcglobal.net> wrote:

> I declare a pointer to a structure and use calloc() to allocate N-
> structures.

...

> 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 ;

First off struct_array_ptr is a really bad name, all names starting with
"str" are reserved.

> struct_array_ptr = calloc(......)

> Function_Call( struct_array_ptr );
>
> What is the right way to do this please?

that looks good, but how does the function know the value of "N"

to match that call, your function should look like this:

void Function_Call( struct my_struct * foo ) ......

It should be defined (in prototype atleast) somewhere before main();
(else you'll get a warning which in this case is harmless)

--
⚂⚃ 100% natural

--- Posted via news://freenews.netfront.net/ - Complaints to ne...@netfront.net ---

Francis Glassborow

unread,
Jan 6, 2012, 4:09:17 PM1/6/12
to
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.

Rather than all those words, please post a simple attempted program that
demonstrates what you are doing, Then we will be able to give
constructive guidance.
Reply all
Reply to author
Forward
0 new messages