Functions & Pointers in C Jordan Erenrich [email protected].
-
Upload
julian-goodman -
Category
Documents
-
view
216 -
download
0
Transcript of Functions & Pointers in C Jordan Erenrich [email protected].
![Page 2: Functions & Pointers in C Jordan Erenrich erenrich@cs.cornell.edu.](https://reader036.fdocuments.net/reader036/viewer/2022082713/5697bfd51a28abf838cad591/html5/thumbnails/2.jpg)
Functions
• Similar to Javafloat multiply(float a, float b) {
float ans = a * b;
reutrn ans;
}
![Page 3: Functions & Pointers in C Jordan Erenrich erenrich@cs.cornell.edu.](https://reader036.fdocuments.net/reader036/viewer/2022082713/5697bfd51a28abf838cad591/html5/thumbnails/3.jpg)
Forward References• C compiler is one pass• Functions must be declared before they are used.
Typically:#include <stdlib.h>
float multiply(float, float); /* Forward reference */
int main (int argc, char* argv[]) {int x, y, z;x = 5;y = 2;z = multiply(x, y);
}
float multiply(float a, float b) {return a*b;
}
![Page 4: Functions & Pointers in C Jordan Erenrich erenrich@cs.cornell.edu.](https://reader036.fdocuments.net/reader036/viewer/2022082713/5697bfd51a28abf838cad591/html5/thumbnails/4.jpg)
Passing by Value
• Function parameters are unchanged, outside of the function. i.e.,
void swap(float a, float b) {float temp;temp = a;a = b;b = temp
}
• This function is useless• How can we modify external data? Use pointers
![Page 5: Functions & Pointers in C Jordan Erenrich erenrich@cs.cornell.edu.](https://reader036.fdocuments.net/reader036/viewer/2022082713/5697bfd51a28abf838cad591/html5/thumbnails/5.jpg)
What is a pointer?
• A reference to a physical memory address
Memory Address~~~~ 0x0000~~~~ 0x0001
0x0002 ~~~~ 0x0002~~~~ .~~~~ .~~~~ .~~~~ .~~~~ 0xffff
![Page 6: Functions & Pointers in C Jordan Erenrich erenrich@cs.cornell.edu.](https://reader036.fdocuments.net/reader036/viewer/2022082713/5697bfd51a28abf838cad591/html5/thumbnails/6.jpg)
Using Pointersint x = 42;
int* iPtr; /* int* is a pointer type */iPtr = &x; /* & gets a pointer to a variable*/
/* Outputs “42 = 42” */printf(“%d = %d\n”, x, *iPtr);
x = 137;
/* Outputs “137 = 137” */printf(“%d = %d\n”, x, *iPtr);
![Page 7: Functions & Pointers in C Jordan Erenrich erenrich@cs.cornell.edu.](https://reader036.fdocuments.net/reader036/viewer/2022082713/5697bfd51a28abf838cad591/html5/thumbnails/7.jpg)
Passing by Reference
• Use the * operator to dereference a pointervoid swap(float* a, float* b) {
float temp;temp = *a;*a = *b;*b = temp
}
• Therefore:int x = 42, y = 137;printf(“%d - %d”, x, y); /*Prints “42 –137” */
swap(&x, &y);printf(“%d - %d”, x, y); /*Prints “137 – 42” */
![Page 8: Functions & Pointers in C Jordan Erenrich erenrich@cs.cornell.edu.](https://reader036.fdocuments.net/reader036/viewer/2022082713/5697bfd51a28abf838cad591/html5/thumbnails/8.jpg)
C array implementation
• Pointer to a contiguous block of memorychar myStr[50] = “Hello World”;
H e l l o w o r l d \0 ~ ~ ~
myStr
0x~~~
![Page 9: Functions & Pointers in C Jordan Erenrich erenrich@cs.cornell.edu.](https://reader036.fdocuments.net/reader036/viewer/2022082713/5697bfd51a28abf838cad591/html5/thumbnails/9.jpg)
Declaring Arrays
• Static length is simplest #define MAXSTRING 1000 char myString[MAXSTRING]
• But, it’s size is…static
• Arrays can not be resized after a static declaration
![Page 10: Functions & Pointers in C Jordan Erenrich erenrich@cs.cornell.edu.](https://reader036.fdocuments.net/reader036/viewer/2022082713/5697bfd51a28abf838cad591/html5/thumbnails/10.jpg)
Dynamically Allocation Arrays
• Explicitly allocate memory with mallocchar *myStr;int length = 1234;myStr = malloc(sizeof(char)*length)
• Must explicitly de-allocate memoryfree(myStr);
![Page 11: Functions & Pointers in C Jordan Erenrich erenrich@cs.cornell.edu.](https://reader036.fdocuments.net/reader036/viewer/2022082713/5697bfd51a28abf838cad591/html5/thumbnails/11.jpg)
Arrays of Arrayschar **arrOfStrs; /*Double pointer*/int i, strLen = 1024, numStrings = 5;
/*Allocate the memory*/arrOfStrs = malloc(sizeof(char*)*numStrings);for(i = 0; i < numStrings; i++) {
arrOfStrs[i] = malloc(sizeof(char)*strLen);}. . ./*Free the memory*/for(i = 0; i < numStrings; i++) {
free(arrOfStrs[i]);}free(arrOfStrs);