// Create a struct variable and assign it a value
struct myStructure s1 = {
13, 'B'
};
// modify the value
s1.myNum = 30;
s1.myLetter = 'C';
// print value
printf("%d %c %s",
s1.myNum,
s1.myLetter);
C Structures
Modify value
Copy structure
struct myStructure s1 = {
13, 'B', "Some text"
};
struct myStructure s2;
s2 = s1;
In the example, the value of s1
is copied to s2
Accessing structure members
// create a structure called myStructure
struct myStructure {
int myNum;
char myLetter;
};
int main() {
// Create a structure variable called myStructure called s1
struct myStructure s1;
// Assign values ââto the members of s1
s1.myNum = 13;
s1.myLetter = 'B';
// Create a structure variable of myStructure called s2
// and assign it a value
struct myStructure s2 = {13, 'B'};
// print value
printf("My number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);
return 0;
}
Create different structure variables
struct myStructure s1;
struct myStructure s2;
// Assign values ââto different structure variables
s1.myNum = 13;
s1.myLetter = 'B';
s2.myNum = 20;
s2.myLetter = 'C';
Strings in the structure
struct myStructure {
int myNum;
char myLetter;
char myString[30]; // String
};
int main() {
struct myStructure s1;
strcpy(s1. myString, "Some text");
// print value
printf("my string: %s", s1.myString);
return 0;
}
Assigning values ââto strings using the strcpy
function
Create structure
struct MyStructure { // structure declaration
int myNum; // member (int variable)
char myLetter; // member (char variable)
}; // end the structure with a semicolon
Create a struct variable called s1
struct myStructure {
int myNum;
char myLetter;
};
int main() {
struct myStructure s1;
return 0;
}
Comments