I can do it all! Struct + Array + Dynamic Allocation + Pointer Arithmetic

Let's create a sample program that combines structures, arrays, dynamic allocation, and pointer arithmetic, along with comments for explanation:

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

// Define a structure representing a point in 3D space
struct Point3D {
    float x;
    float y;
    float z;
};

int main() {
    int numPoints;

    // Get the number of points from the user
    printf("Enter the number of 3D points: ");
    scanf("%d", &numPoints);

    // Dynamically allocate an array of Point3D structures
    struct Point3D *pointsArray = (struct Point3D *)malloc(numPoints * sizeof(struct Point3D));

    // Check if memory allocation was successful
    if (pointsArray == NULL) {
        fprintf(stderr, "Memory allocation failed. Exiting...\n");
        return 1; // Exit with an error code
    }

    // Populate the array with user-inputted 3D points
    printf("Enter the coordinates for each point (x y z):\n");
    for (int i = 0; i < numPoints; ++i) {
        printf("Point %d: ", i + 1);
        scanf("%f %f %f", &(pointsArray[i].x), &(pointsArray[i].y), &(pointsArray[i].z));
    }

    // Print the entered 3D points using pointer arithmetic
    printf("\nEntered 3D points:\n");
    for (int i = 0; i < numPoints; ++i) {
        // Using pointer arithmetic to access each Point3D structure in the array
        printf("Point %d: (%.2f, %.2f, %.2f)\n", i + 1, (pointsArray + i)->x, (pointsArray + i)->y, (pointsArray + i)->z);
    }

    // Deallocate the dynamically allocated memory
    free(pointsArray);

    return 0;
}

Explanation:

  1. We define a structure Point3D to represent a point in 3D space with x, y, and z coordinates.

  2. The program dynamically allocates an array of Point3D structures based on user input for the number of points.

  3. It prompts the user to input the coordinates for each 3D point.

  4. Using pointer arithmetic, it prints the entered 3D points.

  5. Finally, it deallocates the dynamically allocated memory to prevent memory leaks.

This example demonstrates the combination of structures, arrays, dynamic memory allocation, and pointer arithmetic in a simple program.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.