Multidimensional array in C Language

Source: Internet
Author: User

439 reading comments (1)
Collect reports

This series of articles focuses on some vague issues in the C language syntax and some common language skills.

The first statement is: I will give the explanation of C language syntax and skills in the form of source code. These are the basics of C language, practical and important, of course, for the C language veteran, these are all class axe. however, I still think it is helpful for you to improve your language skills, no matter you are a novice or veteran.

In addition, most of the articles are extracted from some typical C language books, but there is no shortage of my feelings. I believe there will be more wonderful things as I study deeply.

Recommended books: <The C programming language >,< pointers on C >,< Expert C Programming >,< C traps and pitfalls>

The article is provided in the form of code reference comments, and the running results are compared.

/* =====================================================
This program is a test program used for learning.
This section describes some learning mistakes in C language.
Detailed explanation of knowledge
======================================================= */
# Include <stdio. h>
# Include <stdlib. h>

Void main ()
{
/* ===================================================== ============
Topic: multi-dimensional array in C Language
Content: The dimension-1 method for defining and referencing multi-dimensional arrays in C language is to use arrays.
Array (that is, the array and multi-dimensional array are the same concept here,
This is not the case in Pascal). In C, arrays are actually considered as a vector.
(Vector) is a one-dimensional array of an object (the array element can be a one-dimensional array ).
And others ).
========================================================== ============= */
/* 1. Break down multi-dimensional arrays and locate array elements in multi-dimensional arrays */
Int data [2] [3] [4] = {1, 2, 3}, {5, 6, 7, 8}, {9, 10, 11, 12 },{ 13, 14, 15, 16 },
{17,18, 19,20 },{ 22 }}};
Printf ("1st test values: % d/N", data [1] [2] [3]); // No error is reported if the array in the test is out of bounds.

// P is a pointer to a two-dimensional array. This must ensure that the dimension behind P is the same as that behind data.
INT (* P) [3] [4] = data;
Printf ("2nd test values: % d/N", P [1] [2] [3]);

// R is a pointer to a one-dimensional array. It can point to the first or second one-dimensional array in data (regard the data element as a one-dimensional array)
INT (* r) [4] = data [1];
Printf ("3rd test values: % d/N", R [1] [2]);

// T is an int pointer that points to the first element of the third dimension in data (the third dimension is arbitrary)
Int * t = data [0] [0];
Printf ("4th test values: % d/N", t [3]);

// The increment of the array dimensions pointed to by the pointer above varies greatly.
Printf ("5th test values: % x/N", R );
R ++;
Printf ("6th test values: % x/N", R );
Printf ("7th test values: % x/N", t );
T ++;
Printf ("8th test values: % x/N", t );

/* 2. initialize a multi-dimensional array */
// An example of how to initialize a multi-dimensional array is mentioned above.
Int data1 [] [3] = {1, 2, 3}, {4, 5}; // If the dimension is greater than the number of initial values, the value is converted to 0.
Printf ("1st test values: % d/N", data1 [1] [2]);

/* 3. Create a two-dimensional string array */
Char fruit [] [9] = {"apple", // method 1
"Orange ",
"EGE ",
"Banana "};
Printf ("1st test values: % s/n", fruit [1]);

Char * anima [] = {"tiger", // method 2
"Lion ",
"Chick ",
"Elephont "};

Printf ("1st test values: % s/n", anima [1]);
}

The running result is as follows:

The 1st test values are as follows: 24

The 2nd test values are as follows: 24

3rd test values: 19

The 4th test values are as follows: 4

The 5th test values are: 12ff50.

The 6th test values are: 12ff60.

The 7th test values are: 12ff20

The 8th test values are: 12ff24

The 1st test values are: 0.

The 1st test values are orange.

The 1st test values are: Lion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.