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

C Structures (Structs)

C Structures (structs) are a way to group related variables of different data types into a single unit. They can be created using the 'struct' keyword, and members can be accessed using dot syntax. The document also covers how to assign values to structure members, copy structures, and modify their values.

Uploaded by

gjahnavi693
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)
27 views15 pages

C Structures (Structs)

C Structures (structs) are a way to group related variables of different data types into a single unit. They can be created using the 'struct' keyword, and members can be accessed using dot syntax. The document also covers how to assign values to structure members, copy structures, and modify their values.

Uploaded by

gjahnavi693
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

15/11/2024, 19:19 C Structures (structs)

 Tutorials  Exercises  Certificates  Services  Search...  Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files
ADVERTISEMENT
to Dubai
C Structures Your destination
from India
C Structures | Search
Book
Rs15,843

Your destination
C Enums
C Enums C Structures (structs) |

C Memory ❮ Previous Next ❯ Search

C Memory Management 

C Reference
C Reference
Structures
C Keywords
Structures (also called structs) are a way to group several related variables into
C <stdio.h>
one place. Each variable in the structure is known as a member of the structure.
C <stdlib.h>
C <string.h> Unlike an array, a structure can contain many different data types (int, float, char,
C <math.h> etc.).
C <ctype.h>
Book

C Examples Create a Structure to Mumbai


C Examples
from New Delhi
C Real-Life Examples You can create a structure by using the struct keyword and declare each of its

C Exercises members inside curly braces: Rs6,486

C Quiz
C Compiler
[Link] 1/15
15/11/2024, 19:19 C Structures (structs)

 Tutorials  Exercises  Certificates 


Services 
struct MyStructure { // Structure declaration
 Get Certified Sign Up Log in

int myNum; // Member (int variable)


HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files char myLetter; // Member (char variable)
}; // End the structure with a semicolon
C Structures
C Structures
To access the structure, you must create a variable of it.
C Enums
Use the struct keyword inside the main() method, followed by the name of the
C Enums
structure and then the name of the structure variable:

C Memory
C Memory Management  Create a struct variable with the name "s1":
COLOR
C Reference struct myStructure { PICKER
int myNum;
C Reference
char myLetter;
C Keywords };
C <stdio.h>
C <stdlib.h> int main() {
C <string.h> struct myStructure s1;

return 0;
C <math.h>
C <ctype.h>
} 
ADVERTISEMENT
C Examples
C Examples
C Real-Life Examples
Access Structure Members
C Exercises
C Quiz To access members of a structure, use the dot syntax ( . ):
C Compiler
[Link] 2/15
15/11/2024, 19:19 C Structures (structs)

 Tutorials  Exercises  Certificates 


Example Services   Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files // Create a structure called myStructure Your destination
struct myStructure {
C Structures int myNum; |
char myLetter;
C Structures Search
};

C Enums int main() {


C Enums // Create a structure variable of myStructure called s1
struct myStructure s1;
C Memory
// Assign values to members of s1
C Memory Management  [Link] = 13;
[Link] = 'B';
C Reference
C Reference // Print values
C Keywords printf("My number: %d\n", [Link]);
printf("My letter: %c\n", [Link]); Book
C <stdio.h>
C <stdlib.h> to Calgary
return 0;
C <string.h>
} from Zurich
C <math.h>
C <ctype.h>
Rs53,527
Try it Yourself »

C Examples
C Examples
Now you can easily create multiple structure variables with different values, using
C Real-Life Examples
just one structure:
C Exercises
C Quiz
C Compiler
[Link] 3/15
15/11/2024, 19:19 C Structures (structs)

 Tutorials  Exercises  Certificates 


Example Services   Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files // Create different struct variables
struct myStructure s1;
C Structures struct myStructure s2;

C Structures
// Assign values to different struct variables
[Link] = 13;
C Enums [Link] = 'B';
C Enums
[Link] = 20;
C Memory [Link] = 'C';

C Memory Management 
Try it Yourself »
C Reference
C Reference
C Keywords
ADVERTISEMENT
C <stdio.h>
C <stdlib.h>
C <string.h>
C <math.h>
C <ctype.h>

Bengaluru ✈︎Mumbai
C Examples
C Examples from Rs5,611 Info
*

C Real-Life Examples
C Exercises
C Quiz
What About Strings in Structures?
C Compiler
[Link] 4/15
15/11/2024, 19:19 C Structures (structs)

Remember that strings in C are actually an array of characters, and unfortunately,


 Tutorials  Exercises  Certificates
you 
can't assign aServices
value to an array like this:  Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files
Example
C Structures
struct myStructure {
C Structures
int myNum;
char myLetter;
C Enums char myString[30]; // String
C Enums };

C Memory int main() {


struct myStructure s1;
C Memory Management 
// Trying to assign a value to the string
C Reference [Link] = "Some text";
C Reference
C Keywords // Trying to print the value
C <stdio.h> printf("My string: %s", [Link]);

C <stdlib.h>
return 0;
C <string.h>
}
C <math.h>
C <ctype.h>
An error will occur:

C Examples prog.c:12:15: error: assignment to expression with array type


C Examples
C Real-Life Examples Try it Yourself »
C Exercises
C Quiz
C Compiler
[Link] 5/15
15/11/2024, 19:19 C Structures (structs)

However, there is a solution for this! You can use the strcpy() function and
 Tutorials  Exercises  Certificates  Services 
assign the value to [Link] , like this:
 Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files
Example
C Structures
C Structures struct myStructure {
int myNum;
char myLetter;
C Enums
char myString[30]; // String
C Enums };

C Memory int main() {


C Memory Management struct myStructure s1;

// Assign a value to the string using the strcpy function
C Reference
strcpy([Link], "Some text");
C Reference
C Keywords // Print the value
C <stdio.h> printf("My string: %s", [Link]);
C <stdlib.h>
C <string.h> return 0;
}
C <math.h>
C <ctype.h>
Result:

C Examples
My string: Some text
C Examples
C Real-Life Examples
Try it Yourself »
C Exercises
C Quiz
C Compiler
[Link] 6/15
15/11/2024, 19:19 C Structures (structs)

 Tutorials  Exercises  Simpler


Certificates  Syntax
Services   Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQLYou can


PYTHON JAVA
also assign valuesPHP HOW TO
to members [Link] variable
of a structure C C++ C#
at declarationBOOTSTRAP
time, REACT MYSQ

C Read Files
in a single line.

C Structures Just insert the values in a comma-separated list inside curly braces {} . Note that
C Structures you don't have to use the strcpy() function for string values with this technique:

C Enums
C Enums Example

C Memory // Create a structure


struct myStructure {
C Memory Management  int myNum;
char myLetter;
C Reference char myString[30];
C Reference };
C Keywords
C <stdio.h> int main() {
// Create a structure variable and assign values to it
C <stdlib.h>
struct myStructure s1 = {13, 'B', "Some text"};
C <string.h>
C <math.h> // Print values
C <ctype.h> printf("%d %c %s", [Link], [Link], [Link]);

C Examples return 0;
}
C Examples
C Real-Life Examples
C Exercises Try it Yourself »
C Quiz
C Compiler
[Link] 7/15
15/11/2024, 19:19 C Structures (structs)

 Tutorials  Exercises  Certificates  Services   Get Certified


Note: The order of the inserted values must match the order of the variable types
Sign Up Log in

HTML CSS JAVASCRIPT SQLdeclared in the structure


