Declaring and initializing both regular and pointer variables for int, float, and char types and then displays the values of each. - C Pointer

C examples for Pointer:Introduction

Description

Declaring and initializing both regular and pointer variables for int, float, and char types and then displays the values of each.

Demo Code

#include <stdio.h>

int main()//  w  ww  . ja  v a  2  s.co m
{
    int kids = 5;
    int * pKids;
    float price;
    float * pPrice;
    char code;
    char * pCode;
    price = 17.50;
    pPrice = &price;

    pKids = &kids;
    printf("Enter E for Employee Discount, S for Sav-More ");
    printf("Discount, or N for No Discount: ");
    scanf(" %c", &code);

    pCode = &code;
    printf("You've got %d kids...\n", 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("full price: $%.2f for your tickets", price);

    }
    // but use dereferenced pointers
    printf("You've got %d kids...\n", *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 :
        printf("You will be paying full price of ");
        printf("$%.2f for your tickets", *pPrice);
    }

    return(0);
}

Result


Related Tutorials