Pointers as function arguments

This is a discussion on Pointers as function arguments within the C Programming forums, part of the General Programming Boards category; Hello, I am having some difficulty in understanding pointers when passed to functions. When I pass a pointer to a ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Pointers as function arguments

    Hello,

    I am having some difficulty in understanding pointers when passed to functions. When I pass a pointer to a function and assign some dynamic memory to its argument in the other function, should that dynamic memory be reflected in the invoking block?

    I have some code below. As you can see from executing it, the string and memory assigned in the function is not reflected in main. Why is this? If the value of testThis is an address and is being assigned to ttArg, shouldn't testThis and ttArg point to the same memory location?

    Code:
    #include <stdio.h>
    
    void testPointer(char *);
    
    main()
    {
    	char * testThis;
    	testPointer(testThis);
    	
    	printf("testThis in main is %s\n", testThis);		
    
    }	/* end main */
    
    /* begint the testPointer */
    void testPointer(char * ttArg)
    {
    
    	ttArg = (char *)malloc(10);
    	
    	strncpy(ttArg, "hello", 6);
    
    	printf("ttArg in testPointer() is %s\n", ttArg);	
    
    
    }	/* end testPointer */
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not really. You're still passing by value. You pass the value (the address that the pointer points to, which is totally random at this point) of the pointer to the function. You aren't passing the address of the pointer. If you wanted to actually update what the original pointer points at, you'd pass a pointer to the pointer.

    Try this on for size:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void testPointer(char **);
    
    main()
    {
    	char * testThis;
    	testPointer(&testThis);
    	
    	printf("testThis in main is %s\n", testThis);		
    
    }	/* end main */
    
    /* begint the testPointer */
    void testPointer(char ** ttArg)
    {
    
    	*ttArg = (char *)malloc(10);
    	
    	strncpy(*ttArg, "hello", 6);
    
    	printf("ttArg in testPointer() is %s\n", *ttArg);	
    
    
    }	/* end testPointer */
    That should work. OTP.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Class function pointers
    By VirtualAce in forum C++ Programming
    Replies: 40
    Last Post: 02-16-2005, 11:55 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Replies: 2
    Last Post: 03-07-2002, 09:14 AM

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