Thread: Question about passing pointers

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    1

    Question about passing pointers

    Can someone tell me what I am doing wrong? I want to pass a NULL pointer to a function which will allocate some memory to it and assign it values. Then I want to be able to access the values outside of the function. This is probably an easy fix, but the solution is escaping me.


    Code:
    void test(int* tmp);
    
    void main(void)
    {
          int* data = NULL;
    
          test(data);
    
          printf("%d %d\n",data[0], data[1]);
    }
    
    void test(int* tmp)
    {
          tmp = (int *)malloc(sizeof(int)*2);
    
          *tmp[0] = 1;
          *tmp[1] = 2;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your function receives and modifies a copy of the original pointer. You have to pass a pointer to a pointer
    Code:
    void test(int** tmp);
    
    void main(void)
    {
          int* data = NULL;
    
          test(&data);
    
          printf("%d %d\n",data[0], data[1]);
    }
    
    void test(int** tmp)
    {
          *tmp = malloc(sizeof(int)*2);  // don't cast
    
          (*tmp)[0] = 1;   // this dereferences twice
          (*tmp)[1] = 2;
    }
    Kurt

    EDIT: don't cast the returnvalue of malloc. Look in the FAQ.
    Last edited by ZuK; 04-21-2006 at 09:50 AM.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    And another point: main always returns an int. Check the faq for the reason.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    or you can return the pointer value from the function:
    Code:
    int* test(void);
    
    int main(void)
    {
          int* data = NULL;
    
          data = test();
    
          printf("%d %d\n",data[0], data[1]);
          return 0;
    }
    
    int* test(void)
    {
          int *tmp = malloc(sizeof(int)*2);
    
          tmp[0] = 1;
          tmp[1] = 2;
          return tmp;
    }

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You should free your malloc()d memory.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Quote Originally Posted by bithub
    or you can return the pointer value from the function:
    Code:
    int* test(void);
    
    int main(void)
    {
          int* data = NULL;
    
          data = test();
    
          printf("%d %d\n",data[0], data[1]);
          return 0;
    }
    
    int* test(void)
    {
          int *tmp = malloc(sizeof(int)*2);
    
          tmp[0] = 1;
          tmp[1] = 2;
          return tmp;
    }

    thankyou bithub.. this helped me a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parameter passing and pointers
    By pokiepo in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 12:13 AM
  2. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  3. Passing by reference/using pointers
    By Wiretron in forum C++ Programming
    Replies: 11
    Last Post: 06-03-2007, 12:16 PM
  4. QUESTION!! parameter passing
    By jave in forum C Programming
    Replies: 8
    Last Post: 10-21-2005, 12:50 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM