The document discusses the relationship between arrays and pointers in C programming. It explains that arrays store a consecutive series of variables under a single name, and each element of the array has its own unique memory address. Pointers can be used to reference array elements, where the array name represents the base address of the array and pointer arithmetic allows accessing elements. It provides examples of declaring and initializing arrays, accessing elements using pointers, and how pointers can be incremented or decremented to traverse the array.
2. 2
One-Dimensional Arrays
Array
– A consecutive series of variables that share one name
– 5 consecutive variables of integer type under the name of num
int num[5] ;
num
1000 1004 1008 1012 1016 Memory addresses
3. 3
The Relationship between Arrays and
Pointers
Example :
– Which address does each element have?
int num[5] ;
num
1000 1004 1008 1012 1016
num[0] num[1] num[2] num[3] num[4]
&num[0] == 1000
&num[1] == 1004
&num[2] == 1008
&num[3] == 1012
&num[4] == 1016
Memory addresses
4. 4
The Relationship between Arrays and
Pointers
Example : What is num?
– num is the constant pointer of which value is the start address of
the array.
int num[5] ; num
1000 1004 1008 1012 1016
num[0] num[1] num[2] num[3] num[4]
num == &num[0] == 1000
Memory addresses
5. 5
The Relationship between Arrays and
Pointers
Example : Arithmetic of pointers
– “pointer + 1” does not mean increasing pointer by 1.
– “pointer + 1” is “the address of the next element”.
– “pointer – 1” is “the address of the prior element”.
int num[5] ;
1000 1004 1008 1012 1016
num[0] num[1] num[2] num[3] num[4]
num == &num[0] == 1000
(num+0) == ??
(num+1) == ??
(num+2) == ??
(num+3) == ??
(num+4) == ??
&num[0]
&num[1]
&num[2]
&num[3]
&num[4]
Memory addresses
7. 7
Pointer Arithmetic and Element Size
Adding an Integer to a Pointer
[Ex]
p = &a[2];
q = p + 3;
p += 6;
0 1 2 3 4 5 6 7 8 9
p
a
0 1 2 3 4 5 6 7 8 9
p
a
0 1 2 3 4 5 6 7 8 9
p
a
q
q
8. 8
Pointer Arithmetic and Element Size
Subtracting an Integer from a Pointer
[Ex]
p = &a[8];
q = p - 3;
p -= 6;
0 1 2 3 4 5 6 7 8 9
p
a
0 1 2 3 4 5 6 7 8 9
p
a
0 1 2 3 4 5 6 7 8 9
p
a
q
q
9. 9
Pointer Arithmetic and Element Size
Subtracting Pointers
[Ex]
p = &a[5];
q = &a[1];
i = p – q; /* i == 4 */
i = q – p; /* i == -4 */
0 1 2 3 4 5 6 7 8 9
q
a
p
10. 10
Pointer Arithmetic and Element Size
Comparing Pointers
– Relational operators (<, <=, >, >=) can be applied
– Equality operators (==, !=) can be applied
[Ex]
p = &a[5];
q = &a[1];
p <= q; /* result is 0 */
p >= q; /* result is 1 */
11. 11
Pointer Arithmetic and Element Size
Example: Pointer Operation
int a[ ] = { 5,15,25,43,12,1,7,89,32,11}
int *p = &a[1], *q = &a[5] ;
1. *(p + 3) ?
2. *(q - 2) ?
3. q - p ?
4. if ( p > q ) ?
5. if ( *p > *q )?
12. 12
Pointer Arithmetic and Element Size
Example: Pointer Operation
#include <stdio.h>
int main(void)
{
double a[2], *p, *q;
p = &a[0]; /* points at base of array */
q = p + 1; /* equivalent to q = &a[1]; */
printf(“%dn”, q – p );
printf(“%dn”, (int) q – (int) p );
printf(“%dn”, sizeof(double) );
return 0;
}
13. Combining the * and ++ Operators
Combining the * and ++ Operators
– p increased or decreased, then *p performed
– p increased or decreased, then *(p-1) or *(p+1) performed
– Increase or decrease 1 of a variable pointing p
13
*++p *(++p), *--p *(--p)
*p++ *(p++), *p-- *(p--)
(*p)++, (*p)--
14. Combining the * and ++ Operators
Combining the * and ++ Operators
14
void main()
{
int k, a[10], *p = a ;
while( p < &a[10] )
*p++ = 0 ;
}
void main()
{
int k, a[10], *p = a ;
while( p < &a[10] ) {
*p = 0 ;
p = p + 1 ;
}
}
15. Combining the * and ++ Operators
Combining the * and ++ Operators
15
void main()
{
int k, a[10], *p = &a[10] ;
while( p >= &a[0] )
*--p = 0 ;
}
void main()
{
int k, a[10], *p = a ;
while( p >= &a[0] ) {
p = p -1 ;
*p = 0 ;
}
}
16. Example
Add inputted integer numbers
16
#include <stdio.h>
int sum( int num[], int size ) {
int k, sum = 0 ;
for( k = 0 ; k < size ; k++ )
sum += num[k] ;
return sum ;
}
void main() {
int a[100], k ;
for( k = 0 ; k < 100 ; k++ )
scanf( “%d”, &a[k] ) ;
printf( “%dn”, sum(a, 100) ) ;
}
#include <stdio.h>
int sum( int num[], int size ) {
int k, sum = 0 ;
for( k = 0 ; k < size ; k++ )
sum += *num++ ;
return sum ;
}
void main() {
int a[100], k ;
for( k = 0 ; k < 100 ; k++ )
scanf( “%d”, &a[k] ) ;
printf( “%dn”, sum(a, 100) ) ;
}
int num[] and int *num
are the same