New version page

# Purdue CS 15900 - Chapter 10 Pointer Applications

Pages: 6
Documents in this Course

## This preview shows page 1-2 out of 6 pages.

View Full Document
Do you want full access? Go Premium and unlock all 6 pages.
Do you want full access? Go Premium and unlock all 6 pages.

Unformatted text preview:

Chapter 10Pointer ApplicationsCS 159 - C ProgrammingPointers and Arrays▪ The name of an array is already a pointer constant which points to the first element of the array (this is why arrays start counting at zero)▪ Because it is a pointer constant, it cannot be changed by using it on the left side of the equal sign▪ The array name pointer only refers to the first element, not the whole array (i.e. the pointer to array name is the same as name)int a = {2, 8 ,3, 4, 1};int *p = a;printf("a = %d\n",a);printf("*a = %d\n",*a);printf("*p = %d\n",*p);a = 2*a = 2*p = 2Pointers and Arrays▪ A pointer can be assigned to any element of an array, just like any other variableint a = {2, 8 ,3, 4, 1};int *p;p = &a;printf("a = %d\n",a);printf("*p = %d\n",*p);a = 4*p = 4Pointer Arithmetic▪ Given pointer p, p ± n is a pointer to another value in the array n elements away▪ The size of the data type is automatically taken into consideration, thus p + 1 is not necessarily the next byte address:address = pointer + (offset *sizeof(element))▪ Just like before, you must be careful that you do not exceed the bounds of the arrayOffsetElementAddressValueaa14073514x41a+1a14073515x42a+2a14073516x43cc14073517x0014073518x0014073519x0114073520xF4c+1c14073521x0014073522x0014073523x0014073524x64bb14073525x0014073526x04b+1b14073527x0014073528x03b+2b14073529x0014073530x17char a = {'A','B','C'};short b = {4, 3, 23} int c = {500, 100}OffsetOffsetElementAddressValuepq-4num14073514p+1q-3num14073516p+2q-2num14073518p+3q-1num14073520p+4qnum14073522p+5q+1num14073524p+6q+2num14073526p+7q+3num14073528p+8q+4num14073530short num; short *p;short *q;p = num;q = &num;int a = {4, 2, 75, 23, 5, 15}; int *p = &a;printf("%d %d %d %d\n",a[-1], *(a-1), p[-4], *(p-4));printf("%d %d %d %d\n",a, *(a+0), p[-3], *(p-3));printf("%d %d %d %d\n",a, *(a+1), p[-2], *(p-2));printf("%d %d %d %d\n",a, *(a+2), p[-1], *(p-1));printf("%d %d %d %d\n",a, *(a+3), p, *(p-0));printf("%d %d %d %d\n",a, *(a+4), p, *(p+1));printf("%d %d %d %d\n",a, *(a+5), p, *(p+2));printf("%d %d %d %d\n",a, *(a+6), p, *(p+3));5434634 5434634 5434634 54346344 4 4 42 2 2 275 75 75 7523 23 23 235 5 5 515 15 15 15-1328003956 -1328003956 -1328003956 -1328003956You must still make sure you do not exceed the bound of the arrayint x[SIZE] = {3, 5, 7, 9, 10};int *y;int i;y = x;for(i = 0; i < SIZE; i++){printf("y[%d] = %d\n", i, *y);y++;}int x[SIZE] = {3, 5, 7, 9, 10};int i;for(i = 0; i < SIZE; i++){printf("x[%d] = %d\n", i, *(x + i));}Orint x[SIZE] = {3, 5, 7, 9, 10};printArray(x);...void printArray(int y[]){for(int i = 0; i < SIZE; i++)printf("y[%d] = %d\n", i, y[i]);return;}int x[SIZE] = {3, 5, 7, 9, 10};printArray(x);...void printArray(int *y){for(int i = 0; i < SIZE; i++)printf("y[%d] = %d\n", i, y[i]);return;}OrMemory Allocation▪ Static - all data used in memory is specifically declared at the beginning of the program • Is an array big enough?• Is there wasted space?▪ Dynamic – memory can be allocated and released during the execution of the program• Memory is allocated in the heap and referenced by pointersMemory AllocationHeapFreeStackCode<stdlib.h>FunctionDescriptionvoid* malloc(size_t size);Allocate memory blockvoid* calloc(size_t nobj, size_t size);Allocate and clear memory blockvoid* realloc(void *p, size_t size);Resize memory blockvoid free(void *p);Free memory block#include <stdio.h>#include <stdlib.h>int amtData();void getData(int*, int);void printData(int*, int);int main(void){int numExp; // Number of experimentsint *data; // Pointer to allocated memorynumExp = amtData();data = (int *)malloc(sizeof(int) * numExp);getData(data, numExp);printData(data, numExp);free(data);return 0;}int amtData(){int n;printf("Please enter number of experiments: ");scanf("%d", &n);return n;}void getData(int *d, int n){int i;for(i = 0; i < n; i++){printf("Enter value #%d: ", i + 1);scanf("%d", &d[i]);}return;}void printData(int *d, int n){int i;for(i = 0; i < n; i++)printf("Data value #%d: %d\n", i + 1, d[i]);return;}Please enter number of experiments: 3Enter value #1: 4Enter value #2: 7Enter value #3: 2Data value #1: 4Data value #2: 7Data value #3: 2free Command▪ Remember to free the memory when it is done being used▪ The pointer used to free memory must be of the same type as the pointer used to allocate the memory▪ Using a pointer after its memory has been released is a common programming error (you can set it to NULL after the memory is freed to be safe)Find the Bugs (3)#include <stdio.h>#include <string.h>int main(void){ int set1 = {4,29,5,6};int set2 = {8,6,4};int *set3 = set1 + 4;int i;for (i = 0; i < 4; i++)set3[i] = set2[i];for (i = 0; i < 8; i++)printf("%d ",set1[i]);return

View Full Document Unlocking...