Introduction to C language Learning (iii) sizeof function, memory address, array

Source: Internet
Author: User

sizeof can be used to calculate the number of bytes of memory that a variable or a constant, a data type occupies.

sizeof(ten);

char c = ' a ';

sizeof(c);

sizeof(float);

Address

1. The memory in the computer is the storage space in bytes . Each byte of memory has a unique number, which is called an address .

Every program and data stored in memory has an address, that is, a function has its own memory address

2. When a variable is defined, the system allocates a storage unit with a unique address to store the variable. Like what:

Char A = ' A ' ; The ASCII value of a is 65

int b[] = {66,33};

In the 16bit compiler environment, the system allocates 1 bytes, 2 bytes of storage unit for a and B respectively. The address of the first byte of a variable storage unit is the address of the variable .


As you can see, the address of variable A is FFC3, and the address of variable B is ffc1. 2 binary data is stored in memory

One-dimensional arrays

The form of the definition is: type array name [number of elements]

int a[5];

int [] A; It is wrong to write differently than Java writes before or after a variable name, and defines the number of elements to be specified.

Initialization

int a[2] = {8,ten};

is actually equivalent to:

int a[2];

a[0] = 8;

a[1] = ten;

The element value list can be the initial value of all elements of an array, or it can be the initial value of a previous element

int a[4] = {2,5};  This assigns the value to the a[0]=2;a[1]=5; a[2]=a[3]= Default value 0


You can not specify the number of elements when defining and initializing directly

int a[] = {2,3,5};

Array initialization can only be used for the definition of the array, after the definition can only one element of an element to be assigned value

int a[3];

a = {1,2,3};  Error. Because the array name a represents the address of a variable, it is a constant, assigning a value to a constant is wrong.


When an array is an argument, because an address is passed, the parameter changes and the argument changes

Two-dimensional arrays

Definition form: type array name [number of rows] [number of columns]

int a[2][3]; 2 rows, 3 columns, 6 elements, two one-dimensional array with three elements


In-memory storage, such as int a[2][2]


( Note:a[0], a[1] is also an array, is a one-dimensional array, and a[0], a[1] is the name of the array, so a[0], a[1] represents the address of the one-dimensional array )

The address of the 1> array A is FFC1, and the address of the array a[0] is also ffc1, i.e. a = a[0];

The address of the 2> element a[0][0] is FFC1, so the address of the array a[0] is the same as the address of the element a[0][0], i.e. a[0] = &a[0][0];

3> can finally come to a conclusion: a = a[0] = &a[0][0], and so on, can be derived a[1] = &a[1][0]

printf ("%p\n%p\n%p", A, a[0], &a[0] [0]);

Initialization of two-dimensional arrays

* Initialize by line

int a[2[3] = {{2,2,3}, {3,4,5}};

* Initialize in order of storage (1th row, then 2nd row)

int a[2[3] = {2,2,3,3,4,5};

* Initialization of some elements

int  a[2 ][3 ] = {{2 }, {3 , 4 }};

int b[3[3] = {{}, {,,2}, {1,2,3}};

*  if only part of the element is initialized, the number of rows can be omitted, but the number of columns cannot be omitted

int  a[][3 ] = {1 , 2 , 3 , 4 , 5 , 6 };

int a[][3] = {{1,2,3}, {3,5}, {}};

Why not just omit the number of columns? Because the int a[2][]={1,2,3,4,5,6} does not determine which row the element is in.

Introduction to C language Learning (iii) sizeof function, memory address, array

Related Article

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.