Thread: Two Dimensional Arrays (What does this do?)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    Two Dimensional Arrays (What does this do?)

    Code:
    char str[40][40];
        int number = 7;
    
        for (int i=0;i<number;i++){
            printf("enter %d th string::",i+1);
            scanf("%s",str[i]);
        }
    Above, I have a little snippet of a code that I'm trying to figure out. I don't really understand how to implement 2d arrays in C that well. But, I mostly want to know is how and where the strings are being stored, especially with the code below.

    Code:
    for (int i=0;i<number;i++){
            printf("enter %d th string::",i+1);
            scanf("%s",str[i]);
        }
    I know that it's asking the user to enter strings, which will be stored into the 2d array. I just don't understand how it's being stored. Will it be placed in the 1st column or 2nd row or something?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    
    int main(void) {
       int i;
       char s[4][50]={
       {"I love it's gentle warble"},
       {"I love it's gentle flow"},
       {"I love to wind my tongue up"},
       {"and then to let it go"}  //**
       };
    
       
       for(i=0;i<4;i++)
          printf("%s\n",s[i]);
       //s[i] is used here, as a pointer to each row's first location
       //within the array. This shows the "array in an array" feature
       //of a 2D array, in C.
    
       return 0;
    }
    **Martha Mitchell's high school yearbook quote she identified with. How very true!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two dimensional arrays help
    By lauren in forum C Programming
    Replies: 3
    Last Post: 04-04-2012, 10:41 AM
  2. Two Dimensional Arrays
    By PritinTheGreat in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2008, 04:52 AM
  3. Two dimensional arrays
    By Masschino in forum C Programming
    Replies: 9
    Last Post: 05-18-2004, 08:17 PM
  4. dimensional arrays
    By ZakkWylde969 in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2003, 04:49 PM
  5. two-dimensional arrays
    By jblea in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2002, 06:30 AM