Solved

Segmentation Fault Question

Posted on 2001-08-07
4
291 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 30 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 84

Expert Comment

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now