Passing strings to functions and modifying them

This is a discussion on Passing strings to functions and modifying them within the C Programming forums, part of the General Programming Boards category; Hey all This is a newbie question... I want to pass a couple of strings to a function and then ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    44

    Passing strings to functions and modifying them

    Hey all
    This is a newbie question... I want to pass a couple of strings to a function and then have that function modify them. The function isn't returning anything, just modifying the strings... when I call the strings again from my main program, they haven't changed. If I print em out from within the function, they have changed. So what's the correct way to pass the address of those strings so I can change em?

    Thanks
    d02

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you are passing a char array or a char *, then any changes made in the function will be seen in the main program.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Unfortunately this not what I'm seeing.

    Code:
    void changeString (char* chSt)
    {
    	chSt = "blah";
    }
    
    int main(int argc, char** argv)
    {
    	char* chSt = "-";
    	changeString (chSt);
    	printf("%s", chSt);
    	
    	return 0;	
    }
    will output "-"

    TiA
    d02

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Remember the rules for function parameters: you can change what is pointed to, you cannot change what is passed in.

    You can modify the string pointed to in a function, and that works just fine. You cannot just try to make the pointer point to some completely different place, and expect that to work (that tries to modify the passed-in pointer). If you want to make the pointer point somewhere else, you must pass it in pointer-to-pointer.

    As you may or may not know, "string literals" are/can be stored in read-only memory, so any attempt to modify them will die a horrible death (at least we don't blue screen anymore, just that little box).

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Oh, and I suppose I should mention that = is not really considered a valid way to assign a string in C, except in the passing-around-pointers-to-constant-chars sense -- strcpy is generally used.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Thanks, that explanation really helped out!

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    http://cpwiki.sourceforge.net/Common...kes_and_errors
    This code can't modify the string because you're using string literals. And they should be const.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying Default Algebra Functions
    By studiesrule in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2006, 11:35 AM
  2. External Functions and Compiler linking issues
    By collymitch in forum C Programming
    Replies: 2
    Last Post: 04-07-2005, 04:17 PM

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