Thread: Pointer Passing Question

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

    Pointer Passing Question

    From the following code:

    Code:
    int setPath(char *aPth);
    
    int main(int argc. char*argv[]) {
       char *path;
    
       setPath(path);
       
       return 0;
    }
    How would I pass the path variable and edit it in the function so that when I changed it in the function, its contents would also change outside of it. I have looked online but I'm still having trouble comprehending this. Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hey great question, this was a personal beef of mine. Don't return a pointer!
    Code:
    #include<stdio.h>
    
    void testfunc (char *string) {
    	int i;
    	printf("in testfunc(): %s\n", string);
    	for (i=0; i<11; i+=2) string[i]='X';
    }	
    
    
    int main () {	    // 12345678901234567890		
    	char string[]="hello world";
    	testfunc(string);
    	printf("in main(): %s\n", string);
    	return 0;
    }
    Now you can use the return value for something else.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    96
    But say I have the following code:

    Code:
    int main() {
       char *path;
    
       func(path);
    }
    
    
    func(char *path) {
       char *foo;
    
       foo = "/index.html";
       strncpy(path, foo, strlen(foo));
    
       return path;
    }
    How do I change the value of path in the function so it is now changed in main?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What you have will work, except that you are not allocating memory for path in the first place.

    If you actually want to change path by pointing it at a different location, you need the address of the pointer (aka a pointer to pointer), and assign *path = ...;

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int main()
    {
        char path[100];
        func(path);
    }
    
    
    void func(char* path)
    {
        const char foo[] = "/index.html";
        strncpy(path, foo, sizeof(foo));
    }
    Or

    Code:
    int main()
    {
        const char* path;
        func(&path);
    }
    
    
    void func(const char** path)
    {
        *path = "/index.html";
    }
    And remember that functions always must have a return type!
    If your function will return nothing, you must add "void".

    Pointers work on a very simple principle.
    All variables are stored in memory, thus have a memory address.
    Each time a variable is passed to a function, a new variable of the same type is created and the value is copied over.
    So the new local copy will always be modified instead of the original.
    So if you pass in the address of the original variable, then the function could just write to that memory address to change the original.
    A memory address is stored by a pointer to that type. So if the original variable is char, then a pointer to that becomes char*.
    To take the address of a variable, use the & operator. Exceptions are arrays which are automatically converted to pointers when passed.
    Last edited by Elysia; 02-13-2009 at 07:31 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Unhappy

    You know the saddest part is I thought about this when I wrote my last post but decided not to bother adding
    Code:
    char *string=malloc(12);
    strcpy(string,"hello world");
    will work in place of
    Code:
    char string[]="hello world";
    but you must remember malloc puts your variable on the heap and so you are responsible for free()ing it...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  3. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM