### segmentation fault

I wrote a program that gets pointers that points at structures of potus that includes name, term and home state. the program then sorts the potus list from last name, inauguration year, and home state. i have to separate the last name from the whole name and the inauguration year from the term. after i finished the code and compiled it i tried to run it but i keep getting segmentation errors. here is the code

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195`` ``````#include #include #include "potus.h" #define FILENAME "presidents.dat" #define CHARLEN 27 using namespace std; void sortName( struct president * [], char * [], int ); // sorts the presidents by last name void sortYear( struct president * [], int ); // sort the presidents by inauguration year void sortState( struct president * [], int ); // sorts the president by home state void swap( struct president ** , struct president ** ); // swaps the two things that are sent to it int main() { struct president * potusList[50]; // array of pointers to structure int numPotus; // number of potus structures extracted from file char temp[27]; // temperorary variable char * ptr; // pointer that points at tokens char * lastName[numPotus]; // array of pointers that point at last name int count; // count for loop //call function to load structures loadPotus( potusList, FILENAME ); numPotus = loadPotus( potusList, FILENAME ); // call functions to sort array for( count = 0; count < numPotus; count++ ) { strcpy( temp, potusList[count] -> name ); ptr = strtok( temp, " ." ); while( ptr ) { ptr = strtok( NULL, " .\n\t" ); lastName[count] = ptr; } } sortName( potusList, lastName, numPotus ); // pause program cout << "Press ENTER to continue"; cin.get ( ); cin.ignore ( 1, '\n' ); for( count = 0; count < numPotus; count++ ) { strcpy( temp, potusList[count] -> term ); ptr = strtok( temp, "- " ); if( ptr ) { year[count] = ptr; } } sortYear( potusList, year, numPotus ); // pause program cout << "Press ENTER to continue"; cin.get ( ); cin.ignore ( 1, '\n' ); sortState( potusList, numPotus ); return 0; } /* sortName: sorts all the potus by last name and prints them out * Parameters: * potusList: array of pointers that point at potus structures * numPotus: number of elements in array * Returns: nothing */ void sortName( struct president * potus[], char * lastName[], int numPotus ) { int count; // count for loop int swapCtr; // counter for sort loop int min = 0; // first name in list // loop to sort for( swapCtr = 0; swapCtr < numPotus; swapCtr ++ ) { min = swapCtr; for( count = min + 1; count < numPotus; count ++ ) { if( lastName[count] < lastName[swapCtr] ) { swap( & potus[swapCtr], & potus[swapCtr + 1] ); } } } // loop to print sorted array cout << "Sorted by Last Name:\n"; for( count = 0; count < numPotus; count++ ) { cout << potus[count] -> name << "\n"; cout << potus[count] -> term << "\n"; cout << potus[count] -> state << "\n"; } } /* sortYear: sorts all the potus by inauguration year * Parameters: * potusList: array of pointers that point to potus structures * numPotus: number of elements in array * Returns: nothing */ void sortYear( struct president * potus[], char * year [], int numPotus ) { int count; // count for loop int swapCtr; // count for outer loop int sorter; // count for inner loop int min = 0; // first name in list // loop to find last name and sort for( count = 0; count < numPotus; count++ ) { min = count; for( swapCtr = count + 1; swapCtr < numPotus; swapCtr ++ ) { if( year[swapCtr] < year[count] ) { swap( & potus[count], & potus[swapCtr] ); } } } // loop to print sorted array cout << "Sorted by Inauguration Year:\n"; for( count = 0; count < numPotus; count++ ) { cout << year[count] << "\n"; cout << potus[count] -> name << "\n"; cout << potus[count] -> term << "\n"; cout << potus[count] -> state << "\n"; } } /* sortState: sorts all the potus by home state * Parameters: * potusList: array of pointers that point to potus structures * numPotus: number of elements in array * Returns: nothing */ void sortState( struct president * potus[], int numPotus ) { int count; // count for loop int swapCtr; // count for swap loop int min = 0; // first name in list for( count = 0; count < numPotus; count++ ) { min = count; for( swapCtr = count + 1; swapCtr < numPotus; swapCtr ++ ) { if( * potus[swapCtr] -> state < * potus[min] -> state ) { swap( & potus[min], & potus[swapCtr] ); } } } // loop to print sorted array cout << "Sorted by Home State:\n"; for( count = 0; count < numPotus; count++ ) { cout << potus[count] -> name << "\n"; cout << potus[count] -> term << "\n"; cout << potus[count] -> state << "\n"; } } /* swap: swaps the 2 pointers that are sent to the function * Parameters: * lesser: pointer that will be replaced * greater: pointer that will take the place of another * Returns: nothing */ void swap( struct president ** lesser, struct president ** greater ) { struct president * temp = 0; // temp variable for structure pointer // swap pointers temp = * lesser; * lesser = * greater; * greater = temp; } ``````

Any ideas on whats wrong in my code?
Last edited on
Look at line 21. You never initialized numPotus. That means that on line 29 when you begin your for loop, it runs from 0 to some huge number, and on line 31, you will eventually go out of the bounds of your potusList[] array
wow i cant believe i missed that thanks but i changed it to 50 but its still giving me a segmentation fault
Topic archived. No new replies allowed.