#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    7
    Rep Power
    0

    Post Segmentation fault on passing a structure to a function


    Hello, i hope someone is able to help me. I am trying to understand this, so i tried this small example.

    I get an "Segmentation fault" when i run this example, can anyone help me and tell me why?

    this is the code:

    ----


    struct result
    {
    int x;
    };

    main()
    {
    struct result result1 = { 4 };

    test(result1);

    printf("Result: %i\n\n", result1.x);

    }

    test(struct result *mypointer) {

    mypointer->x = 6;

    }

    -----

    Thanks for all you help!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    81
    Rep Power
    12
    #include <stdio.h>

    struct result
    {
    int x;
    };
    void test(struct result*);
    int main()
    {
    struct result result1 ={4};
    test(&result1);

    printf("Result: %i\n\n", result1.x);

    }

    void test(struct result *mypointer) {

    mypointer->x = 6;
    }

IMN logo majestic logo threadwatch logo seochat tools logo