C言語なら俺に聞け(入門編)Part 117
■ このスレッドは過去ログ倉庫に格納されています
0767デフォルトの名無しさん
2013/08/02(金) NY:AN:NY.ANそういうプログラムを書く人が多いからC言語は使いづらいってイメージが広まるんだよ
>>761
#include <stdio.h>
#include <stdlib.h>
// 最初に確保するサイズ。最大サイズではない
#define DEF_SIZE 100
typedef struct {
char *data;
int length;
int allocated;
} Stack;
void init(Stack *stack){
stack->data = (char *) malloc(sizeof(char) * DEF_SIZE);
stack->length = 0;
stack->allocated = DEF_SIZE;
}
void push(char c, Stack *stack){
if (++stack->length > stack->allocated){
stack->data = (char *) realloc(stack->data, sizeof(char) * (stack->length));
}
stack->data[stack->length - 1] = c;
}
char pop(Stack *stack){
return stack->data[--stack->length];
}
■ このスレッドは過去ログ倉庫に格納されています