Computer And Programming
Array and Pointer
Array and Pointer
• Array provides a means to allocating and accessing memory.
• Pointers, on the other hand, provides a way to accessing memory that has been allocated by other means, often as an array
2
One Dimensional Array
• A (one-dimensional) array is a series of objects, all of which have the same type.
• An array is a block of memory made up of memory locations of a fixed size.
– declares a as an array of five int objects
– declares x as an array of 100 long double objects
int a[5];
3
long double x[100];
Array Initialization
• We can initialize an array when it is declared by listing the initial values for its elements, as follows:
• Array with five elements has subscript ranging from 0-4. • The values assigned to the array a can be illustrated in
figure below:
int a[5] = {75, 25, 100, -45, 60};
4
75
25
100
-45
60
a[0]
a[1]
a[2]
a[3]
a[4]
Accessing Array
• Subscripted array can be used like variable names, such as x and y. For example:
– assigns 5 to element 2 (the third element) of a,
– sets n to the difference of elements i and j, and
– increments the value of element 2*i-j. – (see postfix and prefix operations)
a[2] = 5;
5
n = a[i] – a[j];
a[2*i – j]++; or ++a[2*i – j];
Declaring Array with Constant
• Constants represented by macros are frequently used in declaring and manipulating arrays.
• For example, if COUNT is defined by
– then
– declares c as an array of 1000 int objects.
#define COUNT 1000
6
int c[COUNT];
Array and Loops
• An example of setting all elements to 0 is given below
• An equivalent implementation is also given as:
for(i = 0; i < COUNT; i++) c[i] = 0;
7
int i = 0;while(i < COUNT) c[i++] = 0;
Pointers
• A pointer is the address of an object of a particular type.
• An address only locates the first byte of an object.
• We need the type because it can determine the size of the object and the meaning of the bits stored in it.
• Typical pointer types are pointer-to-int, pointer-to-long, pointer-to-double, and so on.
8
Indirection Pointer
• If p is a variable with type pointer-to-int, the value of p is the address of an int object.
• Thus, *p is an lvalue of an object that is pointed by p.
9
75
25
100
-45
60
p
*p
75
p
P is a pointer to an object. The value of p is the address of the object. Thus, the value of *p is the value of the object pointed by pointer p.
int *p; // Declare p as a pointer-to-int type
*p = 20; // assigns 20 to the *p. And
printf(“%d”, *p); // prints the value 20, and
n = *p + 10; // assigns 35 to n,
*p += 7; , which is equivalent to *p = *p +7;// adds 7 to the *p, then stores the result to *p,
whereas (*p)++; // increments the value of the target object.
Example of Using a Pointer
10
• We can use the address-of operator to compute the address of any object designated by an lvalue
• If we declare
– then &n has type pointer-to-int and &x has type pointer-to-double.
• If we now declare
– then p is initialized to point to n.
• Thus, *p and n name the same object– They are said to be aliases
Using the Address-of Operator
int n = 100;double x = 3.5;
11
int *p = &n;
Using const in Pointer Declaration
• In pointer, const can be used in two difference places.
• If const is placed at the beginning of the declaration, the target object is declared constant.
• In this case, value of target object *p cannot be changed.
• Thus the following statements are invalid.
• if m and n are integer, then following statements can be applied to p.
const int *p;
12
*p = 100; and (*p)--;
*p = 100; and (*p)--;
Using const in Pointer Declaration (cont)
• If const precedes the pointer variable, then the value of p (address of which p points to) now becomes constant
• The following statements are not allowed.
• However, these statements can be applied to p.
13
int *const p = &m;
p = &n; and p++;
*p = 25; *p += 3; (*p)++; (*p)--;
Pointers and Arrays
• We can use the name of an array to initialize a pointer to the address of the first element of the array.
• Both p and q point to the first element of list.
• C allows the subscript operator to be applied to pointer as well as array names. – It is legitimate to use p[0] to refer to the first element of list.– It is also possible to refer to the 5th element of list by using p[4]
int list[100]; int *p = list; int *q; q = list;
14
Pointers and Arrays (cont)
• So, we can use
• It can also be written as
sum = 0;for(i = 0; i < 100; i++)
sum += list[i];
15
sum = 0;for(i = 0; i < 100; i++)
sum += p[i];
A pointer need not always point to the first element of an array.We can set p to point to element 50 of list :
p = &list[50];
Pointers and Arrays Example
• A pointer need not always point to the first element of an array.
• We can set p to point to element 50 of list :
p = &list[50];
16
p
list[48], p[-2]
list[49], p[-1]
list[50], p[0]
list[51], p[1]
list[52], p[2]
75
25
100
-45
60
list
(Keep in mind, p must point to a valid location of the target object.)
String
• Strings are stored in memory as arrays of char values.
• The null character ('\0') is used to indicate the end of string
• We can check it by trying this statement
– Computer will print
printf(“abcd\0efgh”);
17
abcd
String Declaration
• We can declare an array of characters to hold a string
– This will make a string containing up to 3 characters– The number of charatered used will be one less than the
declaration because it needs tell the termination of string (null)
• String initilization could be as follow:
– It can also be written as
char s[4];
18
char s[4] = “dog”;
char s[4] = { ‘d’, ‘o’, ‘g’, ‘\0’ };
String Declaration (cont)
• We can also leave it to compiler to determine its size
char s[] = “dog”;
19
s[0]
s[1]
s[3]
'd'
'o'
'g'
'\0'
s[2]
s
String Declaration (cont)
• If we declaration statement as
– it will result in
• We can print it using
20
char s[10] = “dog”;
s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8]
s[9]
'd' 'o' 'g' '\0'
Unused
printf(s);
or
printfs(“%s”, s);
String Pointer
• We can assign pointer-to-char to array of character(string).
• The initialization can be as follow:
• We can then print it likewise:
char *p = s;
21
char *p = “dog”;
printf(p);
or
printf(“%s”, p);
Top Related