Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

A.Avinash_Goutham

  • 3 years ago

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

  • This Question is Closed
  1. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @slotema

  2. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  3. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  4. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  5. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  6. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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)

  7. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it's size-1

  8. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    and then it decreases in that while loop

  9. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  10. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  11. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  12. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  13. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    @slotema

  14. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  15. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  16. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  17. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    did u check the file? the file has weird characters

  18. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i used a fprintf for that file

  19. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  20. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ohh so i have to use a loop here?

  21. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Yep. Like you did in the print function.

  22. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  23. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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'.

  24. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  25. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it;s stuck in a loop smwer?

  26. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Check the power function. The problem is in there.

  27. slotema
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    you can also use the pow function from math.h

  28. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  29. A.Avinash_Goutham
    • 3 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  30. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy