?
Solved

Segmentation Fault Question

Posted on 2001-08-07
4
Medium Priority
?
330 Views
Last Modified: 2010-04-15
Can someone please tell me why the following code is genrating a segmentation fault:

#include <stdio.h>
#define ARRAY_SIZE 200

/**************************************
Structure Prototype
**************************************/

typedef struct {
 char name[25];
 char continent[25];
 int population;
 int area;
 int gdp;
} Country;

/**********************************
addCountry Function Prototype
**********************************/

void addCountry (Country *pCountry);

main(){
int intOption = 0;
char chrOption[2];

Country country[ARRAY_SIZE], *pCountry;
pCountry = &country[ARRAY_SIZE];

 while(1){
  puts("\nPlease select an option from the list below:\n");
 
  puts("1. Add Country");
  puts("2. Edit Country");
  puts("3. List One Country");
  puts("4. List all Countries");
  puts("5. Quit\n");

  gets(chrOption);

  intOption = atoi(chrOption);

   if (intOption == 0 || intOption > 5)
    puts("\nInvalid Option...Please try Again");

   if (intOption == 5)
    break;

   switch (intOption) {
    case 1:
     addCountry(pCountry);
     break;
    case 2:  
     printf("You entered 2");
     break;
    case 3:
     printf("You entered 3");
     break;
    case 4:
     printf("You entered 4");
     break;
   }


 }
}

/**********************************
addCountry Function Definition
**********************************/

void addCountry (Country *pCountry){
char name[25];
char continent[25];
char population[25];
char area[25];
char gdp[25];
int intPopulation;
int intArea;
int intGdp;

system("clear");
puts("\nPlease Enter the Information Below: \n");

printf("Name: ");
gets(name);
strcpy(pCountry->name, name);
printf("\n%s", pCountry->name);

printf("Continent: ");
gets(continent);
strcpy(pCountry->continent, continent);
printf("\n%s", pCountry->continent);

printf("Population: ");
gets(population);
intPopulation = atoi(population);
pCountry->population = intPopulation;
printf("%d", pCountry->population);

printf("Area: ");
gets(area);
intArea = atoi(area);
pCountry->area = intArea;
printf("%d", pCountry->area);

printf("GDP: ");
gets(gdp);
intGdp = atoi(intGdp);
pCountry->gdp = intGdp;


printf("%s", pCountry->gdp);



}
0
Comment
Question by:Dargie
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
kotan earned 90 total points
ID: 6362312
At this line,

pCountry = &country[ARRAY_SIZE];

the index of "country" can only accept 0, 1, 2, ..., ARRAY_SIZE - 1.

So everytime you call addCountry(), you should decide which index should be passed.
0
 
LVL 85

Expert Comment

by:ozo
ID: 6362780
Are you entering more than 24 characters for name, continent, population, area, or gdp?
0
 
LVL 85

Expert Comment

by:ozo
ID: 6362800
intGdp = atoi(intGdp);
printf("%s", pCountry->gdp);
what is this?
0
 

Expert Comment

by:Neelima
ID: 6362862
dargie,
pcountry->gdp is an int type and oyu are using %s to print it.
0

Featured Post

Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question