Skip to content

Instantly share code, notes, and snippets.

@nomaddo
Created January 8, 2017 14:17
Show Gist options
  • Save nomaddo/ccaa64c1d213322410888da8a0577fcc to your computer and use it in GitHub Desktop.
Save nomaddo/ccaa64c1d213322410888da8a0577fcc to your computer and use it in GitHub Desktop.
typedef struct t t; みたいな宣言の意味; 同じデータ構造なんだけど、名前を変えて取り違えたときに警告を出すため
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#define dfunc(f) ((void (void *)) f) \
typedef struct set_t {
int cap;
int num;
void ** t;
} set;
set * SET_new (int n)
{
set * t = malloc(sizeof(set));
void * x = malloc(sizeof(void *) * n);
t->cap = n;
t->num = 0;
t->t = x;
return t;
}
bool SET_find (void * e, set * s){
for (int i = 0; i < s->num; i++)
{
if (s->t[i] == e)
return true;
}
return false;
}
void SET_add (void * e, set * s){
if (s->cap > s->num) {
s->t = realloc (s->t, s->cap * 2);
s->cap = s->cap * 2;
}
s->t[s->num] = e;
s->num += 1;
}
void dump (set * s, void f (void *)) {
for (int i = 0; i < s->num; i++)
f (s->t[i]);
}
void print_int (int * n){
printf("%d\n", * n);
}
int * Int_new(int n) {
int * c = malloc(sizeof(int));
*c = n;
return c;
}
typedef struct intset intset;
intset * INTSET_new(int n) {
return (intset *) SET_new(n);
}
void INTSET_add (int n, intset * s) {
int * c = malloc(sizeof(int));
*c = n;
SET_add (c, (set *)s);
}
int main()
{
// 警告が出ない
intset * s = INTSET_new(1);
INTSET_add(1, s);
INTSET_add(2, s);
INTSET_add(3, s);
INTSET_add(4, s);
// 警告が出る
// a.c: In function ‘main’:
// a.c:78:13: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
// set * t = INTSET_new(1);
// ^
set * t = INTSET_new(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment