Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 18th, 2005, 11:16 AM   #1
mesagsxbkr
Newbie
 
Join Date: Jul 2005
Posts: 1
Rep Power: 0 mesagsxbkr is on a distinguished road
Segmentation Fault!!!

I have some background in programming and i have to take this programming class for school. Anyway I was trying to run this program and when I compile it with gcc it compiles with no errors. But then when I try to run the program it returns with "Segmentation Fault". I tried to find out where the error was, but I am having no luck...i know this is a simple program but can you guys try to help me out? This is the code..

/*************************
*This program is creating*
*A table of Olympic*******
*Running distances********
*************************/
#include <stdio.h>
int main()
{
double a=100, b=200, c=400, d=800;
printf(
"Table of Olympic Running Distances\n"
"__________________________________________\n"
"%6s%20s%20s%20s\n"
"%6.0f%20.3f%20.3f%20.3f\n"
"%6.0f%20.3f%20.3f%20.3f\n"
"%6.0f%20.3f%20.3f%20.3f\n"
"%6.0f%20.3f%20.3f%20.3f\n"
"Meters", "Kilometers", "Yards", "Miles",
a, a*0.001, a*1.094, a*0.0006215,
mesagsxbkr is offline   Reply With Quote
Old Jul 18th, 2005, 11:25 AM   #2
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 116
Rep Power: 0 L7Sqr is an unknown quantity at this point
@note Please use code tags next time

It looks as if your code has been cut off, would you mind reposting with the rest of the code (or if the file is huge the trouble spots)

I would suggest gdb as a debugger.
compile with:
gcc -ggdb <file.c>
And you can then invoke the debugger to watch as your program runs.

@edit:
btw, I think you are one short on your commas:
"%6.0f%20.3f%20.3f%20.3f\n"
"%6.0f%20.3f%20.3f%20.3f\n"
"Meters", "Kilometers", "Yards", "Miles",

I believe you should have a comma before "Meters" to begin the argument list.

I have to say also, that that is one ugly way to use a printf statement. Just my opinion.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]
L7Sqr is online now   Reply With Quote
Old Jul 18th, 2005, 12:21 PM   #3
sykkn
Hobbyist Programmer
 
Join Date: Apr 2004
Location: Texas
Posts: 106
Rep Power: 5 sykkn is on a distinguished road
I think this fits your situation perfectly ... check out the source book if you get a chance ...

"One new feature introduced with ANSI C is the convention that adjacent string literals are concatenated into one string. ... However, the automatic concatenation means that a missing comma in an initialization list of string literals no longer causes a diagnostic message. A missing comma now results in a silent marriage of adjacent strings. This has dire consequences in circumstances like ..." ... yours ...

source: Expert C Programming: Deep C Secrets By Peter van der Linden ...

pretty interesting read so far ....
__________________
[ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
/* Don't make me use it! */
sykkn is offline   Reply With Quote
Old Jul 18th, 2005, 1:32 PM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
From what I see posted, and what does cause segfaults, is the that he has more format specifiers than he gives as variables. Obviously some code was truncated.

printf does not know how many arguments it has been given. It assumes there is one variable for each format specifier. So it keeps on trying to print them even when they are out of memory bounds.
jim mcnamara is offline   Reply With Quote
Old Jul 20th, 2005, 12:08 AM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 822
Rep Power: 4 The Dark is on a distinguished road
I think what sykkn was trying to say is that the OP has missed the comma between the end of his format specifier ("blahblah...%20f\n") and the first data parameter "Meters", this means that "Meters" is added to the end of the format specifier and all of the OP's carefully laid out parameters no longer match their %s and %f formats. This will cause a segfault when printf tries to interpret a double as a char *.

Edit: ignore this - didn't see L7Sqr's edit.
The Dark is online now   Reply With Quote
Old Oct 21st, 2005, 5:53 PM   #6
Blighttdm
Newbie
 
Blighttdm's Avatar
 
Join Date: Oct 2005
Posts: 17
Rep Power: 0 Blighttdm is on a distinguished road
Bumping this for my own problem.
As per the original post I'm having the same error...I have no idea why.
The entire program:

#include <stdio.h>
#define size 10

int main(void)
{
  float sluggo[size];
  int i=0,j=0;

  printf("\nWelcome to Sluggo, the program that generates an array of ten elements.");

  for(i=0;i<=9;i++)
   {
     printf("\nPlease enter your %l number:",sluggo[i+1]);
     scanf("%l", &sluggo[i]);   
   }

  for(j=0;j<=9;j++)
  {
   printf("\nHere are the members of the sluggo(in input order):");
   printf("%l,",sluggo[j]);
  }

return 0;
}

Any suggestions?
Blighttdm is offline   Reply With Quote
Old Oct 21st, 2005, 6:45 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
"l" is a modifier, not a format specifier. One one use %ld or %lf or whatever. Read the freakin' manual for what you use. Maybe it won't make sense, but at least give it a shot.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 21st, 2005, 6:46 PM   #8
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 822
Rep Power: 4 The Dark is on a distinguished road
This is a different problem, so probably deserves its own thread.
It would help if you told us when the segmentation fault occurs, is it straight way or only after entering all the items.

Possibly the %l as causing the problem as it is not a proper printf format specifier. You probably want %f for floats and %d for integeres.

Also this line
printf("\nPlease enter your %l number:",sluggo[i+1]);
will access outside the sluggo array when i is 9, i suspect you actually wanted
printf("\nPlease enter your %d number:",i+1);

edit: damn your fast fingers Dawei!
The Dark is online now   Reply With Quote
Old Oct 21st, 2005, 6:54 PM   #9
Blighttdm
Newbie
 
Blighttdm's Avatar
 
Join Date: Oct 2005
Posts: 17
Rep Power: 0 Blighttdm is on a distinguished road
Quote:
Originally Posted by DaWei
"l" is a modifier, not a format specifier. One one use %ld or %lf or whatever. Read the freakin' manual for what you use. Maybe it won't make sense, but at least give it a shot.
Sorry...I do try to read the manuals but c'mon, I'm looking for help.

Anyways it fixed the error, I just have to fix the saving...because it didn't save any input.
Blighttdm is offline   Reply With Quote
Old Oct 21st, 2005, 7:18 PM   #10
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 822
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by Blighttdm
Sorry...I do try to read the manuals but c'mon, I'm looking for help.

Anyways it fixed the error, I just have to fix the saving...because it didn't save any input.
I agree with you, Blighttdm, that was a bit harsh, even for Dawei.
The Dark is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:20 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC