Download presentation
Presentation is loading. Please wait.
1
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n", i, argv[i]); return 0; }
2
Command line arguments main() can be called with arguments –argc (argument count) : Number of command line arguments program is invoked with –argv (argument vector) : Array of pointers to character strings (input arguments with main() ).
3
Arrays of pointers int a[10][20] int *b[10]; a[3][4], b[3][4] both valid references. sizeof(a) = ? sizeof(b) = ? Any other advantage ?
4
Most useful in storing character strings of different lengths. char *name[] = {“Illegal month”, “Jan”}; char aname[][15] = {“Illegal month”, “Jan”}; Similarly : char *argv[]
5
#include void f(char *p[]) { void *q; q = *p; *p = *(p+1); *(p+1) = q; } main(int argc, char **argv) { f(argv); printf("%s,%c\n",argv[0],argv[0][1]); } What does f() do. What is the output of the program if we type: –$ gcc f.c –./a.out 23 45
6
Pointer to functions #include int * funct2(int a) { int b = ++a * 10; return &b; } int main(void) { int *(*fp)(int); int *c, d= 6; c = (int *) malloc(sizeof(int)); *c = 30; fp = funct2; d = 2; c = fp(d); printf("c = %d, d = %d\n",*c,d); return 0; }
7
Automatic and static variables malloc: variables allocated memory at runtime –dynamic memory allocation. –Resides in a heap. –Has to be freed explicitly.
8
Convert to English static int c = 1; const int * pc; int * const cp; int *a[10]; int **ip; int *f(void *) int (*f) (void *)
9
Convert to C Structure person has three components: name (a pointer to a structure that contains two components fname (a character string), and lname (a character string)); dob (an array of 3 integers), and parent (a pointer to a person). Declare variable employees as an array of 10 pointers to structure person. Function p that takes person as a call-by-reference argument, and returns a generic pointer. Procedure map that takes a pointer fp to a function that returns an integer pointer. Show how you would invoke the function referenced by fp, and print out it’s result.
10
char *f0 (char *a[][3]) { return (**a); } char *f1 (char *a[][3]) { return (*(*(a+1)+2)); } int main (void) { char *(*funcs[2]) (char *array[][3]); static char *array[][3] = {{"0","1","2"}, {"3","4","5"}, {"6","7","8"}}; funcs[0] = &f0; funcs[1] = &f1; printf("%s\n", (**funcs)(array)); printf("%s\n", (**(funcs+1))(array+1)); return (0); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.