Monash University >
CSSE >
CSE1303 >
Part A >
Lectures > Lecture A02 notes
CSE1303 Computer Science
Semester 3 (summer), 2003
Part A
Lecture A02 notes: Pointers (revision)
In this lecture
Reading:
- Kruse - Appendix C.6
- Deitel & Deitel - Chapter 7
- King - Chapter 11, 12
- Langsam - Chapter 1.1
- Standish - Chapter 2.3
- Kernighan and Ritchie - Chapter 5.1 to 5.10, 6.2
What is a pointer?
- A variable that contains the memory address of another variable.
- Every pointer "points to" a specific data type.
- All pointers must be initialized.
The operation &
- pointer = &object assigns the address of object to the variable pointer.
- Can only be applied to objects in memory: variables and array elements.
- Cannot be applied to expressions, constants, or register variables.
The operation *
- *pointer accesses the object that the variable pointer "points to".
- objectType* pointer defines pointer as a variable which points to objects of type objectType.
The operation ->
typedef struct SomeRec Something;
Something* pointer;
- pointer->membername is equivalent to (*pointer).membername.
Pointers and function arguments
- To enable a function to access and change an object, a pointer to that object needs to be passed to the function.
- If a large structure is to be passed to a function, it is generally more efficient to pass a pointer than to copy the whole structure.
- Use the const specifier whenever a constant is intended.
Pointer Arithmetic
- If aPtr points to some element of an array, then aPtr++ and ++aPtr increments aPtr to point to the next element.
- If aPtr points to some element of an array, then aPtr-- and --aPtr decrements aPtr to point to the previous element.
- If aPtr points to some element of an array, then aPtr += n increments aPtr to point to n elements beyond where it currently points to.
- If aPtr points to some element of an array, then aPtr -= n decrements aPtr to point to n elements before where it currently points to.
- If aPtr points to the ith element of an array and bPtr points to the jth element of the same array, then aPtr-bPtr equals i-j.
Arrays
- The name of an array is a synonym for the location of the first element of the array, i.e. array and &array[0] are equivalent.
- The expression array[n] is equivalent to *(array + n).
- The expression &array[n] is equivalent to array + n.
- The definition float a[2][3]; defines a to be an array of 2 elements, each of which is an array of 3 floats.
- The expression a[n][m] is equivalent to *(a[n] + m) and *(*(a+n)+m).
- Multidimensional arrays are stored in row-major order. For example in array above the elements are stored (in increasing address) as:
a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2]
Code in this lecture
#include <stdio.h>
void fakeSwap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
int main()
{
int x = 1;
int y = 2;
fakeSwap(x, y);
printf("%d %d\n", x, y);
}
#include <stdio.h>
void trueSwap(int* a, int* b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
int main()
{
int x = 1;
int y = 2;
trueSwap(&x, &y);
printf("%d %d\n", x, y);
}
/* strcpy: copy t to s; array subscript version */
char* strcpy(char* s, char* t)
{
int i;
i = 0;
while (t[i] != 0)
{
s[i] = t[i];
i++;
}
s[i]=0;
return s;
}
/* strcpy: copy t to s; pointer version */
char* strcpy(char* s, char* t)
{
char* p = s;
while (*p++ = *t++)
{
;
}
return s;
}
[ Top |
Home ]
Last Updated: Tuesday 02 December 2003 22:29:28