Thread: an answer raises another problem...

  1. #1
    Registered User
    Join Date
    May 2007

    an answer raises another problem...

    void func(char *s1, char *s2, char *s3){
     while(*s1 != '\0' ) s1++;
     while(*s2 != '\0' ) s2++;
     for(; *s1=*s2=*s3; s1++, s2++, s3++);
    main() {
     char s1[10] = "ABC", s2[10]="DEF";
     char s3[10] = "GHI";
     printf("%s, %s %s", s1, s2, s3);
    result => ABCGHI, DEFGHI GHI

    Now I've got another problem...
    at the end of for statement, pointer s1, s2, s3are all pointing to the '\0' character of each
    when you come back to main and print each string why does it print the whole thing? is it supposed to???
    and oh, another one.
    When does the for statement officially end??
    how does it get "false" value to end the statement???

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    As pointers, s1, s2, and s3, in main() are not altered when passed to another function. In general, you cannot modify a value of a variable in a calling function, but you can modify what such a variable points to....

    Case in point:

    #include <stdio.h>
    void change(int);
    void change(int y)
    	y = 10;
    int main(void)
    	int x = 5;
    	return 0;
    The value printed will be 5.

    If you change it to this:

    #include <stdio.h>
    void change(int *);
    void change(int *y)
    	*y = 10;
    int main(void)
    	int x = 5;
    	return 0;
    Then the value printed will be 10.

    As for the condition of the for statement ending, it ends when you copy a '\0', because numerically a '\0' char is just plain 0. In C, every number is true except 0, which resolves to false.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    Maybe this version is a little clearer.
    #include <stdio.h>
    #include <string.h>
    void func( char *that, char *other, const char *attach ) {
       that  = that + strlen(that);    /* Same as while(*s1 != '\0' ) s1++; */
       other = other + strlen(other);  /* Same as while(*s2 != '\0' ) s2++; */
       while( *attach != 0 ) {
         *that = *other = *attach;
         /* Assign the end of attach to each of the other strings. */
         that++, other++, attach++;
    int main(  ) {
      /* All strings must be big enough to do the copy. */
       char s3[]                = "GHI";
       char s1[ sizeof s3 * 2 ] = "ABC";
       char s2[ sizeof s1 ]     = "DEF";
       func( s1, s2, s3 );
       printf( "%s %s %s\n", s1, s2, s3 );
       return 0;
    Now for the obvious question, how come s1, s2, and s3 are still pointing to the front of the string? Well as pointers, that, other, and attach simply contain the address of their respective string objects. This allows them to operate on the strings indirectly, without actually moving the front of the string which is located in main( ). The power of pointers, as it were.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Random Numbers...Problem With FAQ Answer
    By sitestem in forum C++ Programming
    Replies: 12
    Last Post: 04-14-2004, 09:22 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM