Thread: Sagmentation Fault pls help!

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    30

    Sagmentation Fault pls help!

    I'm writing a program that detects character entry and move a submarine that's on the display. here is the move code.
    h moves left, j moves down, k moves up, l moves right.
    the ocean is a 6x15 box. i understand that if i go out of boarder it will give me segmentation fault, but no matter where i go it ends the program with that msg.
    Pls help!!!!!!!

    test driver
    Code:
    #include<stdio.h>
    main()
    {
            int lat_s = 5;
            int long_s = 0;
            char c;
            int *lats, *longs;
            lats = &lat_s;
            longs = &long_s;
    //      init_ocean();
    //      show_boat(lat_s, long_s);
            c = getchar();
            while(c!= 'q')
            {
                    printf("original lat is %d, long is %d\n", *lats, *longs);
                    move_sub(c, *lats, *longs);
                    printf("latitude is %d, longitude is %d\n", *lats, *longs);
                    c = getchar();
            }
    }
    here's the move_sub code
    Code:
    void move_sub(char c, int *lats, int *longs);
    
    void move_sub(char c, int *lats, int *longs)
    {
            switch(c)
            {
                    case'h': *longs = *longs - 1;
                             printf("longs is %d\n", *longs);
                             break;
                    case'j': *lats  = *lats + 1;
                            printf("lats is %d\n", *lats);
                             break;
                    case'k': *lats = *lats - 1;
                            printf("lat is %d\n", *lats);
                             break;
                    case'l': *longs  = *longs + 1;
                            printf("longs is %d\n", *longs);
                             break;
                    default: printf("Invalid Entry!");
                             break;
            }
    }

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    I dont know where you are having your error message, but you are passing to move_sub a value that a pointer points to. Drop the * from lats and longs when passing them in.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    That was magical!
    Thanks
    I thought I should pass the pointer value, and the pointer value will change according to entry. then it will change the value where it points to.
    What's the difference?

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    The difference is before you were dereferencing some value (say 4), versus dereferencing an actual memory location that may happen to contain the value 4.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > That was magical!
    Try putting the prototype BEFORE main, not just before the function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM
  5. help me pls..... :(
    By mocha_frap024 in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 10:46 AM