# 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 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:

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

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; }

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; }

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]);

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);

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); }

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

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)

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; }

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));

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));

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

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

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);

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; }

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; }

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/14/4257935/slides/slide_17.jpg", "name": "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

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

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:

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

#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