100% tevredenheidsgarantie Direct beschikbaar na je betaling Lees online óf als PDF Geen vaste maandelijkse kosten 4.2 TrustPilot
logo-home
Overig

Structures and Unions in C: A Complete Guide with Examples

Beoordeling
-
Verkocht
-
Pagina's
6
Geüpload op
21-01-2025
Geschreven in
2024/2025

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 choose a structure over a union. The guide includes step-by-step examples to demonstrate practical applications, such as defining complex data types and managing memory efficiently. Perfect for second-year Computer Science students, this document will help you understand these fundamental concepts and improve your C programming skills.

Meer zien Lees minder
Instelling
Vak









Oeps! We kunnen je document nu niet laden. Probeer het nog eens of neem contact op met support.

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
21 januari 2025
Aantal pagina's
6
Geschreven in
2024/2025
Type
Overig
Persoon
Onbekend

Onderwerpen

Voorbeeld van de inhoud

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};

// Accessing structure members
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.2f\n", person1.height);

return 0;
}

, In this example:

 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;
}
€4,58
Krijg toegang tot het volledige document:

100% tevredenheidsgarantie
Direct beschikbaar na je betaling
Lees online óf als PDF
Geen vaste maandelijkse kosten

Maak kennis met de verkoper
Seller avatar
rileyclover179

Ook beschikbaar in voordeelbundel

Maak kennis met de verkoper

Seller avatar
rileyclover179 US
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
0
Lid sinds
11 maanden
Aantal volgers
0
Documenten
252
Laatst verkocht
-

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Veelgestelde vragen