PYTHON JAVA (13
PHPfor int,
HOW 'B'TO
for char, etc).
[Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files

C Structures
C Structures
Copy Structures
C Enums
You can also assign one structure to another.
C Enums
In the following example, the values of s1 are copied to s2:
C Memory
C Memory Management 
Example
C Reference
struct myStructure s1 = {13, 'B', "Some text"};
C Reference
struct myStructure s2;
C Keywords
C <stdio.h> s2 = s1;
C <stdlib.h>
C <string.h>
Try it Yourself »
C <math.h>
C <ctype.h>

C Examples
C Examples Modify Values
C Real-Life Examples
C Exercises If you want to change/modify a value, you can use the dot syntax ( . ).

C Quiz
And to modify a string value, the strcpy() function is useful again:
C Compiler
[Link] 8/15
15/11/2024, 19:19 C Structures (structs)

 Tutorials  Exercises  Certificates 


Example Services   Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files struct myStructure {
int myNum;
C Structures char myLetter;
char myString[30];
C Structures
};

C Enums int main() {


C Enums // Create a structure variable and assign values to it
struct myStructure s1 = {13, 'B', "Some text"};
C Memory
// Modify values
C Memory Management  [Link] = 30;
[Link] = 'C';
C Reference strcpy([Link], "Something else");
C Reference
C Keywords // Print values
printf("%d %c %s", [Link], [Link], [Link]);
C <stdio.h>
C <stdlib.h>
return 0;
C <string.h>
}
C <math.h>
C <ctype.h>
Try it Yourself »

C Examples
C Examples
Modifying values are especially useful when you copy structure values:
C Real-Life Examples
C Exercises
C Quiz
Example
C Compiler
[Link] 9/15
15/11/2024, 19:19 C Structures (structs)

 Tutorials  Exercises 
// Create a structure variable and assign values to it
Certificates  Services  
struct myStructure s1 = {13, 'B', "Some text"};
Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files // Create another structure variable
struct myStructure s2;
C Structures
// Copy s1 values to s2
C Structures
s2 = s1;

C Enums // Change s2 values


C Enums [Link] = 30;
[Link] = 'C';
C Memory strcpy([Link], "Something else");

C Memory Management  // Print values


printf("%d %c %s\n", [Link], [Link], [Link]);
C Reference printf("%d %c %s\n", [Link], [Link], [Link]);
C Reference
C Keywords Try it Yourself »
C <stdio.h>
C <stdlib.h>
C <string.h>
C <math.h>
Ok, so, how are structures useful?
C <ctype.h>
Imagine you have to write a program to store different information about Cars,
C Examples such as brand, model, and year. What's great about structures is that you can
create a single "Car template" and use it for every cars you make. See below for a
C Examples
real life example.
C Real-Life Examples
C Exercises
C Quiz
C Compiler
[Link] 10/15
15/11/2024, 19:19 C Structures (structs)

 Tutorials  Exercises  Real-Life


Certificates  Example
Services   Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQLUse aPYTHON JAVA


structure to PHP
store different HOW TO
information [Link]
about Cars:C C++ C# BOOTSTRAP REACT MYSQ

C Read Files

C Structures
Example
C Structures

struct Car {
C Enums char brand[50];
C Enums char model[50];
int year;
C Memory };

C Memory Management  int main() {


struct Car car1 = {"BMW", "X5", 1999};
C Reference struct Car car2 = {"Ford", "Mustang", 1969};
C Reference struct Car car3 = {"Toyota", "Corolla", 2011};
C Keywords
printf("%s %s %d\n", [Link], [Link], [Link]);
C <stdio.h>
printf("%s %s %d\n", [Link], [Link], [Link]);
C <stdlib.h>
printf("%s %s %d\n", [Link], [Link], [Link]);
C <string.h>
C <math.h> return 0;
C <ctype.h> }

C Examples Try it Yourself »


C Examples
C Real-Life Examples
C Exercises
C Quiz
C Compiler
[Link] 11/15
15/11/2024, 19:19 C Structures (structs)

 Tutorials  Exercises  Certificates  Services   Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files

C Structures
❮ Previous Next ❯
C Structures

C Enums
W3schools Pathfinder
C Enums
Track your progress - it's free! Sign Up Log in

C Memory
C Memory Management 

C Reference
C Reference ADVERTISEMENT

C Keywords
C <stdio.h>
Your destination |
C <stdlib.h>
C <string.h>
to Dubai
C <math.h>
from New Delhi
C <ctype.h>

C Examples Book
Rs20,366
C Examples
C Real-Life Examples
ADVERTISEMENT
C Exercises
C Quiz
C Compiler
[Link] 12/15
15/11/2024, 19:19 C Structures (structs)

 Tutorials  Exercises  Certificates  Services   Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files

C Structures
C Structures

C Enums
C Enums

C Memory
C Memory Management 

C Reference
C Reference
C Keywords  PLUS SPACES GET CERTIFIED FOR TEACHERS

C <stdio.h>
C <stdlib.h>
C <string.h>
FOR BUSINESS CONTACT US
C <math.h>
C <ctype.h>
Top Tutorials Top References
C Examples
HTML Tutorial HTML Reference
C Examples CSS Tutorial CSS Reference
JavaScript Tutorial JavaScript Reference
C Real-Life Examples How To Tutorial SQL Reference
SQL Tutorial Python Reference
C Exercises Python Tutorial [Link] Reference
C Quiz [Link] Tutorial Bootstrap Reference
Bootstrap Tutorial PHP Reference
C Compiler PHP Tutorial HTML Colors
[Link] 13/15
15/11/2024, 19:19 C Structures (structs)
Java Tutorial Java Reference

 Tutorials  Exercises  CertificatesC++


 Tutorial
Services 
jQuery Tutorial
Angular Reference

jQuery Reference
Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQL PYTHON


Top Examples
JAVA PHP HOW TO
Get Certified
[Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
C Structures How To Examples Front End Certificate
SQL Examples SQL Certificate
C Structures Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
C Enums PHP Examples Java Certificate
Java Examples C++ Certificate
C Enums XML Examples C# Certificate
jQuery Examples XML Certificate

C Memory
C Memory Management 
     FORUM ABOUT ACADEMY
C Reference W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
C Reference Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness
C Keywords of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and
C <stdio.h> privacy policy.

C <stdlib.h>
C <string.h>
C <math.h>
C <ctype.h>

C Examples
C Examples
C Real-Life Examples
C Exercises
C Quiz
C Compiler
[Link] 14/15
15/11/2024, 19:19 C Structures (structs)
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by [Link].

 Tutorials  Exercises  Certificates  Services   Get Certified Sign Up Log in

HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C C++ C# BOOTSTRAP REACT MYSQ

C Read Files

C Structures
C Structures

C Enums
C Enums

C Memory
C Memory Management 

C Reference
C Reference
C Keywords
C <stdio.h>
C <stdlib.h>
C <string.h>
C <math.h>
C <ctype.h>

C Examples
C Examples
C Real-Life Examples
C Exercises
C Quiz
C Compiler
[Link] 15/15

You might also like