2

example

char names[2][5] = {"john","boy"};

I want to ask about char 2 dimension array in c, in [2] i mean, it's explaining about total element of array, and [5] i mean, it's explaining about length of character in array

So, is it true how to use array 2 dimension in c ?

Sorry for my bad english

alk
  • 69,737
  • 10
  • 105
  • 255
Agung kusuma
  • 49
  • 1
  • 5
  • The number of pairs of square brackets is the "dimension" of the array; the number *inside* the brackets are the sizes in each of the dimensions. The `[2]` does not make it two-dimensional, the fact that `[2][5]` has two pairs of brackets does. For example `int foo[2][2][2]` is a three-dimensional array, where each size is 2 (so it's a cube of 2 * 2 * 2 = 8 elements). – unwind Feb 17 '20 at 14:04

4 Answers4

3

names is an array of two elements, each element is in turn an array of five elements.

And you use each array like you would any other. So the firs element of names is names[0], the second is names[1]. Then the first element of names[0] is names[0][0], the second names[0][1], etc.

Also, because each element of names is an array, and each of those two arrays are initialized as null-terminated strings, you can use names[0] and names[1] like any other string.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

A multidimensional array is an array elements of which are in turn arrays.

Let's consider your declaration

char names[2][5] = {"john","boy"};

You can introduce a type alias like

typedef char T[5];

so the name T is an alias for the type char[5].

Now your initial declaration may be rewritten like

T names[2] = {"john","boy"};

That is you have an array of two elements which of them is in turn an array of the type char[5].

String literals are also have types of character arrays. For example the string literal "John" can be represented like

char john[5] = { 'J', 'o', 'h', 'n', '\0' };

elements of a string literal are used to initialize a corresponding character array.

So you array is initialized like

char names[2][5] = { { 'J', 'o', 'h', 'n', '\0' }, { 'b', 'o', 'y', '\0', '\0' } };

If a string literal contains less elements than the number of elements of the initialized array then all other elements of the array that have no an explicit initializer will be zero-initialized.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • @MatthewWatson In C a comma separated expression is an expression with the comma operator. There are no jagged arrays in C. – Vlad from Moscow Feb 17 '20 at 11:31
  • @MatthewWatson Do you know that C and C# are different languages? – Vlad from Moscow Feb 17 '20 at 11:33
  • Ah, the original post had the C# tag, which has since been removed! It still had that tag when I started typing my comment. – Matthew Watson Feb 17 '20 at 11:34
  • @VladfromMoscow - There are [several ways](https://stackoverflow.com/questions/1083658/do-jagged-arrays-exist-in-c-c), including [compound literals](https://gcc.gnu.org/onlinedocs/gcc-4.0.4/gcc/Compound-Literals.html) to implement jagged arrays in C. – ryyker Feb 17 '20 at 13:07
  • @ryyker Com;pound literals have nothing common with jagged arrays. In C there is no jagged arrays. – Vlad from Moscow Feb 17 '20 at 13:15
  • @VladfromMoscow: ryyker’s comment does not say standard C provides jagged arrays as a fundamental type. It says jagged arrays can be implemented in C, which is true, and that it can be done using compound literals, which is true. In this answer, you refer to “multidimensional arrays”, but standard C does not provide multidimensional arrays as a fundamental type. They are implemented as a compound construction. So it is no more right or wrong to talk about jagged arrays in C than it is to talk about multidimensional arrays in C. – Eric Postpischil Feb 17 '20 at 13:22
  • @EricPostpischil One more there is no jagged array in C. That is all. – Vlad from Moscow Feb 17 '20 at 13:36
  • There are no multidimensional arrays in C. – Eric Postpischil Feb 17 '20 at 13:42
  • @EricPostpischil This ais a totally wrong statement. For example from the C Standard "Successive subscript operators designate an element of a multidimensional array object...." Pay attention to the words multidimensional array. – Vlad from Moscow Feb 17 '20 at 13:45
  • That text just tells you how to implement multidimensional arrays. It does not make multidimensional arrays a fundamental type in C. Keeping or removing that text would not change anything about the effects of the C standard, and you could also add or not add text that says “an array of pointers to the first elements of arrays constitutes a jagged array”, and it would have the same effect—none except to inform the reader. As I wrote, ryyker’s comment does not say jagged arrays are a fundamental type in C. It says they can be implemented in C, and that is a true statement. – Eric Postpischil Feb 17 '20 at 13:49
  • @EricPostpischil In the C Standard a multidimensional array is defined very clear as an array elements of which has an array type. Moreover all elements have the same array type. So if you are considering arrays as fundamental types then a multidimensional array is the same array. There is no restriction on the type of array elements except tthat it can be a functional type. You can apply the sizeof operator to get the size of an object of array type that is of a multidimensional array. – Vlad from Moscow Feb 17 '20 at 15:30
  • @EricPostpischil Neither compound literals nor multidimensional arrays are jagged arrays. You can not apply the sizeof operator for an object of your imagined jagged array and get its size. You can emulate a jagged array but this has nothing common with arrays in C. – Vlad from Moscow Feb 17 '20 at 15:30
  • @EricPostpischil And you should decide either arrays are fundamental types as you wrote and as a result a multidimensional array also a fundamental type because it is just an array. Or you should exclude arrays from fundamental types.:) – Vlad from Moscow Feb 17 '20 at 15:33
  • @VladfromMoscow: Nobody said compound literals or multidimensional arrays are jagged arrays. Whether `sizeof` can be applied to a thing to get its size is irrelevant to whether the thing exists. (We can implement linked lists in C, but `sizeof` tells nothing about their sizes.) An emulation of a jagged array is an implementation of a jagged array. The C standard does not define “multidimensional array”; C 2018 3. 3 says definitions appear in italic type or on the left side of a syntax rule, which is not the case in 6.5.2.1 2. A multidimensional array is not “just an array.” – Eric Postpischil Feb 17 '20 at 15:36
  • @EricPostpischil You do not understand. Applying the sizeof operator to an object means that it has the corresponding type and the size of objects of the type is well-known and can be calculated at compile time. Th C Standard uses the term multidimensional array as an array of arrays. It is just an array as other arrays but elements of which can be accessed using several subscript operators. So the statement that multidimensional arrays are not fundamentals type while arrays a re fundamental types does not make any sense. – Vlad from Moscow Feb 17 '20 at 15:42
  • @VladfromMoscow: The fact that `sizeof` works on a multidimensional array demonstrates it is an object. It does not demonstrate it is a fundamental type. It is in fact a type that is compound, not fundamental. In any case, you are arguing *non sequiturs*. Nobody claims jagged arrays have any special place in the C standard, just that they can in fact be implemented in C code. – Eric Postpischil Feb 17 '20 at 15:48
0

Consider a table of 2 * 5, where you get 2 rows, each of 5 columns. Here 2 dimensional array contains 2 rows i.e 2 char*. Each row is of size 5

enter image description here

Outputs:

names[0][0] = 'j'
names[1][0] = 'b'
name[0] = "john"
name[1] = "boy"
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
0

char names[2][5] = {"john","boy"};

names is a multi-dimensional char array with the height of 2 and the width 5:

 __________________________________________________________________
│    j      │    o       │    h       │    n        │     \0       │  // 5 char elements, 1.dimension
│names[0][0]│ names[0][1]│ names[0][2]│ names[0][3] │ names[0][4]  │              
│___________│____________│____________│_____________│______________│      
│    b      │    o       │    y       │    \0       │     \0       │  // 5 char elements, 2.dimension
│names[1][0]│ names[1][1]│ names[1][2]│ names[1][3] │ names[1][4]  │              
│___________│____________│____________│_____________│______________│

Sorry, for the simple example but this might help you.