Thread: C Pointers

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    C Pointers


    Hello everybody,

    Can someone help me with this code? I really don't know why It's not working. Why the value in *(pointer->data) is not 5?
    I have a larger code, I just did this small sample code to illustrate the problem.

    Thanks

    #include <stdio.h>
    #include <stdlib.h>

    struct pointerStruct
    {
    int * data;
    };
    typedef struct pointerStruct* myPointerStruct;

    void pointerFunction(myPointerStruct,int );

    int main()
    {
    myPointerStruct pointer=(myPointerStruct) malloc(sizeof(myPointerStruct));
    pointerFunction(pointer,5);
    printf("VALUE AFTER FUNCTION: %d\n",(int)*(pointer->data));
    return 0;
    }

    void pointerFunction(myPointerStruct myPointer, int num){

    myPointerStruct myPointer2 = (myPointerStruct) malloc(sizeof(myPointerStruct));
    myPointer2->data=&num;
    printf("The new value of myPointer2 is: %d\n",*(myPointer2->data));
    myPointer = myPointer2;

    printf("The new value of myPointer is: %d\n",*(myPointer->data));

    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    First: hiding the pointerness of a type behind a typedef is "unsafe". You tend to forget the type is a pointer. It's better to see the * at the proper places in the code.

    Your error is you never allocate memory for data (which is a pointer).
    When you do
    myPointerStruct myPointer2 = (myPointerStruct) malloc(sizeof(myPointerStruct));
    you get a valid memory location for pointer. So pointer->data (another different pointer) exists and can be set to whatever you want. However is has not been initialized and its value cannot be used: which is what you do on the next line
    myPointer2->data=&num;// WRONG!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    The new value of myPointer2 is: 5
    The new value of myPointer is: 5
    VALUE AFTER FUNCTION: 2293508

    That's the output I get, the program doesn't crash and when I do myPointer2->data=&num; it actually works and everything. I need the pointer in the main to have the same value as myPointer, they're supposed to be the same variable. I don't know why at the end of the function prints 5 just fine and as soon as it goes back to the main it loses all information. It's like I'm passing the variable by value instead of reference.
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    Couple problems:

    1. The thing bdb said about typedef-ing a pointer being unsafe is coming back to bite you. myPointerStruct is a pointer, not a struct: sizeof(myPointerStruct) is the size of a pointer, not the size of the struct. General malloc()ing should look like
    Code:
    type* ptr = (type*)malloc(sizeof(type));
    Pay close attention to where the *s are and are not.

    2. num will be lost when the function ends. &num is unsafe: if you look at it inside the function you'll be fine, but if you assign that to something and look at it outside the function you'll have problems.

    And FYI, when you ask a programming question, especially one that involves why the program acts in certain way, please explain what it is that it's actually doing. Like saying that the new "new value of" statements show the right thing but when it tries the "after function" one it segfaults.
    [edit] Like you just did. Thanks.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    Originally Posted by requinix
    General malloc()ing should look like
    Code:
    type* ptr = (type*)malloc(sizeof(type));
    I prefer
    Code:
    type *ptr = malloc(sizeof *ptr);
    C is perfectly capable of converting the void* value returned by malloc() to the correct type of the ptr object: the cast is, at best, redundant; and it may hide an error the compiler would have caught otherwise.

    Using the object (rather than the type) as "parameter" to the sizeof operator has the advantage that you don't need to change the code in two locations when you change the type of the pointer ... and you can't get it wrong either :)
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0
    I understand. Thank you so much for your help, I actually checked the code again and changed the design. Now it's working.

    Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo