0% found this document useful (0 votes)
37 views16 pages

Structures

Structures allow grouping of related data types into a single type. They can contain multiple data types like integers, floats, characters. Structures provide a way to organize related data and treat a group of variables as one single variable of structure type. Pointers can be used to access members of a structure variable. Bit fields help reduce memory usage by specifying size of structure members in bits. Structures are commonly used to define custom data types, organize large amounts of data, and create data structures.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views16 pages

Structures

Structures allow grouping of related data types into a single type. They can contain multiple data types like integers, floats, characters. Structures provide a way to organize related data and treat a group of variables as one single variable of structure type. Pointers can be used to access members of a structure variable. Bit fields help reduce memory usage by specifying size of structure members in bits. Structures are commonly used to define custom data types, organize large amounts of data, and create data structures.

Uploaded by

LEESHMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Structures

• Structures (also called structs) are a way to group several related variables
into one place. Each variable in the structure is known as a member of the
structure.

• Unlike an array, a structure can contain many different data types (int,
float, char, etc.).
Heterogeneous Structures
 Collection of values of possibly different types.
 Name the collection.
Name the components.
 Example : Student record
Singhal
name "V Singhal"

rollno "00CS1001"

classtest 14

midterm 78

final 73

grade ‘B
Structure : terminology
 A struct is a group of items (variables) which may be of
different types.
 Each item is identified by its own identifier, each of
which is known as a member or field of the structure.
 A struct is sometimes called a record or structure.
 Structs are the basis of classes in C++ and Java.
#include <stdio.h> // Assign values to members of s1
s1.myNum = 13;
// Create a structure called myStructure s1.myLetter = 'B';
struct myStructure {
// Print values
int myNum; printf("My number: %d\n", s1.myNum);
char myLetter; printf("My letter: %c\n", s1.myLetter);
};
return 0;
int main() { }
// Create a structure variable of myStructure called s1
struct myStructure s1;
// Create different struct variables
struct myStructure s1;
struct myStructure s2;

// Assign values to different struct variables


s1.myNum = 13;
s1.myLetter = 'B';

s2.myNum = 20;
s2.myLetter = 'C';
#include <stdio.h>
#include <string.h>

struct myStructure {
int myNum;
char myLetter;
char myString[30]; // String
};

int main() {
struct myStructure s1;

// Assign a value to the string using the strcpy function


strcpy(s1.myString, "Some text");

// Print the value


printf("My string: %s", s1.myString);

return 0;
}
Simpler Syntax

• You can also assign values to members of a structure


variable at declaration time, in a single line.
• Just insert the values in a comma-separated list inside curly
braces {}.
• Note that you don't have to use the strcpy() function for
string values with this technique
#include <stdio.h>

// Create a structure
struct myStructure {
int myNum;
char myLetter;
char myString[30];
};
int main() {
// Create a structure variable and assign values to it
struct myStructure s1 = {13, 'B', "Some text"};

// Print values
printf("%d %c %s", s1.myNum, s1.myLetter, s1.myString);

return 0;
}
Typedef in C
• The typedef is a keyword used in C programming to provide some
meaningful names to the already existing variable in the C program.
• It behaves similarly as we define the alias for the commands. In short, we
can say that this keyword is used to redefine the name of an already existing
variable.
Syntax of typedef
typedef <existing_name> <alias_name>

In the above syntax, 'existing_name' is the name of an already existing variable


while 'alias name' is another name given to the existing variable.
The C programming language provides a keyword called typedef,
which you can use to give a type a new name. Following is an example
to define a term BYTE for one-byte numbers −

typedef unsigned char BYTE;

After this type definition, the identifier BYTE can be used as an


abbreviation for the type unsigned char, for example..

By convention, uppercase letters are used for these definitions to remind the user that the type
BYTE b1, b2; name is really a symbolic abbreviation, but you can use lowercase, as follows −
typedef unsigned char byte;
Pointers to Structures

You can define pointers to structures in the same way as you define pointer to any other variable

struct Books *struct_pointer;

Now, you can store the address of a structure variable in the above defined pointer variable. To
find the address of a structure variable, place the '&'; operator before the structure's name as
follows −
struct_pointer = &Book1;

To access the members of a structure using a pointer to that structure, you must use the →
operator as follows −
struct_pointer->title;
Bit Fields

• Bit Fields are used to specify the length of the structure members in
bits.
• When we know the maximum length of the member, we can use bit
fields to specify the size and reduce memory consumption.

• Syntax of Bit Fields


struct structure_name {
data_type member_name: width_of_bit-field;
};
Uses of Structure in C

• The structure can be used to define the custom data types that
can be used to create some complex data types such as dates,
time, complex numbers, etc. which are not present in the
language.
• It can also be used in data organization where a large amount of
data can be stored in different fields.
• Structures are used to create data structures such as trees, linked
lists, etc.
• They can also be used for returning multiple values from a
function.
Limitations of C Structures

• Higher Memory Consumption: It is due to structure padding.


• No Data Hiding: C Structures do not permit data hiding. Structure
members can be accessed by any function, anywhere in the scope of
the structure.
• Functions inside Structure: C structures do not permit functions
inside the structure so we cannot provide the associated functions.
• Static Members: C Structure cannot have static members inside its
body.
• Construction creation in Structure: Structures in C cannot have a
constructor inside Structures.

You might also like