Professional Documents
Culture Documents
Note: i must be defined and its value must be assigned first before
the appearance of score[i].
Array example
2D array
• int num[3][4]; The 1st dimension is 3, and the 2nd dimension is 4
Note:
1. num[0][4], num[1][4]…num[3][0], …do not exist
2. int num[3][4] and int num[4][3] are two different declaration
• For loop for 2D array
Note:
1. The final element is var[19][19],
instead of var[20][20].
2. The index starts with 0
• double f[100][100]
– f[0][0]… f[0][99]
– …f[99][0] ,,, f[99][99]
Array example
Memory occupied by different
data types
• char: 8 bits, 1 byte
• int 32 bits, 2 bytes
• float 32 bits, 4 bytes
• double 64 bits, 8 bytes
• 1 byte = 8 bits
• Ex:
int t[10]; Memory used: 32*10 (bits)=4*10 (bytes)
double [10000]; Memory used: 64*10000(bits)
=8*10000(bytes)=80000 Bytes
=80 K(Bytes) = 0.08M(Bytes)
Examples
Example 1
Declare an array “score” with 20 elements
*&
• In general, when we declare a variable, the memory
allocated is used to save the value of that variable, e.g.
• fscanf(fp,”%d\n”,&lue);
– &lue: get the address of the memory of lue. Deliver
this address to the function fscanf
– When “fscanf” reads a value from a file, the system
saves that value into the memory of that address.
– The content lue becomes that value
• The function “fscanf” reads a value from a file and
saves it to a particular set of memory. Therefore, it is
necessary to add “&” before lue to get the address of
the memory of lue.
* explanation
• int *pt; declare a variable pt. It is a pointer
variable (the memory of pt will be address)
• int i=123;
• pt=&i;
pt 0x00xxxxxx 0x0064FDE4
0x0064FDE4 123
When pt is declared, the system will When i is declared and assigned.
allocate a set of memory (the address The memory of I is used to save value.
is 0x00xxxxxx). This memory will save
the address of some other memory
xxxx
32 bits
xxxx (1) Declare array
u
64*num Occupy less memory
Used repeatedly
Dynamic memory declaration:
check the memory used
388 M 388 M
465 M 465 M
465 M
388 M 465 M
771 M zzz2
391 M
Allocate 64*5000/8 bytes
Save the initial address to the 2 nd
388 M layer of u