반응형
#include <stdio.h>
int main(void)
{
int ary[3] = {10, 20, 30};
printf("ary[0] : %d\n", ary[0]);
printf("*(ary + 0) : %d\n", *ary + 0);
puts("");
printf("&ary[0] : %p\n", &ary[0]);
printf("&*(ary + 0) : %p\n", &*(ary + 0));
printf("ary + 0 : %p\n", ary);
/*
ary[0] : 10
*(ary + 0) : 10
&ary[0] : 0062FE94
&*(ary + 0) : 0062FE94
ary + 0 : 0062FE94
*/
return 0;
}
#include <stdio.h>
int main(void)
{
int ary[3];
int *pa = ary;
int i;
*pa = 10;
*(pa + 1) = 20;
pa[2] = pa[0] + pa[1];
for (i = 0; i < 3; i++)
{
printf("%5d", pa[i]);
}
return 0;
}
#include <stdio.h>
int main(void)
{
int ary[3] = {10, 20, 30};
int *pa = ary;
int i;
printf("array value : ");
for (i = 0; i < 3; i++)
{
printf("%d ", *pa);
pa++;
}
return 0;
}
#include <stdio.h>
int main(void)
{
int ary[5] = {10, 20, 30, 40, 50};
int *pa = ary; // 10
int *pb = pa + 3; // 40
printf("pa : %p\n", pa); // addr of pa = &ary[0]
printf("pb : %p\n", pb); // addr of pb = &ary[4]
pa++;
printf("pb - pa : %p\n", pb - pa);
printf("Output frond array elmt : ");
if (pa < pb) printf("%d\n", *pa);
else printf("%d\n", *pb);
return 0;
}
#include <stdio.h>
void print_ary(int *pa);
int main(void)
{
int ary[5] = { 10, 20, 30, 40, 50 };
print_ary(ary);
return 0;
}
/*
void print_ary(int *pa)
{
int i;
for (i = 0; i < 5; i++)
{
printf("%d ", *pa);
pa++;
}
*/
void print_ary(int* pa)
{
int i;
for (i = 0; i < 5; i++)
{
printf("%d ", pa[i]);
}
}
#include <stdio.h>
void print_ary(int* pa, int size);
int main(void)
{
int ary1[5] = { 10, 20, 30, 40, 50 };
int ary2[7] = { 10, 20, 30, 40, 50, 60, 70 };
print_ary(ary1, 5);
puts("");
print_ary(ary2, 7);
return 0;
}
void print_ary(int* pa, int size)
{
int i;
for (i = 0; i < size; i++)
{
//printf("%d ", pa[i]);
printf("%d ", *pa);
pa++;
}
}
#include <stdio.h>
void input_ary(double* pa, int size);
double find_max(double* pa, int size);
int main(void)
{
double ary[5];
double max;
int i;
int size = sizeof(ary) / sizeof(ary[0]); // sizeof(*ary);
input_ary(ary, size);
/*for (i = 0; i < size; i++)
{
printf("%.1lf ", ary[i]);
}*/
max = find_max(ary, size);
printf("Max value of array : %.1lf\n", max);
return 0;
}
void input_ary(double* pa, int size)
{
int i;
printf("Input %d of float value : ", size);
for (i = 0; i < size; i++)
{
scanf_s("%lf", pa + i);
}
}
double find_max(double* pa, int size)
{
double max;
int i;
max = pa[0];
for (i = 1; i < size; i++)
{
if (pa[i] > max)
{
max = pa[i];
}
}
return max;
}
#include <stdio.h>
void input_nums(int* pnum, int size);
void print_nums(int *pnum, int size);
int main(void)
{
int lotto_nums[6];
int size = sizeof(lotto_nums) / sizeof(lotto_nums[0]);
input_nums(lotto_nums, size);
print_nums(lotto_nums, size);
return 0;
}
void input_nums(int* pnum, int size)
{
int i;
printf("Input %d num : ", size);
for (i = 0; i < size; i++)
{
scanf_s("%d", &pnum[i]);
}
}
void print_nums(int *pnum, int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%d ", pnum[i]);
}
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void SelectionSort(int arr[], int n)
{
int i, k, p, temp;
for (i = 0; i < n - 1; i++) {
p = i;
for (k = i + 1; k < n; k++) {
if (arr[k] < arr[p]) p = k; // Ascending sort(오름차순 정렬)
}
temp = arr[i];
arr[i] = arr[p];
arr[p] = temp;
}
}
int main()
{
int lotto[6] = { 0 }; // 나머지 5개에는 0으로 채움 --> 6개 모두 0으로 초기화 된다.
int i = 0, n = 0;
srand((unsigned)time(NULL)); // 매번 다른 수를 생성하도록 랜덤함수 초기화
while (n < 6)
{
// rand() % 45 : 0부터 44까지 의 난수를 생성한다.
// rand() % 45 + 1 : 1부터 45까지의 난수를 생성한다.
int r = rand() % 45 + 1; // 1~45 사이의 랜덤 수 생성, 중복 가능
for (i = 0; i < n; i++) // 이미 생성된 개수 만큼 반복
if (lotto[i] == r) break; // 이미 생성된 번호인지(중복수) 검사한다.
if (n == i) lotto[n++] = r; // 중복수가 아닐때에만 n위치에 생성된 수를 기억시킨다.
}
// 크기순으로 정렬한다.
SelectionSort(lotto, 6);
// 생성된 번호 출력한다.
for (i = 0; i < 6; i++)
printf("%d\n", lotto[i]);
return 0;
}
#include <stdio.h>
int main(void)
{
int score[3][4];
int total;
double avg;
int i, j;
for (i = 0; i < 3; i++)
{
printf("Input subject score 4 : ");
for (j = 0; j < 4; j++)
{
scanf_s("%d", &score[i][j]);
}
}
for (i = 0; i < 3; i++)
{
total = 0;
for (j = 0; j < 4; j++)
{
total += score[i][j];
}
avg = total / 4.0;
printf("Total score : %d, Average : %.2lf\n", total, avg);
}
return 0;
}
/*
Total score : 328, Average : 82.00
Total score : 364, Average : 91.00
Total score : 280, Average : 70.00
*/
#include <stdio.h>
int main(void)
{
int num[3][4] =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int i, j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
printf("%5d", num[i][j]);
}
puts("");
}
return 0;
}
#include <stdio.h>
int main(void)
{
char animal[5][10];
int i;
int count;
count = sizeof(animal) / sizeof(animal[0]);
for (i = 0; i < count; i++)
{
scanf_s("%s", animal[i]);
}
for (i = 0; i < count; i++)
{
printf("%s ", animal[i]);
}
return 0;
}
#include <stdio.h>
int main(void)
{
char animal1[5][10] =
{
{'d', 'o', 'g','\0'},
{'t', 'i', 'g', 'e', 'r', '\0'},
{'r', 'a', 'b', 'b', 'i', 't', '\0'},
{'h', 'o', 'r', 's', 'e', '\0'},
{'c', 'a', 't', '\0'}
};
char animal2[][10] = {"dog", "tiger", "rabbit", "horse", "cat"};
int i;
for (i = 0; i < 5; i++)
{
printf("%s ", animal1[i]);
}
puts("");
for (i = 0; i < 5; i++)
{
printf("%s ", animal2[i]);
}
return 0;
}
#include <stdio.h>
int main(void)
{
int score[2][3][4] =
{
{{72, 80, 95, 60}, {68, 98, 83, 90}, {75, 72, 84, 90}},
{{66, 85, 90, 88}, {95, 92, 88, 95}, {43, 72, 56, 75}}
};
int i, j, k;
for (i = 0; i < 2; i++)
{
printf("%d class score...\n", i + 1);
for (j = 0; j < 3; j++)
{
for (k = 0; k < 4; k++)
{
printf("%5d", score[i][j][k]);
}
puts("");
}
puts("");
}
return 0;
}
/*
1 class score...
72 80 95 60
68 98 83 90
75 72 84 90
2 class score...
66 85 90 88
95 92 88 95
43 72 56 75
*/
#include <stdio.h>
int main(void)
{
char *pary[5];
int i;
pary[0] = "dog";
pary[1] = "elephant";
pary[2] = "horse";
pary[3] = "tiger";
pary[4] = "lion";
for (i = 0; i < 5; i++)
{
printf("%s\n", pary[i]);
}
return 0;
}
#include <stdio.h>
int main(void)
{
int ary1[4] = {1, 2, 3, 4};
int ary2[4] = {11, 12, 13, 14};
int ary3[4] = {21, 22, 23, 24};
int ary4[4] = {1, 2, 3, 4};
int ary5[4] = {11, 12, 13, 14};
int ary6[4] = {21, 22, 23, 24};
int ary7[4] = {1, 2, 3, 4};
int ary8[4] = {11, 12, 13, 14};
int ary9[4] = {21, 22, 23, 24};
int ary10[4] = {1, 2, 3, 4};
int ary11[4] = {11, 12, 13, 14};
int ary12[4] = {21, 22, 23, 24};
int *pary[4][3] =
{
{ary1, ary2, ary3},
{ary4, ary5, ary6},
{ary7, ary8, ary9},
{ary10, ary11, ary12},
};
int i, j, k;
for (i = 0; i < 4; i++)
{
puts("");
for (j = 0; j < 3; j++)
{
for (k = 0; k < 4; k++)
{
printf("%5d", pary[i][j][k]);
}
puts("");
}
puts("");
}
return 0;
}
#include <stdio.h>
int main(void)
{
int a = 10;
int* pi;
int** ppi;
pi = &a;
ppi = π
printf("--------------------------------------------------\n");
printf("var var-val &operator *operator **operator \n");
printf("--------------------------------------------------\n");
printf(" a%12d%15p\n", a, &a);
printf("pi%12d%15p%11p%11p\n", pi, &pi, *pi);
printf("ppi%11d%14p%11p%11p\n", ppi, &ppi, *ppi, **ppi);
return 0;
}
#include <stdio.h>
void swap(char** ppa, char** ppb);
int main(void)
{
char *pa = "success";
char *pb = "failure";
printf("pa -> %s, pb -> %s\n", pa, pb);
swap(&pa, &pb);
printf("pa -> %s, pb -> %s\n", pa, pb);
return 0;
}
void swap(char** ppa, char** ppb)
{
char* pt;
pt = *ppa;
*ppa = *ppb;
*ppb = pt;
}
#include <stdio.h>
void print_str(char **pps, int cnt);
int main(void)
{
char *ptr_ary[] = {"eagle", "tiger", "lion", "sqirrel"};
int count;
count = sizeof(ptr_ary) / sizeof(ptr_ary[0]);
print_str(ptr_ary, count);
return 0;
}
void print_str(char **pps, int cnt)
{
int i;
for(i = 0; i < cnt; i++)
{
printf("%s\n", pps[i]);
}
}
#include <stdio.h>
int main(void)
{
int ary[3][4] = {{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12}};
int (*pa)[4];
int i, j;
pa = ary;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
printf("%5d", pa[i][j]);
}
puts("");
}
return 0;
}
반응형
'Language > C & C++' 카테고리의 다른 글
dynamic memory allocation (0) | 2022.08.30 |
---|---|
the difference between Parameter and Argument (0) | 2022.08.30 |
HGC - Chapter 9 exam (0) | 2022.08.29 |
C_10. Basic Grammar of Structure (0) | 2022.08.15 |
C_09. Basic Grammar of Function (0) | 2022.08.12 |