Thread: Changing string value via function

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Here
    Posts
    23

    Changing string value via function

    Here is my simple working program:

    Code:
    #include <stdio.h>
    
    #define CHANGE_STR(old_str, new_str) (old_str = new_str)
    
    int main(void)
        {
        char *msg = "This is old message.";
    
        printf("%s\n", msg);
        CHANGE_STR(msg, "This is my new message.");
        printf("%s\n", msg);
    
        return 0;
        }
    But I have a question: how to make it work if macro would be replaced with an average function?

    Like this:

    Code:
    void CHANGE_STR(char *old_str, char *new_str)
        {
        /*
        Some code here.
        I have tried several tricks but so far nothing seems to work.
        */
        }
    I completely understand that using CHANGE_STR as function is completely irrational (since macro does the trick perfectly). But still, is it possible at all? If yes, then how to make it work? I'd just like to understand the logic of the C as deeply as possible.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Is strcpy what you are looking for?

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I completely understand that using CHANGE_STR as function is completely irrational (since macro does the trick perfectly).
    Someone is teaching you incredibly wrong things. A function to replace this is not irrational: A pointer assignment is not an overwrite.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    void CHANGE_STR(char **old_str, char *new_str)   {
        *old_str = new_str;    
    }
    
    // call this way
        char * str = "old_str";
        CHANGE_STR( &str, "new_str" );
    would do the same thing as the macro.

    I hope you understand why it is not very wise to assign string literals to pointers to char


    Kurt
    Last edited by ZuK; 08-05-2012 at 06:12 AM. Reason: typo

  5. #5
    Registered User
    Join Date
    Jun 2012
    Location
    Here
    Posts
    23
    Yes it really works! All the other tricks I tried caused whether errors or segmentation faults.
    Thank you so much.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by heinz55 View Post
    But I have a question: how to make it work if macro would be replaced with an average function?
    Not how you wrote it. If you're trying to write a function that changes the values of passed arguments, it needs to be given a pointer to the said argument:
    Code:
    void CHANGE_STR (char **old_str, char **new_str);
    
    /* ... */
    
    CHANGE_STR(&msg, &new_msg); /* new_msg being another pointer to some segment of read-only memory */
    The macro doesn't need to do this, because the text inside the macro is simply substituted in with no passing of arguments. You could even "pass" two integers to your macro and it would work.

    Quote Originally Posted by heinz55 View Post
    I completely understand that using CHANGE_STR as function is completely irrational (since macro does the trick perfectly). But still, is it possible at all? If yes, then how to make it work? I'd just like to understand the logic of the C as deeply as possible.
    Macros and functions each have their own purposes, and avoiding functions because "macros do the trick" tends to be a bad way to program. Either way, it's unnecessary to write your own encapsulating function or macro that simply assigns one variable to another.


    Quote Originally Posted by std10093 View Post
    Is strcpy what you are looking for?
    No, you can't use strcpy with read-only memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with changing a string in a function
    By noobiept in forum C Programming
    Replies: 7
    Last Post: 07-12-2010, 10:06 AM
  2. string changing problem
    By dusoo in forum C Programming
    Replies: 7
    Last Post: 04-22-2008, 07:18 AM
  3. Changing chars in a string
    By Hawkin in forum C Programming
    Replies: 6
    Last Post: 02-23-2008, 08:06 PM
  4. Changing characters in a string.
    By Loic in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2007, 05:12 AM
  5. Changing a input string to a function call?
    By cezaryn in forum C Programming
    Replies: 4
    Last Post: 07-16-2003, 02:37 PM

Tags for this Thread