Chap. 5 Pointers and Arrays 5.1 Pointers and Adresses 5.2 Pointers and Function Arguments 5.3 Pointers and Arrays 5.4 Address Arithmetic 5.5 Character Pointers and Functions 5.6 Pointer Arrays; Pointers to Pointers 5.7 Multidimensional.

Download Report

Transcript Chap. 5 Pointers and Arrays 5.1 Pointers and Adresses 5.2 Pointers and Function Arguments 5.3 Pointers and Arrays 5.4 Address Arithmetic 5.5 Character Pointers and Functions 5.6 Pointer Arrays; Pointers to Pointers 5.7 Multidimensional.

Chap. 5 Pointers and Arrays
5.1
Pointers and Adresses
5.2
Pointers and Function Arguments
5.3
Pointers and Arrays
5.4
Address Arithmetic
5.5
Character Pointers and Functions
5.6
Pointer Arrays; Pointers to Pointers
5.7
Multidimensional Arrays
5.8
Initialization of Pointer Arrays
5.9
Pointers vs Multi-dimensional Arrays
5.10
Command-line Argument
5.11
Pointers to Functions
5.12
Complicated Declarations
Session 5 - 15 April 2003
Session 6 - 29 April 2003
0
5.1 Pointers and Addresses
Pointer
– A pointer is a variable that contains
the address of a variable
Address Operator &
– &x is the address of the variable x
Indirection or Dereferencing Operator *
– *p access the object the pointer p
points to
A first example
main()
{
int x = 1;
int *p;
p = &x;
printf(“%d”, *p);
variables
name
value
addresses
value
name
0000
…
*p
x
1
F000
&x
FF00
&p
…
p
F000
…
FFFF
A linear address memory model
}
1
A Mystery Program
#include <stdio.h>
main()
{
int i = 1;
int *p;
int **q;
p = &i;
q = &p;
printf(" i = %d
printf(" *p = %d
printf("**q = %d
p = %X
&i = %X
*q = %X
printf("\n");
printf("*&*&*&*&i = %d
printf("*&*&*&*&*p = %d
printf("\nmystery: %d %p
p += 3;
printf(" %p\r\n", p);
q = %x\r\n",
&p = %x\r\n",
i, p, q);
*p, &i, &p);
&q = %x\r\n", **q, *q, &q);
&*&*&*&i = %p\r\n", *&*&*&*&i , &*&*&*&i );
&*&*&*&*p = %p\r\n", *&*&*&*&*p, &*&*&*&*p);
%p
%p", p[0], &p[0], &p[1], &p[2]);
}
2
Output of the Mystery Program
i = 1
*p = 1
**q = 1
p = BFFFFB08
&i = BFFFFB08
*q = BFFFFB08
*&*&*&*&i = 1
*&*&*&*&*p = 1
mystery: 1
q = bffffb0c
&p = bffffb0c
&q = bffffb10
&*&*&*&i = 0xbffffb08
&*&*&*&*p = 0xbffffb08
0xbffffb08
0xbffffb0c
0xbffffb10
0xbffffb14
Simplified
Memory Model
Memory Model
**q
*p
i
1
BFFFFB08
&i
&*p
*q
p
BFFFFB08
BFFFFB0C
&p
&*q
q
BFFFFB0C
BFFFFB10
&q
Reminder
int i=1;
int *p=&i;
int **q=&p;
&**q
i
1
p
q
3
5.2 Pointers and Function Arguments
C passes arguments to functions by value !
void swap(int x, int y)
{/* WRONG */
int temp;
void swap(int *px, int *py)
{/* interchange *px and *py */
int temp;
temp = x;
x = y;
y = temp;
temp = *px;
*px = *py;
*py = temp;
}
}
main()
main()
swap(a,b)
swap(int x, int y)
swap(&a,&b)
x = a;
y = b;
…
As x and y are local variables,
only the copies of a and b
have been swapped !!!
swap(int *px, int *py)
px = &a;
py = &b;
…
a
b
px
The values of a and b
have been swapped
!!!
py
4
5.3 Pointers and Arrays
The declaration int a[4]; defines
an array of size 4, that is, a block of 4
consecutive objects of type int, named
a[0], a[1], a[2], a[3].
a
a[0]
a[1]
a[2]
a+2
a[3]
The name of this array is “a” and its value
is equivalent to the address of its first
element: &a[0].
p+3
p
int a[4];
int *p=a;
a+i points to the i-th element beyond a.
p+i points to the i-th element beyond p.
As p is a variable, p=a or ++p are legal;
but as an array name is not a variable,
expression like a=p or ++a are illegal !
Tricky: p-a is legal, but not p+a !
5
Array as a Parameter of a Function
When an array name is passed to a function, what is passed is the location of the first
element.
Within the called function, this array name is a local pointer variable!
That means, as a formal parameter in a function definition, char s[] and char *s are
equivalent.
// return length of string s
int strlen(char s[])
{
int n;
for (n = 0; *s != '\0'; s++)
n++;
return n;
The following calls will work:
strlen(a); // char a[10];
strlen(p); // char *p;
}
strlen("hello world"); // string constant !!
6
A Test Program
a
#include <stdio.h>
/* Return length of string s */
int strlen(char s[])
{
int n;
for (n = 0; *s != '\0'; s++)
n++;
return n;
}
a
'h'
'h'
'e'
'e'
'l'
'l'
'l'
'l'
'o'
'o'
'\0'
'\0'
p
p
strlen.s
'h'
'e'
main()
{
char a[] = "hello";
char *p = a;
(1)
(2)
(3)
printf("%d\n", strlen(a));
printf("%d\n", strlen(p));
printf("%d\n", strlen("hello"));
'l'
'l'
'o'
'\0'
strlen.s
}
(1) and (2)
(3)
7