/* C Programming Template 8/11/10 Author: Michael Ohene. Copyright (c) 2010 Summary: A template to demonstrate concise, practical programming examples for the C Programming Language Topics covered in this C programming file include the following: 1. Passing variables to functions by the following methods a. by reference b. by value c. via multiple function passes 2. How to allocate memory for arrays, which includes a. Dynamic array allocation (realloc, malloc) i. 1-D array ii.2-D array Purpose: Get a pencil/pen and a piece of paper. Go to the main() function and try to record all the assignments and changes in value. Comp- ilation is not necessary since I have included the assignment values in this program. By the end of this program, hopefully, you will understand pass values to functions. If you want to compile this program I would suggest: 1. Using Linux 2. typing 'cc -o test1 test1.c 3. typing './test1' */ #include // a better name for '#include' is '#insert' #include // pre-written code is inserted by referencing *.h files with #include #include #define COLS 5 typedef struct Person{ int age; int year; struct Person *next; float *grow; }Person; // struct Person{}; won't work in C programming int embedded(struct Person *start, int **num1, int *num2){ int dum_var=9; printf("Num1 is equal to 15: %d\n", **num1); (**num1)++; printf("Num1 is equal to 16: :%d\n", **num1); printf("Num2 is equal to 5: %d\n", *num2); **num1=dum_var; printf("Num1 is equal to 9: %d\n", **num1); start->age=(start->age)-1+(**num1); printf("start->age is equal to 39:%d\n", start->age); return start->age; } // num1 wil be changed so you must add *, int exchange(struct Person *tmp_ptr, int *num1, struct Person *start, int *num2) { int proxy; int dum_var=92; *num1=*num1+3; //i=12+3, note: the '*'s printf("Print 15: %d\n", *num1); //15 *num2=5; proxy=*num1; //proxy=15 *num1=proxy; //num1=15 tmp_ptr->age=(*num1)+(*num1)+1; //tmp_ptr->age=15+15+1=31 printf("Prints 31:%d\n", tmp_ptr->age); proxy=embedded(tmp_ptr, &num1, num2); if (tmp_ptr==start){printf("Doesn't print !:%d\n", dum_var);} tmp_ptr=start; printf("Prints 66:%d\n", tmp_ptr->age); if (tmp_ptr==start){printf("Dum var is 92: %d\n", dum_var);} return proxy; } float *pass_ptr(float assign){ float *float_ptr; assign=32; printf("Assign is now 32.0000: %f\n", assign); *float_ptr=4.4; printf("Float_ptr is 4.4000: %f\n", *float_ptr); return float_ptr; } unsigned short int value(){ unsigned short int t=0; return t; } int main(void) { //char notation[128], buff[128]; int *int_ptr, **dyn_alloc; int t; int num1=12; int num2=4; char word[]="arbitrary_length_word"; char num_as_letter[]="1"; Person *start; Person *tmp_ptr; Person **array; array=malloc(2*sizeof(Person*)); //make two columns (rows do not exist) //wrong declaration! ->> fake=malloc(2*sizeof(Person)); array[0]=malloc(2*sizeof(Person)); //make the first horizontal row //The next line prevents a segmentation fault, we must allocate memory before we use memory tmp_ptr=malloc(sizeof(Person)); tmp_ptr->age=45; start=malloc(sizeof(Person)); start->age=66; tmp_ptr->age=exchange(tmp_ptr, &num1, start, &num2); /********************************************/ int_ptr=malloc(sizeof(int)); //allocate memory for pointer 'int_ptr' *int_ptr=9; //'int_ptr' is a pointer, '*int_ptr' is an integer if (isdigit(num_as_letter[0])){ printf("'1' is an alpha\n"); } if (word[1]==('f')||word[1]==('a')||word[1]==('l')||word[1]==('s')||word[1]==('e') ){ printf("Proper way to check for a match of %c; will not execute", word[1]); } printf("\nI found a ptr 9: %d\n", *int_ptr); if(!value()){ printf("Returning a value of '0' is 'FALSE'\n"); } tmp_ptr->grow=malloc(2*sizeof(float *)); //allocate memory for 'tmp_ptr->grow' tmp_ptr->grow[1]=23.22; //set the second memory location of tmp_ptr->grow equal to '23.22' printf("Grow is 23.21999: %f\n",tmp_ptr->grow[1]); pass_ptr(tmp_ptr->grow[1]); printf("tmp_ptr->grow[1] is 23.21999: %f\n", tmp_ptr->grow[1]); array[0]->next=tmp_ptr; array[0]->age=28; start=array[0]; dyn_alloc = malloc(2*sizeof(int*)); //allocate two columns dyn_alloc[0] = malloc(2*sizeof(int)); //make a first row dyn_alloc[1] = malloc(2*sizeof(int)); //make a second row dyn_alloc[0][0]=9; //first row,first column dyn_alloc[1][0]=43; //second row, first column printf("Print 28:%d\n", array[0]->age); printf("Print 39:%d\n", tmp_ptr->age); printf("Print 28:%d\n", start->age); printf("Print 23.21999:%f\n", tmp_ptr->grow[1]); printf("Print 9:%d\n", dyn_alloc[0][0]); printf("Print 43:%d\n", dyn_alloc[1][0]); free(tmp_ptr); for (t = 0 ; t < 2 ; ++t) { free(dyn_alloc[t]); } // Deallocate the columns free(dyn_alloc); }