This document provides a comprehensive guide to structures and unions in C programming. It covers the key concepts of structure declaration, initialization, and accessing members, along with the differences between structures and unions. You’ll learn how to use unions to save memory, and when to ...
Structures and Unions in C
A structure and a union are both user-defined data types in C, used for grouping
different types of variables together under a single name. They differ in how they
manage memory, which affects how the data is stored and accessed.
1. Structures in C
A structure is a collection of variables of different data types grouped together
under one name. Each variable within the structure is known as a member, and
members can be of different data types, including arrays, pointers, and other
structures.
Declaration and Initialization of a Structure
To define a structure, you use the struct keyword. Here’s how you can define and
initialize a structure in C:
#include <stdio.h>
struct Person {
char name[50];
int age;
float height;
};
int main() {
// Declaring and initializing a structure variable
struct Person person1 = {"Alice", 25, 5.6};
struct Person is the structure definition.
person1 is a variable of type struct Person.
The members name, age, and height are accessed using the dot operator
(.).
Accessing Structure Members
You can access the members of a structure using the dot operator (.) if you have
an instance of the structure. For example, person1.name accesses the name
member of the person1 instance.
To access members of a structure through a pointer, you would use the arrow
operator (->).
Example using a pointer to a structure:
#include <stdio.h>
struct Person {
char name[50];
int age;
float height;
};
int main() {
struct Person person1 = {"Bob", 30, 5.9};
struct Person *ptr = &person1;
// Accessing structure members via pointer
printf("Name: %s\n", ptr->name);
printf("Age: %d\n", ptr->age);
printf("Height: %.2f\n", ptr->height);
return 0;
}
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller rileyclover179. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $7.19. You're not tied to anything after your purchase.