LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Segmentation fault while trying to run C program. (https://www.linuxquestions.org/questions/programming-9/segmentation-fault-while-trying-to-run-c-program-894381/)

jahobjafwar 07-28-2011 07:03 PM

Segmentation fault while trying to run C program.
 
ok. so i wrote a program to fill an array with 100 random numbers ranging from 1 - 200. i compiled the program using gcc. the program successfully compiled but when i try to run it i get a segmentation fault. here is the code. i put *** on the line that gdb indicated was the problem.

#include <stdio.h>
#include<stdlib.h>
#include<time.h>

//Function Declarations
void loadTlist (int tList[]);
void printResults (int tList[]);

int main()
{
// Local Declarations
int tList [100] = {0};



// Statements
loadTlist (tList);
printResults (tList);

return 0;
} //main


/*====================loadTlist===================
A function to fill the array designated 'tList' with
random numbers ranging 1 - 200.
*/
void loadTlist (int tList[])
{
// Statements
srand(time(NULL)); //set seed to time to ensure random number is different every time.

for(int i = 0; 1 < 100; i++)
***tList[i] = rand() % 200 +1;***
return;
} // Load tList


/*====================printResults====================
A function to print out the random array 'tList'
*/
void printResults (int tList[])
{
// Statements
for(int i = 0, line = 0; i < 100; i++, line++)
{
printf("%5d", tList[i]);
if (line == 9)
{
printf("\n");
line = 0;
}
}

return;
} // Print Results


any insight would be appreciated.

johnsfine 07-28-2011 07:17 PM

Quote:

Originally Posted by jahobjafwar (Post 4428060)
for(int i = 0; 1 < 100; i++)
...
any insight would be appreciated.

Maybe my screen has a more readable font than you used, making the bug instantly obvious.

How many times is one less than one hundred?

jahobjafwar 07-28-2011 09:10 PM

i must admit i feel quite silly now. using a 1 instad of i. thanx for pointing it out. not a total loss though. i learned how to use gdb...somewhat.


All times are GMT -5. The time now is 04:41 PM.