C - Structured Datatypes

C - Programming HOME
C - Basic Introduction
C - Program Structure
C - Reserved Keywords
C - Basic Datatypes
C - Variable Types
C - Storage Classes
C - Using Constants
C - Operator Types
C - Control Statements
C - Input and Output
C - Pointing to Data
C - Using Functions
C - Play with Strings
C - Structure Datatype
C - Working with Files
C - Bits Manipulation
C - Pre-Processors
C - Useful Concepts
C - Built-in Functions
C - Useful Resources
Computer Glossary
Who is Who
Copyright © 2014 by tutorialspoint
A structure in C is a collection of items of different types. You can think of a structure as a "record" is in Pascal or a class in Java without methods.
A structure in C is a collection of items of different types. You can think of a structure as a "record" is in Pascal or a class in Java without methods.
Structures, or structs, are very useful in creating data structures larger and more complex than the ones we have discussed so far.
Structures, or structs, are very useful in creating data structures larger and more complex than the ones we have discussed so far.
Simply you can group various built-in data types into a structure.
Simply you can group various built-in data types into a structure.
Object conepts was derived from Structure concept. You can achieve few object oriented goals using C structure but it is very complex.
Object conepts was derived from Structure concept. You can achieve few object oriented goals using C structure but it is very complex.
Following is the example how to define a structure.
struct student {
    char firstName[20];
    char lastName[20];
    char SSN[9];
    float gpa;
  };

Now you have a new datatype called student and you can use this datatype define your variables of student type:
struct student student_a, student_b;

or an array of students as

struct student students[50];

Another way to declare the same thing is:
struct {
    char firstName[20];
    char lastName[20];
    char SSN[10];
    float gpa;
  } student_a, student_b;

All the variables inside an structure will be accessed using these values as student_a.firstName will give value of firstName variable. Similarly we can aqccess other variables.
Try out following example to understand the concept:
#include <stdio.h>
struct student {
    char firstName[20];
    char lastName[20];
    char SSN[10];
    float gpa;
};

main()
{
  struct student student_a;

  strcpy(student_a.firstName, "Deo");
  strcpy(student_a.lastName, "Dum");
  strcpy(student_a.SSN, "2333234" );
  student_a.gpa = 2009.20;

 printf( "First Name: %s\n", student_a.firstName );
 printf( "Last Name: %s\n", student_a.lastName );
 printf( "SNN : %s\n", student_a.SSN );
 printf( "GPA : %f\n", student_a.gpa );
}

This will produce following results:
First Name: Deo
Last Name: Dum
SSN : 2333234
GPA : 2009.20

Sometimes it is useful to assign pointers to structures (this will be evident in the next section with self-referential structures). Declaring pointers to structures is basically the same as declaring a normal pointer:
struct student *student_a;

To dereference, you can use the infix operator: ->.
printf("%s\n", student_a->SSN);

There is an easier way to define structs or you could "alias" types you create. For example:
typedef struct{
    char firstName[20];
    char lastName[20];
    char SSN[10];
    float gpa;
  }student;

Now you can use student directly to define variables of student type  without using struct keyword. Following is the example:
student student_a;

You can use typedef for non-structs:
typedef long int *pint32;

pint32 x, y, z;

x, y and z are all pointers to long ints
Unions are declared in the same fashion as structs, but have a fundamental difference. Only one item within the union can be used at any time, because the memory allocated for each item inside the union is in a shared memory location.
Here is how we define a Union
union Shape {
      int circle;
      int triangle;
      int ovel;
};

We use union in such case where only one condition will be applied and only one variable will be used.
You can create arrays of structs.
Structs can be copied or assigned.
The & operator may be used with structs to show addresses.
Structs can be passed into functions. Structs can also be returned from functions.
Structs cannot be compared!
Structures can store non-homogenous data types into a single collection, much like an array does for common data (except it isn't accessed in the same manner).
Pointers to structs have a special infix operator: -> for dereferencing the pointer.
typedef can help you clear your code up and can help save some keystrokes.

Advertisements


    6 Lectures 
     1.5 hours 

 Mr. Pradeep Kshetrapal

    41 Lectures 
     5 hours 

 AR Shankar

    11 Lectures 
     58 mins

 Musab Zayadneh

    59 Lectures 
     15.5 hours 

 Narendra P

    11 Lectures 
     1 hours 

 Sagar Mehta

    39 Lectures 
     4 hours 

 Vikas Yadav
 Print
 Add Notes
 Bookmark this page