பாடம் 3 / 6 · Strings, Structs, Files · படிக்க 2 min · புதுப்பிக்கப்பட்டது
Structs: சம்பந்தப்பட்ட data-வை சேர்க்கிறது
ஆறு தனித்தனி variables-ஐ தனித்தனியா handle பண்ண வேண்டாம் — அந்த data குழுவுக்கு ஒரே பேரு கொடுத்து ஒண்ணா வைக்கலாம்.
ஒரு struct-ன்னா, ஒண்ணா சேர்ந்து இருக்க வேண்டிய வெவ்வேறு variables-ஐ ஒரே குழுவா சேர்த்து வைக்கிறது. உதாரணத்துக்கு, ஒரு student-க்கு பேரு, roll number, marks-ன்னு மூணு விஷயங்கள் — அவை மூணு தனித்தனி விஷயங்கள் இல்ல; அந்த student-ஓட data-வா ஒண்ணா பார்க்கலாம்.
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int roll;
float marks;
};
int main(void)
{
struct Student s;
strcpy(s.name, "Priya");
s.roll = 42;
s.marks = 87.5f;
printf("%s (%d) scored %.1f\n", s.name, s.roll, s.marks);
return 0;
}
Priya (42) scored 87.5
Structs-ஓட array
struct Student class[3] = {
{"Priya", 1, 87.5f},
{"Arun", 2, 73.0f},
{"Meena", 3, 91.5f}
};
for (int i = 0; i < 3; i++) {
printf("%-8s %.1f\n", class[i].name, class[i].marks);
}
Structs-ம் pointers-ம்: அந்த arrow
void give_bonus(struct Student *s)
{
s->marks += 5.0f; /* arrow: through a pointer */
/* (*s).marks += 5.0f; exactly the same thing */
}
Note
Struct-ஐ நேரடியா வெச்சிருந்தா . use பண்ணுங்க; struct-ஓட pointer வெச்சிருந்தா -> use பண்ணுங்க. (*s).marks-ன்னு type பண்ணுறது கஷ்டம், தப்பா எழுதுறதும் easy — அதனாலதான் -> shortcut.
Note
பெரிய struct-ஐ by value-ஆ function-க்கு கொடுத்தா, அந்த struct முழுசாவே copy ஆகும். Struct பெருசா இருந்தா pointer-ஐ கொடுங்க; function அதை மாற்றவே கூடாதுன்னா const-யும் சேர்த்துக்கோங்க.
உங்ககிட்ட struct Student *p; இருக்கு. Name-ஐ எப்படி படிப்பீங்க?
Create a free account to save your progress, or sign in.