C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Problem Statement
  • Algorithm
  • Program
  • Explanation

Was this helpful?

  1. Structure

Program: Structure pointers and Functions

Problem Statement

Create a C program that involves structures and pointers. The program should define a structure named Item with members:

  • itemName (a pointer to a character array)

  • quantity (an integer)

  • price (a floating-point number)

  • amount (a floating-point number, calculated as quantity * price)

The program should have two functions:

  1. readItem Function:

    • Takes a structure pointer of type Item as a parameter.

    • Allocates memory for the itemName pointer.

    • Reads product name, price, and quantity from the user and stores them in the passed-in structure.

  2. printItem Function:

    • Takes a structure pointer of type Item as a parameter.

    • Prints the contents of the structure, including product name, price, quantity, and total amount.

In the main function:

  • Declare an Item structure and a pointer to the Item.

  • Allocate memory for the itemName pointer.

  • Pass the Item pointer to both the readItem and printItem functions.

  • Finally, free the allocated memory for the itemName pointer.

Algorithm

  1. Declare a structure Item with the required members.

  2. Implement the readItem function:

    • Allocate memory for the itemName pointer.

    • Read product name, price, and quantity from the user.

    • Store the input values in the passed-in structure.

  3. Implement the printItem function:

    • Print the product name, price, quantity, and total amount.

  4. In the main function:

    • Declare an Item structure and a pointer to the Item.

    • Allocate memory for the itemName pointer.

    • Pass the Item pointer to the readItem function.

    • Pass the Item pointer to the printItem function.

    • Free the allocated memory for the itemName pointer.

Program

#include <stdio.h>
#include <stdlib.h>

struct Item {
    char *itemName;
    int quantity;
    float price;
    float amount;
};

void readItem(struct Item *itemPtr);
void printItem(const struct Item *itemPtr);

int main() {
    struct Item item;
    struct Item *itemPtr = &item;

    // Allocate memory for the itemName pointer
    item.itemName = (char *)malloc(50 * sizeof(char));

    // Pass the Item pointer to readItem
    readItem(itemPtr);

    // Pass the Item pointer to printItem
    printItem(itemPtr);

    // Free the allocated memory for itemName
    free(item.itemName);

    return 0;
}

Explanation

  • The Item structure is defined with the necessary members.

  • The readItem function allocates memory for itemName and reads user input.

  • The printItem function displays the contents of the structure.

  • In the main function, an Item structure and a pointer to Item are declared.

  • Memory is allocated for the itemName pointer.

  • The Item pointer is passed to both the readItem and printItem functions.

  • Finally, the allocated memory for itemName is freed.

PreviousStructures and FunctionsNextFile Input and Output

Was this helpful?