Two Dimensional Arrays (What does this do?)

This is a discussion on Two Dimensional Arrays (What does this do?) within the C Programming forums, part of the General Programming Boards category; 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 ...

  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, 11:41 AM
  2. Two Dimensional Arrays
    By PritinTheGreat in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2008, 05:52 AM
  3. Two dimensional arrays
    By Masschino in forum C Programming
    Replies: 9
    Last Post: 05-18-2004, 09:17 PM
  4. dimensional arrays
    By ZakkWylde969 in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2003, 05:49 PM
  5. two-dimensional arrays
    By jblea in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2002, 06:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21