Programming Languages and Paradigms The C Programming Language (pointers, arrays, and strings)
Pointer types In C, any existing type may have an associated pointer type Example (for the int type) int i; /* defines an int variable */ int *p; /* defines a pointer-to-int variable */ p i 5 5 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Pointer values: addresses Pointer variables hold addresses of memory locations e.g., Here, address 9006 is a memory location that contains the integer value 5 p 9006 9006: 5 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Pointer operations Address operator & : from variables to addresses int num = 10; int *p; p = # De-referencing operator * : from addresses to variables printf( “%d\n”, num ); printf( “%d\n”, *p ); printf( “%d\n”, p ); p num 2020 2020: 10 Prints: 10 <some address> 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Arrays are pointers An array name represents the address of the first element of the array Example: int scores[3]; scores[0] = 5; scores[1] = 10; scores[2] = 8; int *p; p = scores; printf( “%d”, *p ); /* same as p[0] */ Prints: 5 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
What values do we assign to pointer variables? An address generated through & An array name Pointer arithmetic calculation Through the malloc() function For char* variables, string literals 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Pointer arithmetic Adding a number to a pointer provides an address Let p be a pointer value of type T and i is an integer Suppose the address P contains a sequence of T values p+i is the address of the ith T data object in memory In fact, *(p+i) refers to this ith value and is synonymous to p[i] which is why indexing operations like arr[i] works 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
malloc malloc(n) allocates n bytes of memory and returns the address of the first byte Example char *s; s = (char *) malloc( 10 ); /* allocates 10 characters */ /* can now use s like as if char s[10]; */ Need to use sizeof function/macro for other types int *p = (int *) malloc( 10*sizeof(int) ); 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
About malloc and dynamic allocation malloc() returns NULL if allocation fails malloc()’s return type is void * which is why a cast is required To free up memory previously allocated by malloc(), use free free( s ); free( p ); Multidimensional array allocation provides many options (array of pointers or pointer to arrays?) 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Pointers and the -> operator Suppose struct stype { … }; struct stype *s; (*s).field is equivalent to s->field Useful for deep data structures 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Strings String: sequence of characters (stored in a character array) The null (‘\0’) terminator String literals ( e.g., “hello” ) String functions (<string.h>) strcpy strlen strcmp strcat … 9/18/2018 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved.