trouble with pointers

This is a discussion on trouble with pointers within the C Programming forums, part of the General Programming Boards category; OK once again, pointers are causing me headaches I'm trying to get a word from a file, and write that ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    19

    trouble with pointers

    OK once again, pointers are causing me headaches

    I'm trying to get a word from a file, and write that into a buffer. The buffer is malloced like so:
    Code:
    char *buf = malloc( INIT * sizeof(char) );
    and the function called:
    Code:
    while ( getword(buf, INIT) != EOF )
    where getword has the definition:
    Code:
    int
    getword(char *W, int size)
    Then the buffer is realloced inside the getword function, if necessary.
    Code:
    size *= 2;
    W = realloc(W, size * sizeof(char) );
    The problem is, if the realloc is done, the W returned is correct, but the buf recieved is junk. I assumed that by reallocing W, i was reallocing the same pointer as buf. Is this not correct?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A pointer lets you change what it points to. Thus, if it's a pointer you need to change, you need a pointer to that pointer.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    If the memory pointed to by buf is realloc'ed within the getword function, then the buf argument must be passed as a pointer to a pointer.

    [edit]Like quzah said.[/edit]

    Code:
    int getword(char **W, int size)
    {
        ...
    
        // Resize/repoint the argument to a new larger memory area
        *W = realloc(*W,new_even_bigger_size);
    
        ...
    
    }
    
    ...
    
    // Initial malloc call
    char * buf = malloc(INIT);
    
    // Call getword function... afterwards, buf points to new address
    getword(&buf,INIT);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    cool thanks, that worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with pointers..
    By elfjuice in forum C Programming
    Replies: 7
    Last Post: 11-25-2007, 12:19 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Replies: 4
    Last Post: 12-10-2006, 06:08 PM
  4. trouble with pointers HELP MEEEE???
    By drdodirty2002 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2004, 11:39 PM
  5. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 06:53 PM

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