You are on page 1of 24

Array and Pointer

• Fixed memory declaration


– 1D array
– 2D array
• Memory occupied by different data types
• Pointer variable (*)
• Address operator (&)
• Dynamic memory declaration

Array: is used to record variables declared as an array. When a set of variables


of the same data type are used, they can be declared as an array. The
advantage of variables declared as an array is that the combination of
array and loop can enhance the efficiency of the development of codes.
1D array
All variables are of the same data type
• int score[10]; The index starts from 0.
– score[0] ~ score[9] 10 variables.
Note: score[10] is undefined.
• double value[5];
– value[0] ~ value[4]
• float average[100];
– average[0] ~ average[99]
The memory used by A[120000] is
• Fixed memory declaration: released

– when an array is declared, the memory is always occupied until


the program exits the function where the array resides.
Array operation
• score[0]=1;
• score[1]=2;
• score[3]=score[1]+score[2];
• i=score[0];

Represent each individual variable

The number can be replaced by an index, e.g.


i=2, score[i]: represent score[2]

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

– num[0][0] num[0][1] num[0][2] num[0][3]


– num[1][0] num[1][1] num[1][2] num[1][3] 12 elements
– num[2][0] num[2][1] num[2][2] num[2][3]

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

The variable “score[i]” is used


Declare an array and assign values
Example 2 simultaneously
Example 3
Declare and assign values for a char array

Example 4 Declaration of 2D array

The use of 2D array


Fixed memory declaration:
check maximum number of elements

Max. number allowed is about 120000. If it is


increased to 130000, the program crashes

Two large arrays are declared.


The program still crashes

Therefore, for the same function,


the max. number of memory allowed
is limited.
Pointer variable(*)
• When a variable is declared, the system will allocate a
certain memory to record the content of this variable
• The content can be two types: value and address
• Value : is the content of the variable, such as integer,
real, character, string, etc.
• Address: is a set of hexahedral number, indicating the
address of the memory

*&
• In general, when we declare a variable, the memory
allocated is used to save the value of that variable, e.g.

- a=5;//Put the value 5 into the memory of a


- b=a; //Put the value of a into the memory of b
• Sometimes, it is necessary to know the address of the
memory allocated to a variable. Therefore, it is
necessary to get the address of the memory
corresponding to a variable.
& and *
• &(Address operator) : is used to get the
address of the memory allocated to a
variable
• *(Pointer variable) : Assign a variable
as a pointer variable. When a variable is
assigned as a pointer variable, its memory
is sued to save “address”, instead of
“value”.
& explanation

• 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

• Data type that can be declared as pointer


variable: char, int, double, float
• Application of *: dynamic memory declaration
Dynamic memory declaration
• int A[10000]: is fixed memory declaration. It is declared inside a function. During the
execution of the function, the memory cannot be released. It can be released only
when the execution of the function is finished.
• Dynamic memory declaration: can declare and release memory in accordance with
the need. The memory can be released even if a function is executed.

xxxx
32 bits
xxxx (1) Declare array
u
64*num Occupy less memory

u[0]=2 double *u; //declare (1)


int num; (2) Memory allocation
u[2]=20
Allocate appropriate
u[99999] num=10000; memory

u = new double[num]; (2) (3) Release memory


… Release current
delete [ ] u; (3) memory

Used repeatedly
Dynamic memory declaration:
check the memory used

388 M 388 M

465 M 465 M

465 M
388 M 465 M

Exit the function 465 M


Example for 1D array declaration
Example for 2D array declaration
xxxx
yyyy Allocate 32 bits to u

Allocate 32*10000/8 bytes.


Save the initial address to the 1 st
388 M layer of u
yyyy
zzz1 zzz1
zzz2
zzz3

771 M zzz2

771 M Release 2nd layer


zzz3
Release 1st layer

391 M
Allocate 64*5000/8 bytes
Save the initial address to the 2 nd
388 M layer of u

You might also like