www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/12/15/08:20:15

Date: Sun, 15 Dec 1996 08:12:51 -0500 (EST)
From: Michael Phelps <morphine AT hops DOT cs DOT jhu DOT edu>
To: Bill Lachance <billl AT synapse DOT net>
cc: djgpp AT delorie DOT com
Subject: Re: Pointers, arrays and assignments
In-Reply-To: <32B3AA6F.1302@synapse.net>
Message-ID: <Pine.GSO.3.95.961215080543.22811A-100000@hops.cs.jhu.edu>
MIME-Version: 1.0


On Sat, 14 Dec 1996, Bill Lachance wrote:

> Ahoy there,
> 
> I'm re-writing my tile-based game under Allegro to work better, 
> cleaner, and faster. That means switching from global variables
> to pointers. ;)
> 
> For some reason though, when I try to assign values to my pointer
> to player structures, the thing crashes.
> 
> I've initialised them like so: player *plyr[3] (the game will 

Do you really need to declare it as "player *plyr[3]"?  If so, you need to
allocate memory for the pointers to pointer and for the pointers
themselves.  Example:
	player	**plyr;
	int	x;

	plyr = (player **)malloc(3 * sizeof(player *));
	for (x = 0; x < 3; x++)
		plyr[x] = (player *)malloc(sizeof(player));
	/* of course, you need to test to see if == NULL, but this is just
	   an example */

If you can simply declare it as
	player	plyr[3];
then access it with "plyr[0].X = 4960"
 > support up to three players, and the array seemed
to be the > best way of supporting that). But when I try a stunt like:
> plyr[0]->X=4960, the program crashes abruptly (I've isolated the
> problem).
> 
> Am I breaking some unknown rule here?
> 
> Bill Lachance
> billl AT synapse DOT net
> 
						---Michael Phelps
						   morphine AT cs DOT jhu DOT edu


                               CH3
                               |
                               N
                             / |
                     ______/   |
                    /      \   CH2
             _____/         \__|__      
           //     \\        /  |  \\     
         //        \\______/___CH2 \\  
          \        /       \       /
           \______/         \_____/
          / ------ \       /      \
        OH           \   /         OH
                       O
 
                   Morphine



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019