🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.mac.programmer.help
3 messages
3 total messages Started by Joel Weinstein Tue, 21 Nov 1995 01:47
Please help me with Pointers and Structs
#23
Author: Joel Weinstein
Date: Tue, 21 Nov 1995 01:47
7 lines
756 bytes
I know it's a lot to ask but I would appreciate it if you guys would
explain pointers and structs things to me.  I have tried to learn them
through several books I have, but the explainations they give are
impossible to understand.  Thanks a lot, and please respond to me via
e-mail.
            
                                                         Joel
Re: Please help me with Pointers and Structs
#22
Author: Bruce Wedding
Date: Wed, 22 Nov 1995 06:57
58 lines
1984 bytes
surgsw@mizzou1.missouri.edu (Joel Weinstein) wrote:

>I know it's a lot to ask but I would appreciate it if you guys would
>explain pointers and structs things to me.  

ftp a copy of Snippets from any Simtel mirror.  It has an excellent
pointer tutorial in it by Ted Jenson.  

What do you not understand about structs?  A struct is merely a
collection of variables that may be handled together in some ways or
addressed individually.  It is usually a "natural" way to group things
that have something in common.  For example:

struct _car {
	short year;
	char model[36];
	char make[36];
	long miles;
};

A struct may contain any valid type including pointers, other structs,
and user defined types.  You may also have arrays of structs.

struct _family_assets{
	double cash_on_hand;
	double savings;
	struct _car car;
};

struct _family_assets assets;

To address an element of a struct, use the . operator like this:
	assets.cash_on_hand = 10000;
To address a struct within a struct, follow the same logic:
	assets.car.year = 1995;

To pass a struct, pass a pointer to it like this:

	/* prototype */
void my_func( struct *_family_assets);
	/* call */
my_func( &assets );

In my_func, to assign values, you must use the -> operator.  Use it
anytime you are using a struct pointer.

/* in my_func */
assets->savings = 25000;

What else do you need to know?

Bruce


	
Bruce D. Wedding                        Have Compiler, Will Travel!
Software Demi God                       
Katy, TX, USA #1
Re: Please help me with Pointers and Structs
#24
Author: Peter Seebach
Date: Wed, 22 Nov 1995 20:30
26 lines
1223 bytes
In article <48uf2k$nab@gryphon.phoenix.net>,
Bruce Wedding <brucew@phoenixnet> wrote:
>struct _car {

The problem here is that "_car" is in the implementation's namespace.

>To pass a struct, pass a pointer to it like this:

This is not necessary; ANSI mandates the passing of structs as normal
parameters, as well.

>What else do you need to know?

Worth mentioning:
foo->bar
is the same as
(*foo).bar

-s
p.s.: You should almost always pass pointer-to-struct; it is likely more
efficient.
-- 
Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
C/Unix proto-wizard -- C/Unix questions? Send mail for help.  No, really!
Fund my computing hobby - send money! -- seebs@intran.xerox.com
The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads