Thread: Passing pointers to strings to functions

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Passing pointers to strings to functions

    Hi,
    I have the basic pointers stuff down but have trouble passing the pointer to a string to a function which then passes it to another function for editing, both functions then return and the change should be displyed in a message box.

    Code:
    void FirstFunction(char **first);
    void SecondFunction(char *second);
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
     	char *szText= "The Text Has Not Changed";
    
    	FirstFunction(&szText);
    
    	MessageBox(NULL, szText, "Note:", MB_OK | MB_ICONINFORMATION);
    
    
    	return 0;
    }
    
    void FirstFunction(char **first)
    {
    	strcpy(*first, "Text Half Changed");
    	MessageBox(NULL, *first, "Info", MB_OK | MB_ICONINFORMATION);
    	SecondFunction(*first);
    
    }
    
    void SecondFunction(char *second)
    {
    	strcpy(second, "The Text Has Changed);
    
    }
    Is there a good tutorial that someone could reccomend? I have looked all over and can only find stuff on passing a pointer to a function once.\

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's not any different the second time, other than the fact that you didn't do it right the first time.
    Code:
    void FirstFunction(char *first);
    void SecondFunction(char *second);
    The other problem is that szText points to a string literal, and the word "literal" there is a big hint meaning "cannot be changed". So when you try to change it, well, it won't. You need a string of your very own if you want to do things like strcpy, and that requires creating an array. Also, lose the stupid faux-Hungarian notation.
    Code:
    char Text[25] = "The Text Has Not Changed";

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Here is a quick example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void first(char*);
    void second(char*);
    
    int main(void){
    
    	char string[]= "Text has not changed";
    	printf("Main: %s\n", string);
    	first(string);
    	printf("In main: %s\n", string);
    
    	getchar();
    	return (0);
    }
    void first(char *firstString){
    
    	strcpy(firstString, "First Change");
    	printf("In first: %s\n", firstString);
    	second(firstString);
    }
    void second(char* secondString){
    
    	strcpy(secondString, "Second Change");
    	printf("In second: %s\n", secondString);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One minor thing to note here:
    Code:
    char array[] = "123456789";
    That gives you 10 characters. You can't put more in. You can put less in, but you can't put more in. It only allocates 10 spaces for you (one for each non-nul character, + the nul on the end). Not to be confused with this:
    Code:
    char array[9] = "123456789";
    Which specifically allocates 9 characters (you can't put more here either, this is just another fun thing C does with character arrays), but not a nul on the end.


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

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Got it now. Easy as just wasn't thinking! Thanks!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by drclark View Post
    Hi,
    I have the basic pointers stuff down but have trouble passing the pointer to a string to a function which then passes it to another function for editing, both functions then return and the change should be displyed in a message box.
    Like this....
    Code:
    #include <windows.h>
    #include <string.h>
    
    
    void FirstFunction(char *first);
    void SecondFunction(char *second);
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
     	char szText[] = "The Text Has Not Changed";
    
    	FirstFunction(szText);
    
    	MessageBox(NULL, szText, "Note:", MB_OK | MB_ICONINFORMATION);
    
    
    	return 0;
    }
    
    void FirstFunction(char *first)
    {
    	strcpy(first, "Text Half Changed");
    	MessageBox(NULL, first, "Info", MB_OK | MB_ICONINFORMATION);
    	SecondFunction(*first);
    
    }
    
    void SecondFunction(char *second)
    {
    	strcpy(second, "The Text Has Changed");
    
    }
    Your version fails because you are trying to change a string literal and no decent compiler is going to let you do that.
    The message boxes won't come up because you haven't included windows.h
    Also you have to compile and linke the program in GUI mode before it's going to even build correctly.





    Is there a good tutorial that someone could reccomend? I have looked all over and can only find stuff on passing a pointer to a function once.\
    Thanks
    Any basic C tutorial is going to cover passing strings to functions.
    Last edited by CommonTater; 08-15-2011 at 08:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help in passing strings to functions..
    By nyekknyakk in forum C Programming
    Replies: 5
    Last Post: 10-13-2010, 09:29 PM
  2. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  3. Passing strings to functions?
    By Afro in forum C Programming
    Replies: 11
    Last Post: 10-01-2007, 02:42 PM
  4. Passing & Returning Strings from Functions
    By nisaacs in forum C Programming
    Replies: 1
    Last Post: 01-30-2002, 05:34 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM