New version page

Purdue CS 15900 - Chapter 10 Pointer Applications

Upgrade to remove ads

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

Save
View Full Document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Upgrade to remove ads
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[0])int a[5] = {2, 8 ,3, 4, 1};int *p = a;printf("a[0] = %d\n",a[0]);printf("*a = %d\n",*a);printf("*p = %d\n",*p);a[0] = 2*a = 2*p = 2Pointers and Arrays▪ A pointer can be assigned to any element of an array, just like any other variableint a[5] = {2, 8 ,3, 4, 1};int *p;p = &a[3];printf("a[3] = %d\n",a[3]);printf("*p = %d\n",*p);a[3] = 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 arrayOffsetElementAddressValueaa[0]14073514x41a+1a[1]14073515x42a+2a[2]14073516x43cc[0]14073517x0014073518x0014073519x0114073520xF4c+1c[1]14073521x0014073522x0014073523x0014073524x64bb[0]14073525x0014073526x04b+1b[1]14073527x0014073528x03b+2b[2]14073529x0014073530x17char a[3] = {'A','B','C'};short b[3] = {4, 3, 23} int c[2] = {500, 100}OffsetOffsetElementAddressValuepq-4num[0]14073514p+1q-3num[1]14073516p+2q-2num[2]14073518p+3q-1num[3]14073520p+4qnum[4]14073522p+5q+1num[5]14073524p+6q+2num[6]14073526p+7q+3num[7]14073528p+8q+4num[8]14073530short num[9]; short *p;short *q;p = num;q = &num[4];int a[6] = {4, 2, 75, 23, 5, 15}; int *p = &a[3];printf("%d %d %d %d\n",a[-1], *(a-1), p[-4], *(p-4));printf("%d %d %d %d\n",a[0], *(a+0), p[-3], *(p-3));printf("%d %d %d %d\n",a[1], *(a+1), p[-2], *(p-2));printf("%d %d %d %d\n",a[2], *(a+2), p[-1], *(p-1));printf("%d %d %d %d\n",a[3], *(a+3), p[0], *(p-0));printf("%d %d %d %d\n",a[4], *(a+4), p[1], *(p+1));printf("%d %d %d %d\n",a[5], *(a+5), p[2], *(p+2));printf("%d %d %d %d\n",a[6], *(a+6), p[3], *(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[8] = {4,29,5,6};int set2[4] = {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
Download Chapter 10 Pointer Applications
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 10 Pointer Applications and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 10 Pointer Applications 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?