Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Struct and proper coding technique

1 view
Skip to first unread message

John Estess

unread,
Jun 14, 1999, 3:00:00 AM6/14/99
to

> First, are there any good web based resources that can provide guidance
> to properly coded C programs ?

There have been plenty of resources posted in the last few days.

> Second, I have a pointer to a data structure that consists of a couple
> of string pointers. I am passing the structures's pointer to a
> function. Within the function I would like to iterate through the
> elements of the structure without using the -> operator. My thought was
> that I could do some sort of pointer arithmetic with a counter and
> starting at the head of the structure iterate through it that way,
> something like:
>
> ctr=0;
> (pStructure + 0) = "string1"; <- I realize this is incorrect...
> (pStructure + 1) = "string2";
> etc...
>

Ollie, I'm not one of the "experts" here, but I'd use a linked list for
something like this. You really seem to want an growable array of
strings, but linked list is how that would be implemented (I think).

John
Energy is Eternal Delight. He who desires but acts not,
breeds pestilence. William Blake

olliecat

unread,
Jun 15, 1999, 3:00:00 AM6/15/99
to
I'm brushing up on my moderate C experience and have two questions for
anybody that could help out.

First, are there any good web based resources that can provide guidance

to properly coded C programs ? I know there are all kinds of different
ways to accomplish a task in C but I think there are also some generally
accepted methods as well. I'd like to learn the 'correct' way and try
to avoid bad habits that will make my code illegible and amateurish.

Second, I have a pointer to a data structure that consists of a couple
of string pointers. I am passing the structures's pointer to a
function. Within the function I would like to iterate through the
elements of the structure without using the -> operator. My thought was
that I could do some sort of pointer arithmetic with a counter and
starting at the head of the structure iterate through it that way,
something like:

ctr=0;
(pStructure + 0) = "string1"; <- I realize this is incorrect...
(pStructure + 1) = "string2";
etc...

Is there a way I can do that ? The reason I'm asking is if I include my
struct definition in a header file then if the struct ever changes I
only have to recompile the programs including the header. If I use the
-> operator to reference the struct elements by name I'll have to make
source code changes every time the struct changes. Am I going about it
the wrong way ? Any suggestions containing illustrative code fragments
would be most welcome.

Thanks - The cat

Stephan Wilms

unread,
Jun 15, 1999, 3:00:00 AM6/15/99
to
olliecat schrieb:

>
> I'm brushing up on my moderate C experience and have two questions for
> anybody that could help out.
>
> First, are there any good web based resources that can provide guidance
> to properly coded C programs ?

Hi olliecat,

I can give you two good non-web-based resources:
R. Sedgwick, Algorithms in C, Addison Wesley
D. Knuth, The Art of Computer Programming vols 1 & 3, Addison Wesley

> Second, I have a pointer to a data structure that consists of a couple
> of string pointers. I am passing the structures's pointer to a
> function. Within the function I would like to iterate through the
> elements of the structure without using the -> operator.

Essentially you can not access the fields of a structure without knowing
their name. If all you want is avoid using the "->" operator, you can use
a pointer to the beginning of the structure in combination with the
"offsetof()" macro (which requires the structure field name). But if you
do not want the function to know about the structure field names there
isn't much you can do. The only thing I can think of is pass in a list of
precalculated structure field offsets, sortof as a replacement for the
names.

> My thought was
> that I could do some sort of pointer arithmetic with a counter and
> starting at the head of the structure iterate through it that way,
> something like:
>
> ctr=0;
> (pStructure + 0) = "string1"; <- I realize this is incorrect...
> (pStructure + 1) = "string2";

If you know the offsets you can do something similar. Lets say the offsets
are in an array called "offsets":
char *pStruct; /* make this point to the start of the structure */
/* ... */
*((char**)(pStruct + offset[0])) = "Test";
*((int*)(pStruct + offset[1])) = 42;

> Is there a way I can do that ? The reason I'm asking is if I include my
> struct definition in a header file then if the struct ever changes I
> only have to recompile the programs including the header.

