Example of a structure in C

This is just a placeholder for how these work. Largely, this is the predecessor of object oriented programming in that a structure has properties that can be assigned.

#include 
#include 

struct student {
  int studentId;
  char studentName[40];
  float pct;
};

int main() {
  int i;
  struct student record1 = {1, "First", 84.3};
  struct student record2 = {2, "Second", 80.6};
  struct student *ptr;

  ptr = &record1;

  printf("  Id is: %d \n", ptr->studentId);
  printf("  Name is: %s \n", ptr->studentName);
  printf("  Percentage is: %f \n\n", ptr->pct);
  printf("%x\n", ptr);

  ptr = &record2;

  printf("  Id is: %d \n", ptr->studentId);
  printf("  Name is: %s \n", ptr->studentName);
  printf("  Percentage is: %f \n\n", ptr->pct);
  printf("%x\n", ptr);
  return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.