Basic pointer question

This is a discussion on Basic pointer question within the C Programming forums, part of the General Programming Boards category; Code: #include <assert.h> #include <malloc.h> void func1(int *val); int main() { int *val = 0; func1(val); assert(val != 0); } ...

  1. #1
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311

    Basic pointer question

    Code:
    #include <assert.h>
    #include <malloc.h>
    
    void func1(int *val);
    
    int main()
    {
      int *val = 0;
      func1(val);
      assert(val != 0);
    }
    
    void func1(int *val)
    {
      int *newint = malloc(sizeof(int));
      val = newint;
    }
    The problem is of course, that assert always fails.

    Can anyone tell me why the pointer in main isn't being assigned to the variable on the heap?

    Ah, is it because after the func1 returns, newint is pointing to garbage?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you want to change the value of an int in a different function, you have to pass the address of the int to a function. If you want to change the value of a pointer in a different function, you have to pass the address of the pointer to the function.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311
    I want the pointer declared in main to point to the int created on the heap when func1 returns. I thought that by passing just "val" into func1, I was passing it's address.

    /sigh

    I could do it this way:
    Code:
    int *func1(void)
    {
      return malloc(sizeof(int));
    }
    
    int main()
    {
      int *val = func1();
    }
    but I wanted to reserve the return value for an error code.

    I can see what the problem is with the original code. All I'm doing is modifying the local paramter pointer var, having no effect on the one in main.

    [EDIT again]

    Sorry for the long posts. Maybe this will be of help to someone else. The answer lies with pointers to a pointer. This is making my head spin, but I've got it now.

    Code:
    #include <assert.h>
    #include <malloc.h>
    
    void func1(int **val);
    
    int main()
    {
      int *val = 0;
      func1(&val);
      assert(*val == 100);
    }
    
    void func1(int **val)
    {
      int *newint = malloc(sizeof(int));
      *newint = 100;
      *val = newint;
    }
    Last edited by MacNilly; 06-18-2007 at 05:10 PM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Indeed. This would work (changes are commented):
    Code:
    #include <assert.h>
    #include <stdlib.h>    // <-- malloc.h is deprecated. Use stdlib.h instead
    
    void func1(int **val);
    
    int main()
    {
      int *val = 0;
      func1(&val);     // <---- Pass the address of val, instead of the value of val
      assert(val != 0);
    }
    
    void func1(int **val)  // <---- make val a pointer to a pointer to type int
    {
      int *newint = malloc(sizeof(int));
      *val = newint;       // <----- You must now dereference val for this assignment
    }
    Last edited by itsme86; 06-19-2007 at 08:30 AM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should update func1 prototype as well
    and you do not need the temporary var newint
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Yeah, I forgot to update the prototype. That's fixed now. I left the temporary variable in just to avoid changing more than needed to be changed to make it work.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. function pointer question
    By andrea72 in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 05:25 AM
  4. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM
  5. basic pointer question need help
    By venckman in forum C Programming
    Replies: 4
    Last Post: 04-23-2003, 11:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21