Page 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
Page 2
Solutionint 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;
}
Page 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;}
Page 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]);
Page 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);
Page 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);
}
Page 7
16-7
Arrays of Pointers
• char *names[] = {“hello”,”how”,”are”,”you?”};
• What does this look like?
Page 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)
Page 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;
}
Page 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));
Page 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));
Page 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
Page 13
Dynamic Memory Allocation
• Memory Layout
• Activation Record
Code
Static Data
Stack
Heap
Page 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);
Page 15
Example#include <stdio.h>
#include <stdlib.h>
#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;
}
Page 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;
}
Page 17
1 #include<stdio.h> 2 #include<stdlib.h> 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 }
Page 18
Working With Strings
• Write version of strcat – Treating string parameters as arrays– Treating string parameters as pointers
Page 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:
Page 20
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for ( i = 0; i < argc; i++ )
{
printf("%s ", argv[i]);
}
printf("\n");
return 0;
}
Page 21
#include <stdio.h>
int main(int argc, char *argv[])
{
while (argc--)
printf("%s ", *argv++);
printf("\n");
return 0;
}