C allows the creation of a user-defined variable. A situation can arise where one needs to store different but logically related values under a single variable. An example is product information. It can consist of the following attributes
- id (int)
- name (string)
- price (float)
A structure/struct allows creating a user-defined data type (Product) consisting of different data types (float, string, price) under a single type.
To create a structure, a struct keyword is used and the consisting different datatypes that it holds are called members.
Syntax
struct struct_name {
datatype member1;
dataype member2;
…
};
Creating the structure of the product example would be -
struct Product
{
int id;
char *name;
float price;
};
Datatype Product has been created and a variable can be declared in the following two ways.
struct Product p;
whereby Product = datatype and p = variable. Or
struct Product
{
int id;
char *name;
float price;
} p;
Initializing and Accessing Structures
Structures cannot be initialized upon declaration but instead once the struct has been created. In a process known as designated initialization, structure members can be initialized in no particular through using a dot operator to access the members.
struct Product p = {.price = 89.45, .id = 1234, .name = "Watch"}
- id (int)
- name (string)
- price (float)
A structure/struct allows creating your data type consisting of different data types under a single name
own_datatype{
//contains different types of data types
}
To create a structure, a struct keyword is used and the declared datatypes are called members.
Syntax
struct struct_name {
datatype member1;
dataype member2;
…
};
Creating the structure of the product example would be
struct Product
{
int id;
char *name;
float price;
};
Datatype Product has been created and a variable can be declared in the following two ways.
struct Product p;
whereby Product = datatype and p = variable. Or
struct Product
{
int id;
char *name;
float price;
} p;
Declaring and Initializing
Structures cannot be initialized upon declaration but instead once the struct has been created. The elements can be initialized in no particular order through a process known as designated initialization. The dot operator (.) is used to access the members and set values.
struct Product p = {.id = 1234, .name = "John", .price = 21.4}
Product p variable is declared and initialized.
Accessing Members
A struct variable is used to access the members of the struct. Elements can be accessed by
- using the dot operator (.)
struct Product p; p.name = "John";
- using a pointer
struct Product *ptr; ptr -> name = "John";
Example in program
#include <stdio.h>
struct Product
{
int id;
char *name;
float price;
};
int main()
{
struct Product p;
p.id = 1204;
p.name = "Watch";
p.price = 24.56;
printf("id: %d name: %s price: %.2f \n", p.id, p.name, p.price);
return 0;
}
Using structure in functions
Functions can return a struct type and contain struct variables on its parameters.
- Function that returns a struct type ``` #include
struct Product { int id; char *name; float price; };
struct Product funcStruct() { struct Product p; p.name = "Jane"; p.id = 1204; p.price = 24.56; return p; }; int main() { struct Product p;
p = funcStruct();
printf("id: %d name: %s price: %.2f \n", p.id, p.name, p.price);
func(p);
return 0;
}
- Function that uses struct type as arguments
#include <stdio.h>
#include <stdlib.h>
struct Product
{
int id;
char *name;
float price;
};
void func(struct Product p)##
{
p.name = "Jane";
p.id = 1204;
p.price = 24.56;
printf("id: %d name: %s price: %.2f \n", p.id, p.name, p.price);
}
int main()
{
struct Product p;
func(p);
return 0;
}
Typedef
This is a keyword that allows creating a new name for a datatype.
Syntax
typedef data_type new_name;
The new name can be used to create variables instead of using datatype.
typedef char myChar;
myChar a = 'g';
printf("%c", a);
Typedef is convenient to use with structures. Instead of rewriting "struct Product p" to use, a datatype of type struct can be generated and used instead to declare struct variables.
Example in a program
#include <stdio.h>
#include <stdlib.h>
typedef struct Product
{
char *name;
float price;
} product;
int main()
{
product p[] =
{
{"Watch", 2.45},
{"Tshirt", 1.45},
{"Shoes", 4.45},
};
int i;
for (i=0; i < 3; i++)
{
printf("Name: %s Price: %.2f \n", p[i].name, p[i].price);
}
return 0;
}
This is how to create struct and typedef and how to use both concepts together. I hope this write-up has been of help to you.