Created
January 3, 2017 19:02
-
-
Save s3f/eef7be8d9f3f339f2f9b3624d23ade2b to your computer and use it in GitHub Desktop.
Chapter 24 - Intro to Pointers created by s3f - https://repl.it/EyR4/10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pointers | |
/* | |
Inside your computer is a bunch of memory which holds your program as it executes as well as the variables you declare. Just as your house has a unique address, so does a memory location. | |
Here are the pointer operators: & --> Address of operator | |
* --> Dereferencing operator | |
Here's how you define an integer and floating point variable: | |
int num; | |
float value; | |
Now here's how you define an integer pointer variable and a floating point pointer variable : | |
int * pNum; --> Defines 2 pointer variables | |
float * pValue | |
example code: | |
int age = 19; --> Stores a 19 in age | |
int * pAge = &age; Tells C to put the address of age into pAge | |
Now the dereferencing operator mainly exists to tell C that the variable is a pointer, not a regular variable. | |
Dereferencing simply means that you use the pointer to get to the other variable | |
The following program below that declares integer, float, and character variables, as well as pointer versions of all 3: | |
*/ | |
#include <stdio.h> | |
main() | |
{ | |
int kids; | |
int* pKids; | |
float price; | |
float* pPrice; | |
char code; | |
char* pCode; | |
price = 17.50; | |
pPrice = &price; | |
printf("\nHow many kids are you taking to the water park? "); | |
scanf(" %d", &kids); | |
pKids = &kids; | |
printf("\nDo you have a discount ticket for the park? "); | |
printf("\nEnter E for Employee discount, S for Sav- More "); | |
printf("Discount, or N for No Discount "); | |
scanf(" %c", &code); | |
pCode = &code; | |
printf("\nFirst let's do it with the variables: \n"); | |
printf("You've got %d kids...", kids); | |
switch (code) | |
{ | |
case ('E'): | |
printf("The employee discount saves you 25% on the "); | |
printf("$%.2f price", price); | |
printf("\nTotal ticket cost: $%.2f", (price * .75 * kids)); | |
break; | |
case ('S'): | |
printf("The Sav-more discount saves you 15% on the "); | |
printf("$%.2f price", price); | |
printf("\nTotal ticket cost: $%.2f", (price * .85 * kids)); | |
break; | |
default: // Either entered N for No Discount or an invalid letter | |
printf("You will be paying full price of "); | |
printf("$%.2f for your tickets", price); | |
} | |
// Now we repeat the same code but use dereferenced pointers instead | |
printf("\n\n\nNow let's do it with pointers: \n"); | |
printf("You've got %d kids...", *pKids); | |
switch (*pCode) | |
{ | |
case ('E'): | |
printf("The employee discount saves you 25% on the "); | |
printf("$%.2f price", *pPrice); | |
printf("\nTotal ticket cost: $%.2f", (*pPrice * .75 * *pKids)); | |
break; | |
case ('S'): | |
printf("The Sav-more discount saves you 15% on the "); | |
printf("$%.2f price", *pPrice); | |
printf("\nTotal ticket cost: $%.2f", (*pPrice * .85 * *pKids)); | |
break; | |
default: // Either entered N for No Discount or an invalid letter | |
printf("You will be paying full price of "); | |
printf("$%.2f for your tickets", *pPrice); | |
} | |
return (0); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment