Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (anonymous):

hey can any1 xplain me why i got a segementation fault? i was using gcc btw

OpenStudy (anonymous):

@slotema

OpenStudy (anonymous):

Most of the time, segmentation faults are caused by an array index that is too large. That's the case here as well. Check the values of `var` and try to figure out when that value is too large to access x/number.

OpenStudy (anonymous):

i took care f it r8? var < size in for loop? that's not good enough?

OpenStudy (anonymous):

But what's the value of var when `var >= size`?

OpenStudy (anonymous):

it's not var>=size it was var>=0

OpenStudy (anonymous):

As long as `var < size`, the loop will run. But after the loop is finished, what is the value of var? (which you use on number in the second loop)

OpenStudy (anonymous):

it's size-1

OpenStudy (anonymous):

and then it decreases in that while loop

OpenStudy (anonymous):

it's not `size - 1`, since `(size - 1) < size`.

OpenStudy (anonymous):

oh ok lemme try it but i got a printf for first iteration

OpenStudy (anonymous):

hey thanks it worked......gimme a min

OpenStudy (anonymous):

nelo? sry... m confused.....wat's ur name again?

OpenStudy (anonymous):

@slotema

OpenStudy (anonymous):

slotema is just fine. What's the problem with that program?

OpenStudy (anonymous):

it goes in to an infinite loop and prints weird symbols when i xpect some numbers.........

OpenStudy (anonymous):

That's a problem in `print_alt`. You're trying to print a string there ("%s"). A string in C is basically an array of characters ended by an element with the value 0 or '\0' (but not ended by '0'). Since number does not have a 0 at it's end, it'll keep printing something until the printf encounters a 0.

OpenStudy (anonymous):

did u check the file? the file has weird characters

OpenStudy (anonymous):

i used a fprintf for that file

OpenStudy (anonymous):

In your fprintf, the same thing is happening. You're trying to print an array as if it were a string. That's not going to work.

OpenStudy (anonymous):

ohh so i have to use a loop here?

OpenStudy (anonymous):

Yep. Like you did in the print function.

OpenStudy (anonymous):

what if a put a /0 in the end? will it work?

OpenStudy (anonymous):

It should stop printing, but what it's printing won't be readable numbers. If you're trying to print the integer 1 as a character, it won't show up as '1'.

OpenStudy (anonymous):

i changed the flag condition and i changed the print...

OpenStudy (anonymous):

it;s stuck in a loop smwer?

OpenStudy (anonymous):

Check the power function. The problem is in there.

OpenStudy (anonymous):

you can also use the pow function from math.h

OpenStudy (anonymous):

yup i found it sry my net's goin crazy and it's still in an infinite loop

OpenStudy (anonymous):

i finallyy completed it........ thanks @slotema

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!