Thread: assigning an string of arrays by loop

  1. #16
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by WaterSerpentM View Post
    btw the same thing happens without code 2
    Did you run my version?

    Print the first array before the for() loop, then run the loop starting with the second array.

  2. #17
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    i did run it

    whats the point of printing before the loop?

  3. #18
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by WaterSerpentM View Post
    i did run it

    whats the point of printing before the loop?
    The first line before the loop prints:
    a

    Then in the loop, it prints the remainder of the arrays, correctly:
    aa
    aaa
    aaaa
    aaaaa
    aaaaaa
    aaaaaaa
    aaaaaaaa
    aaaaaaaaa
    aaaaaaaaaa

  4. #19
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    so

    array[0] starts off as nothing, then strcat to 'a'
    then with strcpy, array[1] becomes 'a', then strcat to 'aa', and so on

    ?

  5. #20
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by WaterSerpentM View Post
    so

    array[0] starts off as nothing, then strcat to 'a'
    then with strcpy, array[1] becomes 'a', then strcat to 'aa', and so on

    ?
    It should be quite obvious with the code and the results:

    array[0] is set to "a" using strcpy()
    print array[0]

    for() loop:
    strcpy() is used to copy from arrray[0] to array[1]
    then strcat() is used to append "a" to array[1]
    print array[1]
    etc...

  6. #21
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    yup I get it entirely =]

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    LOL - and I was thinking "must start at i=1" while editing the code before posting it.

    Oh well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    WaterSerpentM:

    The following is an alternative method for displaying what you wanted. It eliminates the need for both strcpy(), and strcat() functions!

    Study it carefully.

    Code:
    #include <stdio.h>
    // string.h no longer needed!
    
    int main(void)
    {
      // The line below will initialize the first array, ba[0] to "a"
      // and initialize all other bytes to Nul bytes ('\0')
      char ba[10][12] = {{"a"}};
      int x = 0;
    
      printf("%s\n",ba[0]);     // Print first array
    
      for(int i = 1;i < 10;i++) // For each of the remaining arrays
      {
        for(x = 0; x <= i; x++) // For each 'a' in the array
        {
          ba[i][x] = 'a';
        }
    
        // The following line is actually redundent, as all the
        // other bytes were Nulled.  But I would allways use it
        // to assure the string is Null terminated, in case the
        // ba array is reused later!
        ba[i][x] = '\0';
    
        printf("%s\n",ba[i]);   // Print next array
      }
    
      return 0;
    }

  9. #24
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    check this out =]

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main() {
    
    
    
    
    
        char chara[10][10];
        int i;
    
    
        for(i=0;i<10;i++){
            strcpy(chara[i],chara[i-1]);
            strcat(chara[i],"a");
        }
    
    
        for(i=9;i>-1;i--){
        printf("%s\n",chara[i]);
        }
    
    
        return 0;
    }

  10. #25
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Code:
    for(i=0;i<10;i++){
        strcpy(chara[i],chara[i-1]);
        strcat(chara[i],"a");
    }
    The first time through the loop, i == 0, and i - 1 IS -1!!!

    I don't understand how this works! It is wrong and at some point it will fail!

    Code:
    for(i=9;i>-1;i--){
      printf("%s\n",chara[i]);
    }
    Better:

    Code:
    for(i=9;i >= 0;i--){
      printf("%s\n",chara[i]);
    }

  11. #26
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    it works, it outputs:

    aaaaaaaaaa
    aaaaaaaaa
    aaaaaaaa
    aaaaaaa
    aaaaaa
    aaaaa
    aaaa
    aaa
    aa
    a

  12. #27
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by WaterSerpentM View Post
    it works, it outputs:

    aaaaaaaaaa
    aaaaaaaaa
    aaaaaaaa
    aaaaaaa
    aaaaaa
    aaaaa
    aaaa
    aaa
    aa
    a
    It may seem to work, but you are accessing an array OUTSIDE of the defined array!!!

    chara[-1] WRONG!!!!!

  13. #28
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    I gave you two different solutions that work, but you keep going back to code that is wrong!

  14. #29
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    that was salem's coding originally,

    and it looks more construct than yours

  15. #30
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by WaterSerpentM View Post
    that was salem's coding originally,

    and it looks more construct than yours
    Salem's code was wrong. She admitted it. I don't know what you mean by "looks more construct".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with assigning character arrays
    By KSG XII in forum C Programming
    Replies: 7
    Last Post: 04-05-2015, 04:04 PM
  2. Replies: 3
    Last Post: 07-01-2014, 07:48 AM
  3. assigning 2D arrays to pointers
    By rakeshkool27 in forum C Programming
    Replies: 5
    Last Post: 01-19-2010, 12:34 AM
  4. trouble assigning arrays
    By shintaro in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2008, 08:31 AM
  5. Assigning values to arrays
    By napkin111 in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2003, 08:52 PM

Tags for this Thread