LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2011, 07:03 PM   #1
jahobjafwar
Member
 
Registered: Aug 2010
Posts: 31

Rep: Reputation: 0
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.
 
Old 07-28-2011, 07:17 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by jahobjafwar View Post
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?
 
Old 07-28-2011, 09:10 PM   #3
jahobjafwar
Member
 
Registered: Aug 2010
Posts: 31

Original Poster
Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault in C program radhikamlk Programming 5 02-10-2011 10:46 PM
segmentation fault in program at run time tkmsr Programming 4 10-16-2010 05:13 PM
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 12:09 AM
why segmentation fault in this program? asahlot Programming 13 10-17-2005 12:47 PM
"segmentation fault" when run a simple c program acer_peri Programming 11 05-28-2004 04:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration