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

1
  • 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). Commented Feb 17, 2020 at 14:04

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

16 Comments

@MatthewWatson In C a comma separated expression is an expression with the comma operator. There are no jagged arrays in C.
@MatthewWatson Do you know that C and C# are different languages?
Ah, the original post had the C# tag, which has since been removed! It still had that tag when I started typing my comment.
@VladfromMoscow - There are several ways, including compound literals to implement jagged arrays in C.
@ryyker Com;pound literals have nothing common with jagged arrays. In C there is no jagged arrays.
|
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"

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.