This is the post I’m keeping on the down-low since my site is 99% about Python. Along the way, in order to better understand the underpinnings of Python (which was written in C as were other languages), I decided I need to learn enough C to be able to function the in the language. I’m taking a course from Udemy but the instruction leaves a lot to be desired and for every hour spent listening to lectures I’m spending about 4 or 5 reading stuff on the internet pertaining to the current subject of study. One big hurdle was trying to integrate my understanding of how pointers, structures and functions work together. I could never find a single, unified example. So here I present one so I will know at least one coherent example exists on the internet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | /* USING POINTERS WITH STRUCTURES AND FUNCTIONS */ #include <stdio.h> #include <stdlib.h> /* Declare struct structure first */ struct item { char name[30]; /* Could create a pointer here with: char *itemName; */ /* but if we do it that way we will need malloc to allocate later */ /* the advantage to creating just a pointer is we do not use up memory here */ int quantity; float price; float amount; }; /* ...then declare function - struct must already exist */ void readItem( struct item *); /* * says pass a pointer - could add tag name, ex: *pMyPtr could make it a constant with (const struct item *pMyPtr) */ int main() { struct item itemx, *itemPtr; /* Create struct instance and pointer */ itemPtr = &itemx; /* initialize pointer to struct INSTANCE */ /* if only created pointer above, we would use malloc here with: */ /* itemPtr->name = (char *) malloc(30 * sizeof(char)); /* then check for null with: if(itemPtr == NULL) exit(-1); */ readItem(itemPtr); /* call function - then print proof of performance */ printf ( "\nReturned from function\nData inserted by pointer: \n" ); printf ( "Name: %s\n" , itemx.name); printf ( "Price: %.2f\n" , itemx.price); printf ( "Quantity: %d\n" , itemx.quantity); printf ( "Amount (from pointer): %.2f\n" , itemx.amount); printf ( "Compare to Amount calclated: %.2f\n" , itemx.price * itemx.quantity); free (itemPtr->name); /* need to free memory if we allocate it */ return 0; } void readItem( struct item *itemPtr) { printf ( "As prompted, please enter name, price, and quantity.\n" ); printf ( "Item name: " ); /* note no '\n needed in inputs */ gets (itemPtr->name); /* gets captures strings with spaces scanf would be: scanf("%s", itemPtr->name); */ printf ( "Enter price: " ); scanf ( "%f" , (&itemPtr->price)); printf ( "Enter quantity: " ); scanf ( "%d" , (&itemPtr->quantity)); /* Demo pointer efficacy */ printf ( "\nName: %s\n" , itemPtr->name); /* the -> operator selects element and deref's pointer */ printf ( "Price: %.2f\n" , (*itemPtr).price); /* alternative construction not using the -> operator */ printf ( "Quantity: %d\n" , itemPtr->quantity); printf ( "\nTesting __________\n" ); printf ( "Pointer arithmetic: \n" ); float sum = (itemPtr->price)*(itemPtr->quantity); printf ( "sum is: %.2f\n" , sum); /* could assign amount directly with */ itemPtr->amount = sum; /* itemPtr->amount = (float)itemPtr->quantity * itemPtr->price; */ return ; } |