728x90
반응형
#include <stdio.h>
/*To declare structures and use members*/
struct student
{
int num;
double grade;
};
int main(void)
{
struct student s1;
s1.num = 2;
s1.grade = 2.7;
// USe Object member access oprator (.)
printf("Class Num : %d\n", s1.num);
printf("Grade : %.1lf\n", s1.grade);
/*
Class Num : 2
Grade : 2.7
*/
puts("");
/*Size of structural variables*/
int st_Size = sizeof(s1);
int nm_Size = sizeof(s1.num);
int gr_Size = sizeof(s1.grade);
printf("Structure Size : %d byte\n", st_Size);
printf("Num_var Size : %d byte\n", nm_Size);
printf("Grade_var Size : %d byte\n", gr_Size);
/*
Structure Size : 16 byte
Num_var Size : 4 byte
Grade_var Size : 8 byte
+Padding byte : 4 byte = "Byte alignment"
*/
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*A structure with an array and a pointer as a member*/
struct profile // Profile structure declaration
{
char name[20];
int age;
double height;
char* intro;
};
int main(void)
{
struct profile yuju;
strcpy(yuju.name, "Yuju Cheon");
yuju.age = 5;
yuju.height = 85.6;
yuju.intro = (char*)malloc(80); // Dynamic memory allocation
printf("Intro : ");
gets(yuju.intro);
printf("Name : %s\n", yuju.name);
printf("Age : %d\n", yuju.age);
printf("Height : %.2lf\n", yuju.height);
printf("Intro : %s\n", yuju.intro);
free(yuju.intro); // malloc return
return 0;
}
/*
print -> Intro : Nice to meet you!
Name : Yuju Cheon
Age : 5
Height : 85.60
Intro : Nice to meet you!
*/
#include <stdio.h>
/*Initialization of Structural Variables and Substitution Operation*/
//Student data print with top grades
struct student
{
int id;
char name[20];
double grade;
};
int main(void)
{
struct student
s1 = { 315, "Hong", 2.4 },
s2 = { 316, "Lee", 3.7 },
s3 = { 317, "Park", 3.6 };
struct student max;
/*
max.id = s1.id;
strcpy(max.name, s1.name);
max.grade = s1.grade;
*/
max = s1;
if (s2.grade > max.grade) max = s2;
if (s3.grade > max.grade) max = s3;
printf("Class Num : %d\n", max.id);
printf("Name : %s\n", max.name);
printf("Grade : %.1lf\n", max.grade);
return 0;
}
/*
Class Num : 316
Name : Lee
Grade : 3.7
*/
#include <stdio.h>
/*Using Structural Variables for Function Parameters*/
// Return the structure to exchange the values of two variables
struct vision
{
double left;
double right;
};
struct vision exchange(struct vision robot);
int main(void)
{
struct vision robot;
printf("Input eyesight : \n");
scanf_s("%lf%lf", &(robot.left), &(robot.right));
robot = exchange(robot);
printf("Changed eyesight : %.1lf %.1lf\n", robot.left, robot.right);
return 0;
}
struct vision exchange(struct vision robot)
{
double temp;
temp = robot.left;
robot.left = robot.right;
robot.right = temp;
return robot;
}
/*
Input eyesight :
2.0
1.2
Changed eyesight : 1.2 2.0
*/
#include <stdio.h>
/*Structure Pointer & Arrow Operator*/
// Using Structure pointer
struct score
{
int kor;
int eng;
int math;
};
int main(void)
{
struct score yuju = { 90, 80, 70 }; //decralation structure and initializaion
struct score* ps = &yuju; //Save the addr to Strct pointer
printf("Korean : %d\n", (*ps).kor); //Access member as Strct Pointer
printf("English: %d\n", ps->eng); // Using arrows operator
printf("Math : %d\n", ps->math);
return 0;
}
/*
Korean : 90
English: 80
Math : 70
*/
#include <stdio.h>
/* Structure array */
// Initialize and print structure array
struct address
{
char name[20];
int age;
char tel[20];
char addr[80];
};
int main(void)
{
struct address list[5] =
{
{"Hong", 23, "111-1111", "Seoul"},
{"Lee", 35, "222-2222", "Incheon"},
{"Jang", 19, "333-3333", "Daegu"},
{"Yoo", 15, "444-4444", "Busan"},
{"Ahn", 45, "555-5555", "Jeju"}
};
int i;
for (i = 0; i < 5; i++)
{
printf("%10s%5d%15s%20s\n", list[i].name, list[i].age, list[i].tel, list[i].addr);
}
return 0;
}
/*
Hong 23 111-1111 Seoul
Lee 35 222-2222 Incheon
Jang 19 333-3333 Daegu
Yoo 15 444-4444 Busan
Ahn 45 555-5555 Jeju
*/
#include <stdio.h>
/* Functions that handle an array of structures */
// Print the value of a structure array using the arrow operator in the function
struct address
{
char name[20];
int age;
char tel[20];
char addr[80];
};
void print_list(struct address* lp);
int main(void)
{
struct address list[5] =
{
{"Hong", 23, "111-1111", "Seoul"},
{"Lee", 35, "222-2222", "Incheon"},
{"Jang", 19, "333-3333", "Daegu"},
{"Yoo", 15, "444-4444", "Busan"},
{"Ahn", 45, "555-5555", "Jeju"}
};
print_list(list);
return 0;
}
void print_list(struct address* lp)
{
int i;
for (i = 0; i < 5; i++)
{
printf("%10s%5d%15s%20s\n",
(lp + i)->name, (lp + i)->age, (lp + i)->tel, (lp + i)->addr);
}
}
/*
Hong 23 111-1111 Seoul
Lee 35 222-2222 Incheon
Jang 19 333-3333 Daegu
Yoo 15 444-4444 Busan
Ahn 45 555-5555 Jeju
*/
#include <stdio.h>
/* Self-referential Structures */
// Creating a list with a Self-referential structures
struct list // Self-ref Structure
{
int num; // Save the data Member
struct list* next; // A pointer member pointing to the structure itself
};
int main(void)
{
struct list // initialize Strct var
a = { 10, 0 },
b = { 20, 0 },
c = { 30, 0 };
struct list* head = &a, * current; // head pointer initialize (***Linked list)
a.next = &b; // 'a' pointer member pointing to the 'b'
b.next = &c; // 'b' pointer member pointing to the 'c'
printf("head -> num : %d\n", head->num); // Using Number of 'a' pointing a head
printf("heade -> next -> num : %d\n", head->next->num); // Use num member of b as head
printf("List all : \n");
current = head; // The first current pointer points to 'a'
while (current != NULL) // Repeated ends when output to the last structural variable
{
printf("%d ", current->num); // The num output of the structural variable pointed by the current
current = current->next; // Allow current to point to the following structural variables
}
puts("");
return 0;
}
/*
head -> num : 10
heade -> next -> num : 20
List all :
10 20 30
*/
#include <stdio.h>
/* Union */
// Processing of class numbers and grade data using UNION.
union student
{
int num;
double grade;
};
int main(void)
{
union student s1 = { 315 };
printf("Class Num : %d\n", s1.num);
s1.grade = 4.4;
printf("Grade : %.1lf\n", s1.grade);
printf("Class Num : %d\n", s1.num);
return 0;
}
/*
Class Num : 315
Grade : 4.4
Class Num : -1717986918
>> Initial value of class number changed by grade member.
# Rule 1.
The size of the Union variable is determined
by the member with the largest size.
union studetn s1 = { 315 };
Total size = 8 byte
grade(double) size = 8 byte
num(int) size = 4 byte
# Rule 2.
The initialization of the Union variable uses
brackets({ }) to initialize only the first member.
If you initialize a member that is not the first member,
you must save the member directly as a member access operator.
union student a = { .grade = 3.4 };
union student s1 = { 315 };
s1.grade = 4.4;
*/
#include <stdio.h>
/* Enumeration */
// Programs with Enumeration
enum season {SPRING, SUMMER, FALL, WINTER};
int main(void)
{
enum season ss;
char* pc = NULL; // Save the string Pointer
ss = SPRING;
switch (ss)
{
case SPRING:
pc = "inline"; break;
case SUMMER:
pc = "swimming"; break;
case FALL:
pc = "trip"; break;
case WINTER:
pc = "skiing"; break;
}
printf("My leisure activities : %s\n", pc);
return 0;
}
/*
My leisure activities : inline
*/
#include <stdio.h>
/* typedef */
// Override datatypes using typedef
struct student
{
int num;
double grade;
};
typedef struct student Student;
void print_data(Student* ps);
int main(void)
{
Student s1 = { 315, 4.2 };
print_data(&s1);
return 0;
}
void print_data(Student* ps)
{
printf("Class Num : %d\n", ps->num);
printf("Grade : %.1lf\n", ps->grade);
}
/*
Class Num : 315
Grade : 4.2
*/
728x90
반응형
'Language > C & C++' 카테고리의 다른 글
Pointer Examples (0) | 2022.09.10 |
---|---|
Transfer an array to a function's arguments (0) | 2022.09.09 |
Number guessing game in Python 3 and C (0) | 2022.08.31 |
malloc exam (0) | 2022.08.30 |
dynamic memory allocation (0) | 2022.08.30 |