๐Ÿค– 3. ๋ฐฐ์—ด, ๊ตฌ์กฐ์ฒด, ํฌ์ธํ„ฐ

Yeonsu Summerยท2022๋…„ 7์›” 7์ผ
0

์ž๋ฃŒ๊ตฌ์กฐ

๋ชฉ๋ก ๋ณด๊ธฐ
3/4
post-thumbnail
post-custom-banner

๋ฐฐ์—ด

๋ฐฐ์—ด(array) : ๋™์ผํ•œ ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ๋จ
=> random access(direct access) : ์ˆœ์ฐจX
=> ์—ฐ์†์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„ ํ•„์š” : ์ธ๋ฑ์Šค ์‚ฌ์šฉ

๋ฐฐ์—ด ADT(์ถ”์ƒ ์ž๋ฃŒํ˜•) : <์ธ๋ฑ์Šค(index), ๊ฐ’(value)>์˜ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ง‘ํ•ฉ

  • create(size) : size๊ฐœ์˜ ์š”์†Œ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์—ด ์ƒ์„ฑ
  • get(A, i) : ๋ฐฐ์—ด A์˜ i๋ฒˆ์งธ ์š”์†Œ ๋ฐ˜ํ™˜
  • set(A, i, v) : ๋ฐฐ์—ด A์˜ i๋ฒˆ์งธ ์œ„์น˜์— ๊ฐ’ v ์ €์žฅ
    (c์–ธ์–ด์—์„œ๋Š” ๊ตฌํ˜„ํ•  ํ•„์š” X)


โœ… 1์ฐจ์› ๋ฐฐ์—ด

  • create ์—ฐ์‚ฐ : int list[6];
  • set ์—ฐ์‚ฐ : list[0] = 100;
  • get ์—ฐ์‚ฐ : value = list[0];

์ปดํŒŒ์ผ๋Ÿฌ ์ฃผ์†Œ : base+i*sizeof(int) (์‹œ์ž‘์ฃผ์†Œ: base, type: int)

โœ… 2์ฐจ์› ๋ฐฐ์—ด
ex) int list[3][5];

0์—ด(column)1์—ด2์—ด3์—ด4์—ด
0ํ–‰list[0][0]list[0][1]list[0][2]list[0][3]list[0][4]list[0][5]
1ํ–‰list[1][0]list[1][1]list[1][2]list[1][3]list[1][4]
2ํ–‰list[2][0]list[2][1]list[2][2]list[2][3]list[2][4]

๊ณ„์‚ฐ 1) int test[3][5]๋กœ ์„ ์–ธ๋œ ๋ฐฐ์—ด์—์„œ test[2][3]์˜ ์›์†Œ์˜ ์ฃผ์†Œ๊ฐ’์€?
2*5 + 3 = 13

๊ณ„์‚ฐ 2) int test[3][4][5]๋กœ ์„ ์–ธ๋œ ๋ฐฐ์—ด์—์„œ test[2][3][3]์˜ ์›์†Œ์˜ ์ฃผ์†Œ๊ฐ’์€?
2*4*5 + 3*5 + 3 = 58

๊ตฌ์กฐ์ฒด

๊ตฌ์กฐ์ฒด(structure) : ํƒ€์ž…์ด ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌถ๋Š” ๋ฐฉ๋ฒ•

๐Ÿ“Œ 'struct' ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ

๊ตฌ์กฐ์ฒด ํ˜•์‹๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜
struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„ {
ํ•ญ๋ชฉ1;
ํ•ญ๋ชฉ2;
...
};
struct ๊ตฌ์กฐ์ฒด์ด๋ฆ„ ๊ตฌ์กฐ์ฒด๋ณ€์ˆ˜;
struct studentTag {
char name[10];
int age;
double gpa;
};
struct studentTag s;

๐Ÿ“Œ ๊ตฌ์กฐ์ฒด ์•ˆ์˜ ๋ฉค๋ฒ„ ์‚ฌ์šฉ : ๋ณ€์ˆ˜ ๋’ค์— ๋ฉค๋ฒ„์—ฐ์‚ฐ์ž '.' ์‚ฌ์šฉ

strcpy(s.name, "kim");
s.age = 20;
s.gpa = 4.3;

๐Ÿ“Œ c์–ธ์–ด์—์„œ๋Š” typedef์„ ์‚ฌ์šฉํ•ด ๊ตฌ์กฐ์ฒด๋ฅผ ์ƒˆ๋กœ์šด ํƒ€์ž…์œผ๋กœ ์„ ์–ธ ๊ฐ€๋Šฅ

struct studentTag {
    char name[10];
    int age;
    double gpa;
} student;

๐Ÿ“Œ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐ ์ดˆ๊ธฐํ™” ๊ฐ€๋Šฅ

student s = { "kim", 20, 4.3 };

๋ฐฐ์—ด์˜ ์‘์šฉ: ๋‹คํ•ญ์‹

p(x) = aโ‚™xโฟ + aโ‚™โ‚‹โ‚xโฟโปยน + ... + aโ‚x + aโ‚€
=> a: ๊ณ„์ˆ˜, x: ๋ณ€์ˆ˜, n: ์ฐจ์ˆ˜ (๊ฐ€์žฅ ํฐ ์ฐจ์ˆ˜=๋‹คํ•ญ์‹์˜ ์ฐจ์ˆ˜)

โ‘  ๋ชจ๋“  ์ฐจ์ˆ˜์˜ ๊ณ„์ˆ˜๊ฐ’์„ ๋ฐฐ์—ด์— ์ €์žฅ : dense(๋ถ„ํฌ๋„ ์ข์€ ํ˜•ํƒœ)์—์„œ ์œ ๋ฆฌ
=>๊ฐ„๋‹จํ•˜๊ณ  ์‰ฝ๊ฒŒ ์ดํ•ด ๊ฐ€๋Šฅ
=> ๋Œ€๋ถ€๋ถ„์˜ ํ•ญ์˜ ๊ณ„์ˆ˜๊ฐ€ 0์ธ ํฌ์†Œ ๋‹คํ•ญ์‹์—์„  ๊ณต๊ฐ„์˜ ๋‚ญ๋น„๊ฐ€ ์‹ฌํ•จ

ex) 10xโต+6x+3 = 10โˆ™xโต+0โˆ™xโด+0โˆ™xยณ+0โˆ™xยฒ+6โˆ™x+3

#include <stdio.h>
#define MAX(a,b) (((a)>(b))?(a):(b))    // a>b->a, a<=b->b
#define MAX_DEGREE 101    // ๋‹คํ•ญ์‹ ์ตœ๋Œ€ ์ฐจ์ˆ˜:100 + ์ƒ์ˆ˜:1

typedef struct {
	int degree;    // ๋‹คํ•ญ์‹์˜ ์ฐจ์ˆ˜๋ฅผ degree์— ์ €์žฅ
    float coef[MAX_DEGREE];    // ๊ณ„์ˆ˜๋ฅผ coef์— ์ €์žฅ
} polynomial;

polynomial poly_add(polynomial A, polynomial B) {
	polynomial C;
    int Apos = 0; Bpos = 0; Cpos = 0;
    int degree_a = A.degree;
    int degree_b = B.degree;
    C.degree = MAX(A.degree, B.degree);
    
    while (Apos <= A.degree && Bpos <= B.degree) {
    	if (degree_a > degree_b) {
        	C.coef[Cpos++] = A.coef[Apos++];
            degree_a--;
        }
        else if (degree_a == degree_b) {
        	C.coef[Cpos++] = A.coef[Apos++] + B.coef[Bpos++];
            degree_a--; degree_b--;
        }
        else {
        	C.coef[Cpos++] = B.coef[Bpos]++;
            degree_b--;
        }
    }
    return C;
}

void print_poly(polynomial p) {
	for (int i=p.degree; i>0; i--) {
        print("3.1fx^%d + ", p.coef[p.degree - i], i);
    printf("%3.1f \n", p.coef[p.degree]);
}

int main(void) {
    polynomial a = {5, {3, 6, 0, 0, 0, 10}};    // degreen, coef
    polynomial b = {4, {7, 0, 5, 0, 1}};
    polynomial c;
    
    print_poly(a);
    print_poly(b);
    c = poly_add(a, b);
    print_poly(c);
    return 0;
}

Q. C.coef[Cpos++] = A.coef[Apos++]์™€ C.coef[++Cpos] = A.coef[++Apos]์˜ ์ฐจ์ด์ ?

A. for๋ฌธ์—์„œ๋Š” ์ฐจ์ด ์—†์Œ. ์ „์ž๋Š” ๊ณ„์‚ฐ ํ›„ ํฌ์ง€์…˜ ๊ฐ’ ์ฆ๊ฐ€, ํ›„์ž๋Š” ํฌ์ง€์…˜ ๊ฐ’ ์ฆ๊ฐ€ ํ›„ ๊ณ„์‚ฐ

โ‘ก ๋‹คํ•ญ์‹์—์„œ 0์ด ์•„๋‹Œ ํ•ญ๋งŒ์„ ํ•˜๋‚˜์˜ ์ „์—ญ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ• : sparse(๋ถ„ํฌ๋„ ๋„“์€ ํ˜•ํƒœ)์— ์œ ๋ฆฌ
=> ๊ณต๊ฐ„ ๋‚ญ๋น„ ์ ์Œ
=> ์ธ๋ฑ์Šค ๋ณ€์ˆ˜๋ฅผ ๊ด€๋ฆฌํ•ด์•ผํ•จ, ๋‹คํ•ญ์‹์— ๋”ฐ๋ผ ๊ณต๊ฐ„์„ ๋” ์ฐจ์ง€ํ•  ์ˆ˜ ์žˆ์Œ, ๋ฐฉ๋ฒ•์ด ์ข€ ๋” ์–ด๋ ค์›€

ex) A = 8xยณ+7x+1 = ( (8,3), (7,1), (1,0) ) , B = 10xโต+6x+3 = ( (10,5), (6,1), (3,0) )

coef: ๊ณ„์ˆ˜, expon: ์ฐจ์ˆ˜, avail: ํ˜„์žฌ ๋น„์–ด์žˆ๋Š” ์š”์†Œ์˜ ์ธ๋ฑ์Šค

#include <stdio.h>
#include <stdlib.h>
#define MAX_TERMS 101

typedef struct {
	float coef;
    int expon;
} polynomial;

polynomial terms[MAX_TERMS] = { {8,3}, {7,1}, {1,0}, {10,3}, {3,2}, {1,0} };
int avail = 6;    // c์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค

char compare(int a, int b) {
	if (a>b) return '>';
    else if (a == b) return '=';
    else return '<';
}

void attach(float coef, int expon) {
	if (avail > MAX_TERMS) {
    	fprintf(stderr, "ํ•ญ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์Œ\n");
        exit(1);
    }
    terms[avail].coef = coef;
    terms[avail].expon = expon;
    avail++;
}

