Title: More Pointers
1More 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
2Solution
- 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
3Pointers
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
4Arrays, 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 x3 x y6 y4 x y x4 y
x9 for (i 0 i lt 10 i) printf(d
, xi)
5Pointer Arithmetic
int x 1,2,3,4,5,6,7,8,9,10 int y y x
1 y x / cant do / y x3 (y1)
x5 x y6 y4 x y (x4) y
(x 9)
6Passing 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 numbersMAX_NUMS, size
- size getValues(numbers)
- mean Average(numbers, size)
-
- int Average(int inputValues, int size)
-
- for (index 0 index lt size index) sum
sum indexValuesindex return (sum / size)
7Arrays of Pointers
- char names hello,how,are,you?
- What does this look like?
8char names hello,how,are,you?
- char word names1
- char all names1
- What are the values of the following?
- names31
- names
- (names03)
- (word)
- ((all)1)
9Returning 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 buffer128
- int index 0
- while ((strindex ! \0) (index lt 128))
- bufferindex strindex index
- return buffer
10Dynamic 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))
11Dynamic 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))
12Dynamic 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
13Dynamic Memory Allocation
- Memory Layout
- Activation Record
Code
Static Data
Stack
Heap
14Dynamic 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)
15Example
- include ltstdio.hgt
- include ltstdlib.hgt
- 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')
-
- linei c
- if (i gt size - 2)
-
- size INCREMENT
- line (char )realloc(line, size)
-
- i
-
- linei '\0'
- return line
17 1 includeltstdio.hgt 2
includeltstdlib.hgt 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(i0 iltnum i) 13
datai (char ) calloc( 20,
sizeof(char)) 14 15 for(i0
iltnum i) 16
printf("Enter item d ", i1) 17
scanf("s", datai) 18
19 20 printf("You entered\n")
21 for(i0 iltnum i) 22
printf("s ", datai) 23
printf("\n") 24 25 return 0
26
18Working With Strings
- Write version of strcat
- Treating string parameters as arrays
- Treating string parameters as pointers
19Command 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 ltstdio.hgt
- int main(int argc, char argv)
-
- int i
- for ( i 0 i lt argc i )
-
- printf("s ", argvi)
-
- printf("\n")
- return 0
21- include ltstdio.hgt
- int main(int argc, char argv)
-
- while (argc--)
- printf("s ", argv)
- printf("\n")
- return 0