Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.

Similar presentations


Presentation on theme: "More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value."— Presentation transcript:

1 More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value –Both of the above functions must have void return types –Print out the doubled value in main

2 Solution int main(void) { double value; inputDataAndDouble(&value); printf(“Your value doubled is %f.”, value); return 0; } void inputDataAndDouble(double *num) { printf(“Enter a number and I will double it: “); scanf(“%lf”, num); /* Why not &num? */ doubleValue(num); /* Why not &num or *num? */ } void doubleValue(double *n) { *n = *n * *n; }

3 Pointers What gets printed? int main(void) { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z); return 0; }

4 Arrays, and Pointers Pointer and Array equivalence –Array name is “pointer” to first element in array int x[] = {1,2,3,4,5,6,7,8,9,10}; int *y; y = x; *y = x[3]; *x = y[6]; y[4] = *x; y = &x[4]; *y = x[9]; for (i = 0; i < 10; i++) printf(“%d “, x[i]);

5 Pointer Arithmetic int x[] = {1,2,3,4,5,6,7,8,9,10}; int *y; y = x + 1; ++y; ++x; /* can’t do */ *y = ++x[3]; *(y+1) = x[5]++; *x = y[6]; y[4] = *x; y = &*(x+4) *y = *(x + 9);

6 Passing Arrays as Arguments C passes arrays by reference –the address of the array (i.e., of the first element) is passed to the function –otherwise, would have to copy each element main() { int numbers[MAX_NUMS], size; … size = getValues(numbers); mean = Average(numbers, size); … } int Average(int inputValues[], int size) { … for (index = 0; index < size; index++) sum = sum + indexValues[index]; return (sum / size); }

7 16-7 Arrays of Pointers char *names[] = {“hello”,”how”,”are”,”you?”}; What does this look like?

8 16-8 char *names[] = {“hello”,”how”,”are”,”you?”}; char *word = names[1]; char **all = names+1; What are the values of the following? names[3][1] **names *(names[0]+3) *(++word) *(*(++all)+1)

9 Returning arrays from functions Assume that str is never more than 128 characters What is wrong with this function? How can it be fixed? char *copyString(char *str) { char buffer[128]; int index = 0; while ((str[index] != ‘\0’) && (index < 128)) {buffer[index] = str[index]; index++;} return buffer; }

10 Dynamic Memory Allocation void *malloc(size_t size) –malloc allocates size number of bytes in memory and returns a pointer to it. The memory is not cleared. –Use: –Use malloc to fix copyString Make size flexible What must caller do with return value when done with it? char *line; int *x; line = (char *) malloc (sizof(char) * 80); x = (int *) malloc(sizeof(int));

11 Dynamic Memory Allocation void *calloc(size_t nelm, size_t size) –calloc allocates size * nelm number of bytes in memory and returns a pointer to it. Memory is zeroed out. –Use: char *line; int *x; line = (char *) calloc (80, sizof(char)); x = (int *) calloc(1, sizeof(int));

12 Dynamic Memory Allocation void *realloc(void *ptr, size_t size) –realloc changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. –Use: See Example

13 Dynamic Memory Allocation Memory Layout Activation Record Code Static Data Stack Heap

14 Dynamic Memory Allocation void free(void *ptr) –free releases the memory pointed to by ptr. The memory must have been created by malloc or one of its kind. –Use: char *line; line = (char *) calloc (80, sizof(char)); free(line);

15 Example #include #define INCREMENT 80 char *getLine(void); int main(void) { char *line = getLine(); /* no limit to the size of line */ printf("%s\n", line); free(line); return 0; }

16 char *getLine(void) { char *line, c; int size = sizeof(char) * INCREMENT; int i = 0; line = (char *)malloc(size); while((c = getchar()) != EOF && c != '\n') { line[i] = c; if (i >= size - 2) { size += INCREMENT; line = (char *)realloc(line, size); } i++; } line[i] = '\0'; return line; }

17 1 #include 2 #include 3 4 int main() { 5 char **data; /* same as char char *data[] */ 6 int num, i; 7 8 printf("How many items: "); 9 scanf("%d",&num); 10 11 data = (char **) calloc(num, sizeof(char *)); 12 for(i=0; i<num; i++) 13 data[i] = (char *) calloc( 20, sizeof(char)); 14 15 for(i=0; i<num; i++) { 16 printf("Enter item %d: ", i+1); 17 scanf("%s", data[i]); 18 } 19 20 printf("You entered:\n"); 21 for(i=0; i<num; i++) 22 printf("%s ", data[i]); 23 printf("\n"); 24 25 return 0; 26 }

18 Working With Strings Write version of strcat –Treating string parameters as arrays –Treating string parameters as pointers

19 Command Line Arguments main(int argc, char *argv[]) So, if your program is invoked as: % a.out one two three these parameters would look like:

20 #include int main(int argc, char *argv[]) { int i; for ( i = 0; i < argc; i++ ) { printf("%s ", argv[i]); } printf("\n"); return 0; }

21 #include int main(int argc, char *argv[]) { while (argc--) printf("%s ", *argv++); printf("\n"); return 0; }


Download ppt "More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value."

Similar presentations


Ads by Google