void poly_add(int As, int Ae, int Bs, int Be, int *Cs, int *Ce) {
	float tempcoef;
    *Cs = avail;
    
    while (As <= Ae && Bs <= Be) {
    	switch (compare(terms[As].expon, terms[Bs].expon)) {
        	case '>':
            	attach(terms[As].coef, terms[As].expon);
                As++; break;
            case '=':
            	tempcoef = terms[As].coef + terms[Bs].coef;
                if (tempcoef)    // 0 ์ผ ๋•Œ ์•„๋ฌด๊ฒƒ๋„X, 0 ์•„๋‹ ๋•Œ attach
                	attach(tempcoef, terms[As].expon);
                As++; Bs++; break;
            case '&lt;':
            	attach(terms[Bs].coef, terms[Bs].expon);
                Bs++; break;
            }
            
        for (; As<=Ae; As++)    // A์˜ ๋‚˜๋จธ์ง€ ํ•ญ๋“ค ์ด๋™
        	attach(terms[As].coef, terms[As].expon);
        for (; Bs<=Be; Bs++)    // B์˜ ๋‚˜๋จธ์ง€ ํ•ญ๋“ค ์ด๋™
        	attach(terms[Bs].coef, terms[Bs].expon);
        *Cs = avail - 1;
    }
    
    void print_poly(int s, int e) {
    	for (int i=s; i&lt;e; i++)
        	printf("%3.1fx^&d + ", terms[i].coef, terms[i].expon);
        printf("%3.1fx^&d\n", terms[e].coef, terms[e].expon);
    }
    
    int main(void) {
    	int As=0, Ae=2, Bs=3, Be=5, Cs, Ce;
        poly_add(As, Ae, Bs, Be, &Cs, &Ce);
        print_poly(As, Ae);
        print_poly(Bs, Be);
        print_poly(Cs, Ce);
        return 0;
    }

๋ฐฐ์—ด์˜ ์‘์šฉ: ํฌ์†Œํ–‰๋ ฌ

ํฌ์†Œํ–‰๋ ฌ ํ‘œํ˜„๋ฐฉ๋ฒ•

โ‘  ํ–‰๋ ฌ์„ 2์ฐจ์› ๋ฐฐ์—ด๋กœ ํ‘œํ˜„ : dense
=> ๋งŽ์€ ํ•ญ์ด 0์œผ๋กœ ๋˜์–ด์žˆ๋Š” ๊ฒฝ์šฐ(ํฌ์†Œํ–‰๋ ฌ) -> ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„ ์‹ฌํ•จ

a[i][j] => a[j][i]

#include <stdio.h>
#define ROWS 3
#define COLS 3

void matrix_transpose(int A[ROWS][COLS], int B[ROWS][COLS]) {
	for (int r=0; r<ROWS; r++)
    	for (int c=0; c<COLS; c++)
        	B[c][r] = A[r][c];
}

void matrix_print(int A[ROWS][COLS]) {
	for (int r=0; r<ROWS; r++) {
    	for (int c=0; c<COLS; c++)
        	printf("%d ", A[r][c]);
        printf("\n");
    }
}

int main(void) {
	int array1[ROWS][COLS] = { {2,3,0}, {8,9,1}, {7,0,5} };
    int array2[ROWS][COLS];
    
    matrix_transpose(array1, array2);
    matrix_print(array1);
    matric_print(array2);
    return 0;
}

โ‘ก 0์ด ์•„๋‹Œ ํ•ญ๋งŒ ํ‘œํ˜„ : sparse
=> ํฌ์†Œํ–‰๋ ฌ : ๋ฐฐ์—ด ์‚ฌ์šฉํ•˜๋˜ 0์ด ์•„๋‹Œ ์š”์†Œ๋“ค๋งŒ ๋‚˜ํƒ€๋ƒ„ -> ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„ ์ ์Œ
=> ์ „์น˜์—ฐ์‚ฐ : ์ƒ๋‹นํ•œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•„์š”

(row, col, value) => (col, row, value)

#include <stdio.h>
#include <stdlib.h>
#define MAX_TERMS 100

typedef struct {
	int row;
    int col;
    int value;
} element;

typedef struct SparseMatrix {
	element data[MAX_TERMS];
    int rows;
    int cols;
    int terms;
} SparseMatrix;

SparseMaxtrix matrix_transpose(SparseMatrix a) {
	SparseMatrix b;
    
    int bindex;
    b.rows = a.rows;
    b.cols = a.cols;
    b.terms = a.terms;
    
    if (a.terms > 0) {
    	bindex = 0;
        for (int c=0; c<a.cols; c++) {
        	for (int i=0; i<a.terms; i++) {
            	if (a.data[i].col == c) {
                	b.data[bindex].row = a.data[i].col;
                    b.data[bindex].col = a.data[i].row;
                    b.data[bindex].value = a.data[i].value;
                    bindex++;
                }
            }
        }
    }
    return b;
}

void matrix_print(SparseMatrix a) {
	for (int i=0; i&lt;a.terms, i++) {
    	printf("(%d, %d, %d) \n", a.data[i].row, a.data[i].col, a.data[i].value);
    }
}

int main(void) {
	SparseMatrix m = {
    	{ {0,3,7}, {1,0,9}, {1,5,8}, {3,0,6}, {3,1,5}, {4,5,1}, {5,2,2} },
        6,
        6,
        7
    };
    SparseMatrix result;
    
    result = matrix_transpose(m);
    matrix_print(result);
    return 0;
}

ํฌ์ธํ„ฐ

  • ํฌ์ธํ„ฐ(pointer) : ๋‹ค๋ฅธ ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜
    => ์‹ค์งˆ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ(ํ•˜๋“œ์›จ์–ด) ์ ‘๊ทผ
int a = 100;
int *p;
p = &a;
// *p == a
  • ์—ฐ์‚ฐ์ž
    โ‘  & ์—ฐ์‚ฐ์ž : ๋ณ€์ˆ˜์˜ ์ฃผ์†Œ๋ฅผ ์ถ”์ถœํ•˜๋Š” ์—ฐ์‚ฐ์ž
    โ‘ก * ์—ฐ์‚ฐ์ž : ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์žฅ์†Œ์— ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ์—ฐ์‚ฐ์ž

  • ์ž๋ฃŒํ˜•์— ๋”ฐ๋ฅธ ํฌ์ธํ„ฐ

int *p;  // intํ˜• ๋ณ€์ˆ˜
float *pf;  // doubleํ˜• ๋ณ€์ˆ˜
char *pc;    // charํ˜• ๋ณ€์ˆ˜
  • ๋„ ํฌ์ธํ„ฐ : ์–ด๋–ค ๊ฐ์ฒด๋„ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š๋Š” ํฌ์ธํ„ฐ (ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ๋„ ํฌ์ธํ„ฐ์ธ์ง€ ๊ฒ€์‚ฌํ•ด์•ผํ•จ)
if (p == NULL) {
	fprintf(stderr, "์˜ค๋ฅ˜: ํฌ์ธํ„ฐ๊ฐ€ ์•„๋ฌด ๊ฒƒ๋„ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
    return;
}
  • ์ด์ค‘ํฌ์ธํ„ฐ
int **a;
a = 200;
*a = 100;
**a = 10;
  • ํฌ์ธํ„ฐ๋ฅผ ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ
#include <stdio.h>

void swap(int *px, int *py) {
	int tmp;
    tmp = *px;
    *px = *py;
    *py = tmp;
}

int main(void) {
	int a=1, b=2;
    print("swap์„ ํ˜ธ์ถœํ•˜๊ธฐ ์ „: a=%d, b=%d\n", a, b);
    swap(&a, &b);
    print("swap์„ ํ˜ธ์ถœํ•œ ๋‹ค์Œ: a=%d, b=%d\n", a, b);
    return 0;
}

Q. swap(&a, &b)๋กœ callํ•˜๊ณ  (*px, *py)๋กœ ๋ฐ›๋Š” ์ด์œ ?

A. swap์—์„œ a, b๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ main์œผ๋กœ ๋„˜์–ด๊ฐ€์ง€ ์•Š์Œ (px, py๋Š” ์ฃผ์†Œ๋ฅผ ๊ฐ–๊ณ  ์žˆ์œผ๋‹ˆ ๊ดœ์ฐฎ์Œ)


๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น

- ๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น(dynamic memory allocation) : ํ•„์š”ํ•œ ๋งŒํผ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์šด์˜์ฒด์ œ๋กœ๋ถ€ํ„ฐ ํ• ๋‹น ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๊ณ , ๋๋‚˜๋ฉด ์‹œ์Šคํ…œ์— ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ฐ˜๋‚ฉํ•˜๋Š” ๊ธฐ๋Šฅ => ํšจ์œจ์ 

- ํžˆํ”„(heap) : ๋™์  ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น๋˜๋Š” ๊ณต๊ฐ„, ์šด์˜์ฒด์ œ๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๋ชจ์•„ ๋†“์€ ๊ณณ

int *p;
p = (int *)malloc(sizeof(int));  // โ‘  ๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น
*p = 1000;  // โ‘ก ๋™์  ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ
free(p);  // โ‘ข ๋™์  ๋ฉ”๋ชจ๋ฆฌ ๋ฐ˜๋‚ฉ

โ‘  malloc( ) : ๋™์  ๋ฉ”๋ชจ๋ฆฌ ๋ธ”๋Ÿญ์˜ ์‹œ์ž‘ ์ฃผ์†Œ๋ฅผ ๋ฐ˜ํ™˜
ย ย ย ย void * : ๋ฐ˜ํ™˜๋˜๋Š” ์ฃผ์†Œ์˜ ํƒ€์ž…
ย ย ย ย ๋ฉ”๋ชจ๋ฆฌ ํ™•๋ณด๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด NULL์„ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜
โ‘ก ๋™์  ๋ฉ”๋ชจ๋ฆฌ๋Š” ํฌ์ธํ„ฐ๋กœ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
โ‘ข free( ) : ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ ๋ธ”๋ก์„ ์šด์˜์ฒด์ œ์—๊ฒŒ ๋ฐ˜ํ™˜
ย ย ย ย ์ฃผ์˜! malloc( ) ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ–ˆ๋˜ ํฌ์ธํ„ฐ ๊ฐ’์„ ์žŠ์–ด๋ฒ„๋ฆฌ๋ฉด ์•ˆ๋จ => ์žŠ์–ด๋ฒ„๋ฆฌ๋ฉด ๋™์  ๋ฉ”๋ชจ๋ฆฌ ๋ฐ˜ํ™˜ ๋ถˆ๊ฐ€๋Šฅ
ย ย ย ย malloc( )์€ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•˜๋ฉด NULL์„ ๋ฐ˜ํ™˜ (๋ฐ˜๋“œ์‹œ NULL์ธ์ง€ ๊ฒ€์‚ฌ)

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define SIZE 10

int main(void) {
	int *p;
    
    p = (int *)malloc(SIZE * sizeof(int));
    if (p == NULL) {
    	fprint(stderr, "๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•ด์„œ ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. \n");
        exit(1);
    }
    
    for (int i=0; i<SIZE; i++) 
    	p[i] = i;
        printf("%d ", p[i]);
        
    free(p);
    return 0;
}

(*ps).i == ps->i : ํฌ์ธํ„ฐ๋ฅผ ํ†ตํ•ด ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ„์— ์ ‘๊ทผ

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct studentTag {
	char name[10];
    int age;
    double gpa;
} student;

int main(void) {
	student *s;
    
    s = (student *)malloc(sizeof(student));
    if (s == NULL) {
    	fprintf(stderr, "๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋ถ€์กฑํ•ด์„œ ํ• ๋‹นํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\n");
        exit(1);
    }
    
    strcpy(s->name, "Park");
    s->age = 20;
    
    free(s);
    return 0;
}
profile
๐Ÿ€ an evenful day, life, journey
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€