0% found this document useful (0 votes)
7 views15 pages

Understanding Structures in C Programming

The document provides an overview of structures in programming, defining them as collections of variables of different types under a single name. It explains how to define a structure using the 'struct' statement, access its members using the member operator and structure pointer operator, and highlights the advantages of using structures for organizing related data. Examples illustrate structure definition, variable creation, and member access methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

Understanding Structures in C Programming

The document provides an overview of structures in programming, defining them as collections of variables of different types under a single name. It explains how to define a structure using the 'struct' statement, access its members using the member operator and structure pointer operator, and highlights the advantages of using structures for organizing related data. Examples illustrate structure definition, variable creation, and member access methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

STRUCTURES

COMSCI 1200
GBS
WHAT IS A STRUCTURE?
•A collection of variables of different types under a
single name.
•All the structure elements are stored at contiguous
memory locations.
•Are used to represent a record
DEFINING A STRUCTURE
To define a structure, you must use the struct
statement. The struct statement defines a new
data type, with more than one member.
Syntax:
struct [structure tag] {
member definition;
member definition;
…..
} [one or more structure variables];
CONTINUATION
The structure tag is optional and each member
definition is a normal variable definition, such as
int i; or float f; or any other valid variable
definition. At the end of the structure's definition,
before the final semicolon, you can specify one or
more structure variables but it is optional.
EXAMPLES
struct Books { struct person
char title[50]; {
char author[50]; char name[50];
char subject[100]; int citNo;
int book_id; float salary;
} book; };
ADVANTAGES OF STRUCTURE
Here are pros/benefits for using structure:
✓ Structures gather more than one piece of data about the same subject
together in the same place.
✓ It is helpful when you want to gather the data of similar data types and
parameters like first name, last name, etc.
✓ It is very easy to maintain as we can represent the whole record by using
a single name.
✓ In structure, we can pass complete set of records to any function using a
single parameter.
✓ You can use an array of structure to store more records with similar types.
STRUCTURE VARIABLE DEFINITION
When a structure is defined, it creates a user-
defined type but, no storage or memory is
allocated.
EXAMPLE
struct person int main()
{ {
char name[50]; struct person person1,
int citNo; person2, person3[20];
float salary; return 0;
}; }
EXAMPLE CONTINUATION
Another way of creating a structure variable is:
struct person
{
char name[50];
int citNo;
float salary;
} person1, person2, person3[20];
ACCESSING MEMBERS OF A STRUCTURE
There are two types of operators used for
accessing members of a structure.

1. Member operator(.)
2. Structure pointer operator(->)
// Create a structure called myStructure
struct myStructure {
int myNum;
char myLetter;
};

int main() {
// Create a structure variable of myStructure called s1
struct myStructure s1;

// Assign values to members of s1


[Link] = 13;
[Link] = 'B';

// Print values
printf("My number: %d\n", [Link]);
printf("My letter: %c\n", [Link]);

return 0;
}
struct myStructure { struct myStructure {
int myNum; int myNum;
char myLetter; char myLetter;
char myString[30]; // String char myString[30]; // String
}; };
int main() {
int main() { struct myStructure s1;
struct myStructure s1;
// Assign a value to the string
// Trying to assign a value to using the strcpy function
the string strcpy([Link], "Some text");
[Link] = "Some text";
// Print the value
// Trying to print the value printf("My string: %s",
printf("My string: [Link]);
%s", [Link]); return 0;
}
return 0;
}
// Create a structure You can also assign values to
struct myStructure {
int myNum; members of a structure variable at
char myLetter;
char myString[30]; declaration time, in a single line.
}; Just insert the values in a comma-
int main() { separated list inside curly braces {}.
// Create a structure variable Note that you don't have to use
and assign values to it
struct myStructure s1 = the strcpy() function for string values
{13, 'B', "Some text"}; with this technique
// Print values
printf("%d %c %s", [Link],
[Link], [Link]);
return 0;
}
To access members of a structure using pointers, we use the -> operator.
#include <stdio.h>
struct person
{
int age;
float weight; In this example, the address of person1
}; is stored in the personPtr pointer using
personPtr = &person1;.
int main()
{
struct person *personPtr, person1; Now, you can access the members of
personPtr = &person1; person1 using the personPtr pointer.
printf("Enter age: ");
scanf("%d", &personPtr->age);
personPtr->age is equivalent to
printf("Enter weight: "); (*personPtr).age
scanf("%f", &personPtr->weight); personPtr->weight is equivalent to
printf("Displaying:\n"); (*personPtr).weight
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}
End of slide!

You might also like