#10 (07/02/2025)

Structures

#include <stdio.h>

int main()
{
	int a[4][2]={{89, 76},{65, 91}, {56, 78}, {49, 56}};

        return 0;
}
#include <stdio.h>

struct student{
        char *ID;
        int Midterm;
        int Final;
        char Grade;
		};
int main()
{
  struct student smith={"1000123456", 89, 98, 'A'},
                 doe={"1000123457", 45, 53, 'F'},
                 jones={"1000123458", 67, 80, 'C'};

  printf("%s\n", smith.ID);
  printf("%c\n", jones.Grade);
  printf("%d\n", doe.Midterm);

  doe.Midterm=79;
  doe.Grade='D';

  printf("%c\n", doe.Grade);

  return 0;
}
As seen in the example above, a member in a structure can be accessed by using the dot (.).
#include <stdio.h>

struct student{
        char *Name;
        int Midterm;
        int Final;
        char Grade;
		};
int main()
{
  struct student fall[15];
  int i;
  FILE *data;
  data=fopen("junk.dat", "r");

  for (i=0;i<15;i++)
   fscanf(data, "%s %d %d %c", 
        fall[i].Name, &fall[i].Midterm, &fall[i].Final, &fall[i].Grade);
  fclose(data);

  printf("%d\n", fall[0].Midterm);
  printf("%s\n", fall[0].Name);

  return 0;
}
Doe 45 47 F
Jones 89 65 B
Smith 76 79 B
........
#include <stdio.h>

struct student{
        char *Name;
        int Midterm;
        int Final;
        char Grade;
		};
int main()
{
  struct student fall[3]= { {"Jones", 45, 65, 'D'}, {"Doe", 98, 79, 'B'}, {"Smith", 67, 76, 'C'}};
/*
..................
*/
  printf("%s %d %d %c\n", fall[0].Name, fall[0].Midterm, fall[0].Final, fall[0].Grade);
  return 0;
}
It is also possible to use a pointer to a structure as
#include <stdio.h>

struct student{
        char *Name;
        int Midterm;
        int Final;
        char Grade;
		};
int main()
{
  struct student Smith={"David Smith", 12, 45, 'F'}, *ptr;
  ptr = &Smith;
/*
..................
*/

  printf("%s %d %d %c\n", ptr->Name, ptr->Midterm, ptr->Final, ptr->Grade);
  return 0;
}
Note that a member in a structure can be referred by using ->.
Finally, by using typedef, you can define a structure and declare a variable as that structure type just like integer or float.
#include <stdio.h>
typedef struct {
        char *Name;
        int Midterm;
        int Final;
        char Grade;
      } student;

int main()
{
  student Jones={"Jones", 12, 45, 'F'}, *ptr;
  ptr = &Jones;
/*
..................
*/

 printf("%s\n", ptr->Name);
  return 0;
}
The concept of structure is extended to the concept of class which plays an essential role in C++ (and Java).
The following code defines the complex type (a + b i) and computes addition of two complex numbers:
#include <stdio.h>

typedef struct
 {float Real; float Im;} Complex;

Complex ComplexAdd(Complex z1, Complex z2)
{
	Complex z;
	z.Real = z1.Real + z2.Real;
	z.Im = z1.Im + z2.Im;
	return z;
}

int main()
{
	Complex z1, z2, z;

	printf("Enter real and imaginary parts of z1 separated by space = ");
	scanf("%f %f", &z1.Real, &z1.Im);

        printf("Enter real and imaginary parts of z2 separated by space = ");
	scanf("%f %f", &z2.Real, &z2.Im);

	z = ComplexAdd(z1, z2);

	printf("%f + %f I \n", z.Real, z.Im);
	return 0;
}

Closure on C syntax

  1. What happened to the arguments of main() ?
    See Lecture note #09.
  2. What does "return 0;" really mean ?
    The OS requires a return value when you run a program. Depending on the return value, the OS knows whether the program you run exit successfully or not.




File translated from TEX by TTH, version 4.03.
On 30 Jun 2025, 21:04.