This is impossible to do in a portable ANSI-C way. You might be able to
create a non portable solution by including a table of compiler specific
offsets in the header file, though.

> If I use the
> -> operator to reference the struct elements by name I'll have to make
> source code changes every time the struct changes. Am I going about it
> the wrong way ?

There is a real solution to your problem, but it would require a totally
different and much more complicated aproach. You could build a database of
structure entries and identify each structure entry by a type ID and/or a
name string:
struct entry
{
int typeId;
char name[MAX_NAME_SIZE];
char data[MAX_DATA_SIZE]; /* use "void*" for dynamic allocation */
};

If you create an array of these entries you can iterate through the
entries without knowing the name. The name string has the additional
advantage of identifying the entry by a real string that has been obtained
from somewhere (eg. entered by user, received by communication).

Stephan
(initiator of the campaign against grumpiness in c.l.c)

Andy Knight

unread,
Jun 15, 1999, 3:00:00 AM6/15/99
to
olliecat wrote:
>
> I'm brushing up on my moderate C experience and have two questions for
> anybody that could help out.
>
> First, are there any good web based resources that can provide guidance
> to properly coded C programs ? I know there are all kinds of different
> ways to accomplish a task in C but I think there are also some generally
> accepted methods as well. I'd like to learn the 'correct' way and try
> to avoid bad habits that will make my code illegible and amateurish.
>
> Second, I have a pointer to a data structure that consists of a couple
> of string pointers. I am passing the structures's pointer to a
> function. Within the function I would like to iterate through the
> elements of the structure without using the -> operator. My thought was

> that I could do some sort of pointer arithmetic with a counter and
> starting at the head of the structure iterate through it that way,
> something like:
>
> ctr=0;
> (pStructure + 0) = "string1"; <- I realize this is incorrect...
> (pStructure + 1) = "string2";
> etc...

>
> Is there a way I can do that ? The reason I'm asking is if I include my
> struct definition in a header file then if the struct ever changes I
> only have to recompile the programs including the header. If I use the

> -> operator to reference the struct elements by name I'll have to make
> source code changes every time the struct changes. Am I going about it
> the wrong way ? Any suggestions containing illustrative code fragments
> would be most welcome.
>
> Thanks - The cat

If we could see your structure definition, matters might be a little
clearer. However, I'm going to make a guess about what you're trying to
achieve. I think you have something like this...

struct MyStruct {
char* pszA ;
char* pszB ;
} ;

...and that you want to be able to use some form of pointer arithmetic
to "get at" the members of MyStruct. Sadly, that's a no-no! You can rely
upon the first member being at the beginning of the structure but you
can NOT rely on anything else other than the order in which the members
of the structure appear. (This is due to alignment/padding issues that
vary from system to system.)

What you most certainly CAN do is this...

#define NITEMS 2

struct MyStruct {
char* pszAll[NITEMS] ;
} ;

Now you can write your enumeration code based on an iteration from zero
to NITEMS-1. If you need to add another member to your structure, you
just have to increase the value of NITEMS and your enumeration code will
continue to work.

--
Andy Knight

The good thing about banging your head against a brick wall
is that the pain goes away when you stop.

Lawrence Kirby

unread,
Jun 15, 1999, 3:00:00 AM6/15/99
to
In article <3765A0BC...@bellatlantic.net>
olli...@bellatlantic.net "olliecat" writes:

>I'm brushing up on my moderate C experience and have two questions for
>anybody that could help out.
>
>First, are there any good web based resources that can provide guidance
>to properly coded C programs ? I know there are all kinds of different
>ways to accomplish a task in C but I think there are also some generally
>accepted methods as well. I'd like to learn the 'correct' way and try
>to avoid bad habits that will make my code illegible and amateurish.

There is actually a lot of information in the FAQ concerning the correct
way to do things.

>Second, I have a pointer to a data structure that consists of a couple
>of string pointers.

It would help if you showed us the structure definition then we would
know exactly what we are talking about.

