Thread: assigning an string of arrays by loop

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    78

    assigning an string of arrays by loop

    the idea is to assign an array of string, for example:

    a[0] = a
    a[1] = aa
    a[2] = aaa
    a[3] = aaaa
    a[4] = aaaaa

    but assigned and outputted in a for loop:

    Code:
    printf("%s\n", ba[i]);

    at the moment, i have something like this
    Code:
        char ba[10];
    
    
        char first[] = "first";
        char second[] = "second";
    
    
    
        for(i=0;i<10;i++){
            strcat(second,first);
                ba[i] = second;
                printf("%s\n", ba[i]);
    
    
      }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Print the result of this?
    Code:
    char ba[10][12];
    strcpy(ba[0],"a");
    for(int i=0;i<10;i++) {
        strcpy(ba[i],ba[i-1]);
        strcat(ba[i],"a");
    }
    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.

  3. #3
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    thanks =]

  4. #4
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    that outputs:

    ·⌂a
    ·⌂aa
    ·⌂aaa
    ·⌂aaaa
    ·⌂aaaaa
    ·⌂aaaaaa
    ·⌂aaaaaaa
    ·⌂aaaaaaaa
    ·⌂aaaaaaaaa
    ·⌂aaaaaaaaaa

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Why don't you try thinking for yourself and figuring out the small mistake in Salem's code.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    Code:
    char ba[10][12];
    strcpy(ba[0],"a");
    for(int i=0;i<10;i++) {
        strcpy(ba[i],ba[i-1]);
        strcat(ba[i],"a");
        printf("%s\n",ba[i]);
    }
    =]

    a
    aa
    aaa
    aaaa
    aaaaa
    aaaaaa
    aaaaaaa
    aaaaaaaa
    aaaaaaaaa
    aaaaaaaaaa

  7. #7
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    thanks for the code, seeing that it works is one thing, i would like to know how one of the code means, can someone explain this code for me?

    Code:
    strcpy(ba[i],ba[i-1]);

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by WaterSerpentM View Post
    Code:
    char ba[10][12];
    strcpy(ba[0],"a");
    for(int i=0;i<10;i++) {
        strcpy(ba[i],ba[i-1]);
        strcat(ba[i],"a");
        printf("%s\n",ba[i]);
    }
    On line 4, the first time through the loop, what is the value of i and what is the value of i-1???

    strcpy() copies the string in the previous array to the current array, then the next line appends the string "a" to the current array.

    You have a problem with the first time through the loop!
    Last edited by rstanley; 1 Week Ago at 06:26 PM.

  9. #9
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    im a bit confused, are they both 'ba' array? the 10 and 12 one?

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by WaterSerpentM View Post
    im a bit confused, are they both 'ba' array? the 10 and 12 one?
    ba is a two dimensional array. 10 arrays of 12 chars each. Think in terms of a spreadsheet of 10 rows of 12 fields.

  11. #11
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    ok so,

    on the first loop the first i is 0 and the second i is -2

    ?
    Last edited by WaterSerpentM; 1 Week Ago at 06:34 PM.

  12. #12
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Why would you assume that?

    "for(int i=0;i<10;i++)"

    Wouldn't i be 0 the first time? In that case, what is i-1?

    i-1 is before the start of the array!

  13. #13
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    soz i edited just before you posted

    'ok so,

    on the first loop the first i is 0 and the second i is -1

    ?'

  14. #14
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by WaterSerpentM View Post
    soz i edited just before you posted

    'ok so,

    on the first loop the first i is 0 and the second i is -1

    ?'
    Yes.

    Normally, for loops are written as
    Code:
    for(int i = 0, i < 10; i++)
    But in this case, you might write it as:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    
      char ba[10][12];
      strcpy(ba[0],"a");
      printf("%s\n",ba[0]);
    
      for(int i=1;i<10;i++)
      {
        strcpy(ba[i],ba[i-1]);
        strcat(ba[i],"a");
        printf("%s\n",ba[i]);
      }
        return 0;
    }

  15. #15
    Registered User
    Join Date
    Jul 2022
    Posts
    78
    btw the same thing happens without code 2

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