0% found this document useful (0 votes)
35 views

C Structure and Function

A C structure allows grouping of variables of different data types together under a single name. It is similar to a class in that both hold a collection of data types. A structure creates a user-defined data type for grouping items under a single data type. Structures are defined using the struct keyword followed by an identifier. Members can then be declared inside curly braces. Structures can be passed as arguments to functions to allow accessing and printing their member values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

C Structure and Function

A C structure allows grouping of variables of different data types together under a single name. It is similar to a class in that both hold a collection of data types. A structure creates a user-defined data type for grouping items under a single data type. Structures are defined using the struct keyword followed by an identifier. Members can then be declared inside curly braces. Structures can be passed as arguments to functions to allow accessing and printing their member values.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

C Structure

and Function
Structure
a collection of variables of different data types under a
single name. It is similar to a class in that, both holds a
collection of data of different data types.

A STRUCT is a C data structure that can be used to store


together elements of different data types. In C, a structure
is a user-defined data type. The structure creates a data type
for grouping items of different data types under a single
data type.
For example:

You want to store some information about a person:

his/her name, citizenship number and salary. You can easily


create different variables name, citNo, salary to store these
information separately.

However, in the future, you would want to store


information about multiple persons. Now, you'd need to
create different variables for each information per person:
name1, citNo1, salary1, name2, citNo2, salary2
When to use C Struct Initialization
a Structure?
To create a C structure, we use the
struct keyword, followed by an
Here are some reasons identifier. The identifier becomes the
using structure in C. name of the struct. Here is the syntax
for creation of a C struct:
Use a struct when you need to store
elements of different data types under Syntax:
one data type.
C structs are a value type rather than struct struct_name
being a reference type. Use a struct if {
you don’t intend to modify your data // struct members
after creation.
}
How to declare a structure Then inside the curly braces, you
can declare one or more members
in C programming? (declare variables inside curly
braces) of that structure.

Struct
For example:
struct Person
{
The struct keyword defines a char name[50];
structure type followed by an int age;
float salary;
identifier (name of the };
structure).
Here a structure person is defined
which has three members: name,
age and salary.
Creating Struc #include <stdio.h>

Instances struct Person


{
int citizenship;
Creating Struct int age;
Instances: };

Person p; int main()


{
Accessing Struct Person p;
Members return 0;
}
p.Age = 27;
Another way of creating a struct variable is:

struct Person {
// code
} person1, person2, p[20];
In both cases,

person1 and person2 are struct Person variables


p[] is a struct Person array of size 20.
int main() {

// assign value to name of person1


#include <stdio.h> strcpy(person1.name, “Minerva Magbitang");
#include <string.h>
// assign values to other person1 variables
// create struct with person1 variable person1.citNo = 1984;
struct Person { person1. salary = 2500;
char name[50];
int citNo; // print struct variables
float salary; printf("Name: %s\n", person1.name);
} person1; printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);

return 0;
}
Struct as
Function
Argument
You can pass a struct to a function as an
argument. This is done in the same way as
passing a normal argument. The struct variables
can also be passed to a function. A good
example is when you need to display the values
of struct members.
#include <stdio.h>
#include <string.h>

struct Person
{
int citizenship;
int age;
};
void func(struct Person p);

int main()
{
struct Person p;
p.citizenship = 1;
p.age = 27;

func(p);
return 0;
}
void func(struct Person p)
{
printf(" Person citizenship: %d\n",p.citizenship);
printf(" Person age: %d",p.age);
}
Limitation of a C++
Structure
• The struct data type cannot be
treated like built-in data types. • Static members cannot be
• Operators like + -, and others declared inside the
cannot be used on structure structure body.
variables. • Constructors cannot be
• The members of a structure can created inside a structure.
be accessed by any function
regardless of its scope.

You might also like