CENG 110 W1 Structuresr
CENG 110 W1 Structuresr
Week 2
Homeworks 25
Midterm Exam 30
Final Exam 45
©2016 Pearson Education, Inc., Hoboken,
NJ. All rights reserved.
10.1 Introduction
char *suit; // A pointer to a string representing the suit value (e.g., «Hearts", «Spades")
};
• Keyword struct introduces the structure
definition.
• The identifier card is the structure tag, which
names the structure definition and is used with
struct to declare variables of the structure type—
e.g., struct card.
• Only specific members are passed, not the whole • cout << "Name: " << name << ", Age: " << age << ",
structure. Salary: $" << salary << endl;
• • }
• int age; • }
};
cout << "Name: " << e.name << ", Age: " << e.age << ",
Salary: $" << e.salary << endl;
}
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
10.5 Using Structures with Functions
Passing Structure by Reference (Using Pointers) int main() {
double salary;
};
enum aylar {
OCAK = 1, SUBAT, MART, NISAN, MAYIS, HAZIRAN, TEMMUZ, AGUSTOS, EYLUL,
EKIM, KASIM, ARALIK
};
int main() {
const char *monthName[] = { "", "Ocak", "Şubat", "Mart", "Nisan", "Mayıs",
"Haziran", "Temmuz", "Ağustos", "Eylül", "Ekim", "Kasım", "Aralık" };
return 0;
}
• C23 supports anonymous structs and unions that can be nested in named
structs and unions
• Members in a nested anonymous struct and union are considered to be
members of the enclosing type and can be accessed directly through an object
of the enclosing type
• Example
– struct MyStruct {
int member1;
int member2;
struct {
int nestedMember1;
int nestedMember2;
};
};
– For a variable myStruct of type struct MyStruct, you can access the members as:
• myStruct.member1
myStruct.member2
myStruct.nestedMember1
myStruct.nestedMember2
• struct Example {
• int id;
• int main() {
• struct Example ex;
• ex.id = 1;
• ex.floatValue = 3.14; // Doğrudan erişim!
• return 0;
• }
• // Değerleri yazdır
• printf("member1: %d\n", myStruct.member1);
• printf("member2: %d\n", myStruct.member2);
• printf("nestedMember1: %d\n", myStruct.nestedMember1);
• printf("nestedMember2: %d\n", myStruct.nestedMember2);
• return 0;
• }
Imagine a sensor that can return either an int (raw ADC value) or a float (calibrated
voltage). Using an anonymous union, we can access either value using the same
memory location.
Hardware Register Access Allows both bit-level and full register access
Bit Manipulation
– INT02-C: As a result of the integer promotion rules
(discussed in Section 5.6), performing bitwise
operations on integer types smaller than int can
lead to unexpected results. Explicit casts are
required to ensure correct results.
– INT13-C: Some bitwise operations on signed integer
types are implementation defined—this means that
the operations may have different results across C
compilers. For this reason, unsigned integer types
should be used with the bitwise operators.
– #include <iostream>
– using namespace std;
– enum Color {
– RED = 1,
– BLUE = 2,
– GREEN = 2, // Hata riski! BLUE ve GREEN aynı değeri paylaşıyor.
– YELLOW = 3
– };
– int main() {
– Color myColor = GREEN;
– if (myColor == BLUE) {
– cout << "Color is BLUE" << endl; // Buraya yanlışlıkla girilebilir.
– }
– return 0;
– }