Thread: A question concerning character arrays

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    A question concerning character arrays

    OK, I know it works, I jst want to know why.

    OK, here is the deal, for examlpe I have a function:

    Code:
     void function(char s[]);
    and I want it to take the character array as input and manipulate it and then exit.

    here is an example invocation. why i am trying to accomplish is to take the array string and change its value from test to temp.

    Code:
      main(){
     
      char string[MAX];
    
      string="test";
      function(string);
    
      printf("%s",string);
    
    }

    ok the function written like this gets the desired resuts, the vaiable s is set to "temp" in the function and retains this value when the function exits and string is back in the main program.

    Code:
    void function(char s[]){
       sscanf("temp","%s",s);
    }
    now my question is why when written this way, it does not get the desired result.

    Code:
    void function(char s[]){
       char t[MAX];
       sscanf("temp","%s",t);
       s=t;
    }
    within the scope of the function s and t are correct, however when the function exists, back in main, string does not retain the value.

    I have a feeling this has something to do with pointers and referencing of the arrays, I'm just not sure what. Any additional reference material for this as well.

    Thanks!

    --ellipses--

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > s=t;
    Make that:
    strcpy( s, t );

    strcpy() is located in <string.h>

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    OK, Cool; that works too, but I am still wodering why the other function doesn;t work. Is it becuase the array that is declard within the function is destroyed after the function exits?

    for example this code works as well.

    Code:
    void equal(char s1[],char s2[]);
    
    main(){
    char string1[MAX];
    char string2[MAX];
    
    sscanf("foo","%s",string1);
    sscanf("bar","%s",string2);
    
    /* lets make string2 equal to string1 */
    equal(string1,string2);
    
    }
    
    void equal(char s1[],char s2[]){
        int i;
      for(i=0;st1[i] != '\0';i++){
       st2[i]=st1[i];
      }
      st2[i]='\0';
    }
    but yet if it used a variable within the scopr of the function only to temportily hold a value, it woudln't work.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Is it becuase the array that is declard within the function is destroyed after the function exits?
    That's one reason. But in addition to that, there's another reason.
    Code:
    s=t;
    doesn't copy t into s. All it does it assign the address of t to s. Since s is a copy pointing to string's address in main(), when the function ends, string points to it's original address. You can change the string's data (strcpy for example, or your equal() function by doing it one char at a time).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just a quick question about character arrays
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 04-03-2006, 07:20 AM
  2. Replies: 11
    Last Post: 11-26-2004, 08:13 PM
  3. Comparing Character Arrays
    By LostNotFound in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 12:42 PM
  4. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM