Solved

Segmentation Fault Question

Posted on 2001-08-07
4
297 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.

770 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