www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/12/10/00:16:59

Xref: news-dnh.mv.net comp.os.msdos.djgpp:3731
Path: news-dnh.mv.net!mv!news.sprintlink.net!cheatum.frontiernet.net!Empire.Net!news.net99.net!news.alt.net!news1.alt.net!news.u.washington.edu!aue
From: aue AT u DOT washington DOT edu (A. Aue)
Newsgroups: comp.os.msdos.djgpp
Subject: wierd "bug" in my program?
Date: 9 Dec 1995 04:18:51 GMT
Organization: University of Washington, Seattle
Lines: 137
Nntp-Posting-Host: homer31.u.washington.edu
Nntp-Posting-User: aue
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Dj-Gateway: from newsgroup comp.os.msdos.djgpp

Help! I'm having a really strange problem with the bridge program I'm 
writing under djgpp. There are three functions, main(), deal() and 
initialize(). I declare a structure outside of the program containing all 
the information I'll need for the hands so that all of the functions can 
use it. Initialize, which words properly, simply zeroes all the variables 
in the structure. deal() reads random numbers from 1-52 into the arrays 
Hand[x].cards. This also seems to work properly. The problem crops up 
when I print the contents of these arrays onto the screen. Oh yeah - 
here's my sys. info:


I am running it on a 386dx40, 4meg ram, under dos (_not_ the windows dos 
box.)

Anyway, the problem is as follows:

At first, I didn't even have the element hand.name included in the Hand 
structure, because I don't need it. Every time I ran the program, though, 
I got a "segmentation violation in pointer blah blah blah" - which is 
wierd, 'cause I'm not even using pointers in the program, though I guess 
the compiler must be creating some.
Anyway, I don't know why I put the element in, but I did, and when I run 
it now, it works - _as long as_ the strings I give allhands[o].name are 
three or less characters long. (i.e. "sou" is valid, but "south" is not.)
Whenever I put in a longer string, it gives me the segmentation 
violantion errors again. When I symify, it won't tell me the line number 
in the program where the error occured (the program runs, but gives me 
the error message at the end of the for loop in the main() function. What 
is going on here? Why won't it run properly without the name[] element, 
and why does it mess up when allhands.name[o] is more than three 
characters long? I'm confused.

If anyone has any light to shed on this problem, I'd really appreciate 
it. To avoid wasting BW, feel free to email me at <aue AT u DOT washington DOT edu>.
Thanks in advance for any help.
(program follows)




#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct hand {
	char name[30];
	int cards[13];
	int clubs[13];
	int diamonds[13];
	int hearts[13];
	int spades[13];
	int numclubs;
	int numdiamonds;
	int numhearts;  
	int numspades;
	int hcp;
	int dp;
	int tp;
	int turn;
	int bid;
	} Hand;


main()
{
Hand hands[4];
int n;
void initialize (Hand[]);
void deal (Hand[]);

printf("begin main");

initialize (hands);
deal (hands);
printf("hello.");

for (n=1; n<=13; ++n)
	printf("South %d west %d north %d east %d\n", hands[1].cards[n], hands[2].cards[n], hands[3].cards[n], hands[4].cards[n]);
printf("end main.");        

}
void deal (Hand allhands[])
{
int r;
int n;
int deck[53];
int shuffle[53];
printf("beginning deal");
srand((unsigned)time(NULL));  /* seeds random number counter */
for (n=1; n<=52; ++n)        /* reads cards into deck */
	deck[n]=n;
n=1;
while (n <= 52)             /* shuffles the deck */
	{
	r=rand()%52;
	if (deck[r+1]!=0)
		{
		shuffle[n]=deck[r+1];
		deck[r+1]=0;
		++n;
		}
	}

for (n=1; n<=13; ++n)              /* reads the cards into hands */
 
	{
	allhands[1].cards[n]=shuffle[n];
	allhands[2].cards[n]=shuffle[n+13];
	allhands[3].cards[n]=shuffle[n+26];
	allhands[4].cards[n]=shuffle[n+39];
	}
printf("end deal.");
}

	
void initialize (Hand allhands[])
{
int n;
int o;

printf("begin initialize");
for (o=1; o<=4; ++o)
	{
	scanf("%s", allhands[o].name);
	for (n=0; n<=13; ++n)
		{
		allhands[o].cards[n]=allhands[o].clubs[n]=allhands[o].diamonds[n]=
		allhands[o].hearts[n]=allhands[o].spades[n]=0;
		}
	allhands[o].numclubs=allhands[o].numdiamonds=allhands[o].numhearts=  
	allhands[o].numspades=allhands[o].hcp=allhands[o].dp=allhands[o].tp=
	allhands[o].turn=allhands[o].bid=0;
	}

printf("end initialize.");
}

- Raw text -


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