100 likes | 219 Vues
This program demonstrates how to handle command line arguments in C using `argc` and `argv`, which count and hold the inputs provided at program execution. It also explores pointers to functions, dynamic memory allocation, and the use of structures in C. The example utilizes arrays of pointers for effective memory management and showcases how to invoke functions that operate on these pointers. Participants will gain insights into the practical use of automatic and static variables, as well as the conversion to C structures, achieving a comprehensive understanding of these important programming concepts.
E N D
What does this program do ? #include <stdio.h> 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; }
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()) .
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 ?
Most useful in storing character strings of different lengths. • char *name[] = {“Illegal month”, “Jan”}; • char aname[][15] = {“Illegal month”, “Jan”}; • Similarly : char *argv[]
#include <stdio.h> 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
Pointer to functions #include <stdio.h> #include <malloc.h> 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; }
Automatic and static variables • malloc: variables allocated memory at runtime • dynamic memory allocation. • Resides in a heap. • Has to be freed explicitly.
Convert to English • static int c = 1; • const int * pc; • int * const cp; • int *a[10]; • int **ip; • int *f(void *) • int (*f) (void *)
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.
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); }