-5

So the title says it all really. I've just fixed a piece of code simply by changing char variable[51] to char variable[51][51]. I understand that the first version means that the user can only enter up to 50 characters into the variable (assume that im using scanf to save an input to the variable). However, what does the second box do in the second version? Google searches aren't helping, however i'm assuming that in the second version, the second [] means that the variable can take up to 50 different strings. If anyone can confirm this, or correct me if i'm wrong, that'd be great. Thanks!

  • `[]` is array. `[][]` is array of arrays. – Eugene Sh. Oct 29 '15 at 17:21
  • 2
    Google "two-dimensional arrays". – Ken White Oct 29 '15 at 17:21
  • Yes you are right. Now search about "Multidimensional Arrays". – haccks Oct 29 '15 at 17:21
  • 2
    @Charley1601 " I've just fixed a piece of code simply by changing char variable[51] to char variable[51][51]. ...However, what does the second box do in the second version?" Why did you think that you fixed the code if you do not understand what you did?:) – Vlad from Moscow Oct 29 '15 at 17:25
  • 1
    Possible duplicate of [How do I use arrays in C++?](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c) – default Oct 29 '15 at 17:30
  • @VladfromMoscow my code was previously looping a number of times (set by the user), but only printing the last thing that was input by the user. i don't think the user's different inputs were being saved into an array, so it was obviously because i was using the wrong type of array. i had an idea that an extra array would solve it, but was 100% since im fairly new to C :L – Charley1601 Oct 29 '15 at 17:39

4 Answers4

2

The expression char variable[51][51]; means that variable is an array of 51 arrays, each of which contains 51 characters.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1
char variable[51]           // 1-d character array 

This a array of char . It can contain 51 characters .

char variable[51][51]        // 2-d  character array

This declares array of 51 arrays which can contain 51 characters.

If you intend to use them as string (or pass to string manipulation functions) then -

1. First one can contain upto 50 characters.

2. Second will have 51 strings of 50 characters each .

Note- One space is left for null character.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
1

Actually, char var[51] will hold 51 characters that can be indexed from 0 to 50.

Now if a second set of brackets is added, it means that char var[51][51] can now represent a 2-dimensional array or square matrix of 51 rows by 51 columns.

dspfnder
  • 1,135
  • 1
  • 8
  • 13
  • 1
    Not really. There are 51 contigous blocks of 51 characters laid out in memoy. They are no "columns". You can _address_ them as rows and columns in C as `var[row][col]`. – Paul Ogilvie Oct 29 '15 at 18:57
  • Right, I used 'represent' hoping it would've made it clear that var[][] is only a representation of a matrix and now how the data is stored. – dspfnder Oct 29 '15 at 19:07
0

char variable[51] is an array of 51 chars.

char variable[51][51] is an array of 51 arrays of 51 chars (AKA a two-dimensional array with 51 rows and 51 columns, but that isn't entirely correct). That means that each element in the array is itself an array.

Your compiler would store them in sequence, so by getting the user input into variable, it would just get into the next elements (i.e., the next arrays) in case it contains more than 51 characters. And when you print variable, it would seem to be correct, though it's actually taking more than 1 element in the array.