> I am passing the structures's pointer to a
>function. Within the function I would like to iterate through the
>elements of the structure without using the -> operator.

Generally speaking you can't do that because unless you work explictly
through the type of the structure concerned you don't know the offsets
of the various members. There can be arbitrary padding between structure
members.

> My thought was
>that I could do some sort of pointer arithmetic with a counter and
>starting at the head of the structure iterate through it that way,
>something like:
>
>ctr=0;
>(pStructure + 0) = "string1"; <- I realize this is incorrect...
>(pStructure + 1) = "string2";
>etc...

If you have a number of objects of a particular type and you need to
be able to index them then the natural thing to do is put them in an
array. You can in effect make them as well by using #defines or even
better enums constants as index values. If that applies to everything
in the structure you might replace the structure entirely by an array.
If there are other members in the structure you can make the array
a structure member.

>Is there a way I can do that ? The reason I'm asking is if I include my
>struct definition in a header file then if the struct ever changes I
>only have to recompile the programs including the header. If I use the
>-> operator to reference the struct elements by name I'll have to make
>source code changes every time the struct changes.

You'll presumably have to do that anyway if other parts of the program
access the structure members by name. If no part of the program does
there wasn't much point in defining them as separate structure members to
start with. Also ask yourself how and why the structure definition might
change in the future. Normally a significant change to a datastructure
would imply a fairly radical change to the code that maintains it.
Careful design to start with gegerally minimises the need for that sort
of thing.

> Am I going about it the wrong way ?

I think so.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


Paul Mesken

unread,
Jun 15, 1999, 3:00:00 AM6/15/99
to
On Tue, 15 Jun 1999 09:21:58 +0200, Stephan Wilms
<Stepha...@cwa.de> wrote:

>olliecat schrieb:


>>
>> I'm brushing up on my moderate C experience and have two questions for
>> anybody that could help out.
>>
>> First, are there any good web based resources that can provide guidance
>> to properly coded C programs ?
>

>Hi olliecat,
>
>I can give you two good non-web-based resources:
> R. Sedgwick, Algorithms in C, Addison Wesley
> D. Knuth, The Art of Computer Programming vols 1 & 3, Addison Wesley
>

YES! Properly coded C programs according to Donald Knuth. Finally you
understand that essentially all programming languages come down to
Assembly (MIX in the case of Knuth) ;-)

Richard Heathfield

unread,
Jun 15, 1999, 3:00:00 AM6/15/99
to
Paul Mesken <usu...@euronet.nl> wrote in article
<37679ddc...@news.euronet.nl>...

Er, no. MIX is the (mythical) machine. The assembly language that goes with
it is called MIXAL.

Since (by unstated but nevertheless undoubtedly completely unanimous
assent) Donald Knuth is always on-topic here, I have included a link to a
MIX emulator which you may find diverting (but I must #include
<disclaimer.h> here, as I didn't write it!):

http://members.home.net/bmenees/MIXBuilder.htm

Enjoy.

--
Richard Heathfield

The bug stops here.


Stephan Wilms

unread,
Jun 16, 1999, 3:00:00 AM6/16/99
to
Paul Mesken schrieb:

>
> On Tue, 15 Jun 1999 09:21:58 +0200, Stephan Wilms
> <Stepha...@cwa.de> wrote:
>
> >I can give you two good non-web-based resources:
> > R. Sedgwick, Algorithms in C, Addison Wesley
> > D. Knuth, The Art of Computer Programming vols 1 & 3, Addison Wesley
> >
> YES! Properly coded C programs according to Donald Knuth. Finally you
> understand that essentially all programming languages come down to
> Assembly (MIX in the case of Knuth) ;-)

Weeeeellllll, in case of Knuth you will have to rethink the code as C code
:-) Anyway, how did you end up here ? Has comp.lang.asm.x86 been closed
down due to lack of public interest ?

While you're here you might want to look up the Lawrence Kirby posting
where he explains the cache usage of the Pentium processor :-)

0 new messages