Solved

Segmentation Fault Question

Posted on 2001-08-07
4
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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

734 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