Mail Archives: djgpp/1998/11/05/18:40:41
> If you are reading this text, then your mail reader does not support
> the MIME (Multipurpose Internet Mail Extensions) standard. To take
> full advantage of the features of this message, you need to upgrade
> your mailer to a MIME V1.0 compliant package. Some parts of this
> message may be in human readable form.
--zzzz726d825658c60salnet.orgzzzz
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
I've been having some trouble. The attached C source file compiled, but
the instant I attempt to run it, it quits. Does anybody have any ideas
as to why I'm having trouble?
Ethan Bowker
eab AT salnet DOT org
--zzzz726d825658c60salnet.orgzzzz
Content-Type: text/plain; name="call.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="call.c"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LAST -1
struct person {
char *name;
int call1, call2;
} *people;
int calc(int index, int num) {
if(people[index].call1!=LAST)
num=calc(people[index].call1,num+1);
else num++;
if(people[index].call2!=LAST)
num=calc(people[index].call2,num+1);
else num++;
return num;
}
int main() {
char **input;
char name[20];
char *tok;
int i, j, done=0, numlines=10;
input=(char **)malloc(10);
puts("Enter data:\n");
for(i=0,done=0;!done;i++) {
if(i==numlines)
if(realloc(&input,numlines+=10)==NULL) {
puts("Not enough memory!\n");
return 1;
}
if((input[i]=(char *)malloc(20))==NULL) {
puts("Not enough memory!\n");
return 1;
}
gets(input[i]);
if(input[i][0]=='.') done=-1;
}
free(input[i]);
realloc(&input,i-1);
numlines=i-1;
printf("Enter person: "); gets(name);
if((people=malloc(sizeof(struct person)*numlines))==NULL) {
printf("Not enough memory!\n");
return 1;
}
for(i=0;i<numlines;i++) {
people[i].name=strtok(input[i]," :");
people[i].call1=people[i].call2=LAST;
}
for(i=0;i<numlines;i++) {
tok=strtok(input[i]," :");
while(tok[0]==0) tok=strtok(NULL," :");
tok=strtok(NULL," :");
while(tok[0]==0) tok=strtok(NULL," :");
for(j=0;j<numlines;j++)
if(stricmp(people[j].name,tok)==0) {people[i].call1=j; break;}
tok=strtok(NULL," :");
while(tok[0]==0) tok=strtok(NULL," :");
for(j=0;j<numlines;j++)
if(stricmp(people[j].name,tok)==0) {people[i].call2=j; break;}
}
for(i=0,j=0;i<numlines;i++)
if(stricmp(people[i].name,name)==0) {j=calc(i,0);break;}
printf("%s calls %i people.", name, j);
return 0;
}
--zzzz726d825658c60salnet.orgzzzz--
- Raw text -