Pointer Example
Pointer Example
1)
typedef struct entry {
double x;
struct entry *e;
}entry;
2)
typedef struct entry {
double x;
struct entry *e;
}entry;
3)
typedef struct entry {
double x;
struct entry *e;
}entry;
4)
typedef struct test {
struct test **a;
}test;
int main() {
test *m;
int i;
m = (test *)malloc(sizeof(test));
m->a = (test **)malloc(sizeof(test *) * 3);
for(i = 0; i < 2; i++) {
m->a[i] = (test *)malloc(sizeof(test));
}
m->a[2] = (test*)malloc(3 * sizeof(test));
m = f(&(m->a));
}
test *f(test ***ppp) {
test *p, **pp;
pp = *ppp; p = *pp;
*pp = (test *)malloc(2 * sizeof(test));
return pp[1];
}
5)
6)
typedef struct node {
char c;
struct node *next;
}node;
typedef node *list;
void append(list *l, int i) {
list t;
t = (node *)malloc(sizeof(node));
t->c = i + 'a' + 2;
t->next = NULL;
if(*l == NULL) {
*l = t;
return;
}
(*l)->next = t;
return;
}
int main() {
int i;
list p;
p = NULL;
for(i = 0; i < 3; i++) {
append(&p, i);
}
}
7)
typedef struct node {
int m;
struct node *left, *right;
}node;
typedef node *tree;
void insert(tree *r, int i) {
tree t, p, q;
int j;
t = (node *)malloc(sizeof(node));
t->m = i;
t->left = t->right = NULL;
if(*r == NULL) {
*r = t;
return;
}
q = p = *r;
j = i;
while(i) {
if(!p) q = p;
if(i % 2 && p)
p = p->left;
else if(p)
p = p->right;
i--;
}
if(j % 2)
q->left = t;
else
q->right = t;
return;
}
int main() {
int i;
tree p;
p = NULL;
for(i = 0; i < 3; i++) {
insert(&p, i);
}
}