Let’s see an example of a C modular Program, in outline form, as:
/* c_mod_prog.c -- example of a C modular program */ #include <stdio.h> /* function prototypes */ int shots(); int winner(int); void display_winner_loser(int); int main(void) { int x; char y; /* function calling */ shots(); winner(some_val); display_winner_loser(some_val); return 0; } int shots() { --- /* statements */ --- return win_shots; } int winner(int ) { --- --- return no_winner; } void display_winner_loser(int ) { --- --- }
Here in the above ex. program, observe that main() function has interfaces to the calling functions which are defined here, say in same file, however they could be defined in different files for their common access by other programs also. Now we consider importance of using pointers in modular C Programs. Let’s take some examples:
/* ptr_with_mod_c_prog1.c -- program shows modular c program */ #include <stdio.h> void write_table(int); int main(void) { int num; printf("Wanna create Table of some number, enter number: "); scanf("%d", &num); write_table(num); return 0; } void write_table(int copy) /* num copied into copy */ { int i; printf("\tTable of %d\n", copy); for (i = 1; i <= 10; i++) printf("%5d * %5d = %5d\n", copy, i, copy * i); printf("\n"); }
Output of the program for number, say 5, is as:
Wanna create Table of some number, enter number: 5 Table of 5 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
Notice that value of num is copied into integer variable copy declared in the function write_table() and not the address of num variable. Here, We performed on the copy of value of num.
/* * ptr_with_mod_c_prog.c -- program shows use of pointers in modular * c program */ #include <stdio.h> void find_char(char *, char); int main(void) { char ch; char str[] = "Hello, What are you doing these days?"; printf("User, which character u wanna find in string, enter character:" " "); ch = getchar(); find_char(str, ch); /* str, a string, is an address */ return 0; } void find_char(char *sp, char ch_cpy) /* address of string str copied into sp, pointer to string */ { char ch; while ( (ch = *sp++) != '\0' && ch != ch_cpy) ; if (ch_cpy == ch ) printf("Character \"%c\" is found!\n", ch_cpy); else printf("Character \"%c\" isn't found!\n", ch_cpy); }
Output of the above program for characters, say q, r, is as:
User, which character u wanna find in string, enter character: q Character "q" isn't found! User, which character u wanna find in string, enter character: r Character "r" is found!
Notice, however, here that we copied the address of string “str” into the pointer variable “sp” declared in the function find_char() and not the full string as it is. Using of pointer here saved lots of memory by copying the address of the first character of string “str” and not the full string irrespective of string length. Also, using pointer, we performed manipulations on the original string.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Check C Books
- Check Computer Science Books
- Apply for C Internship
- Practice BCA MCQs
- Watch Advanced C Programming Videos