View difference between Paste ID: sjWV6MZS and 1K6aNBce
SHOW: | | - or go back to the newest paste.
1
2
#include <stdio.h>
3
#include <stdlib.h>
4
5
struct elem{
6
    int *vect;
7
    int lenght;
8
};
9
typedef struct elem array;
10
typedef array *Array;
11-
Array newArray(int);
11+
12
Array newArray();
13
void insert(Array, int);
14
void stampa(Array);
15
16
int main()
17
{
18
    int i, n, x;
19
    Array a = newArray();
20
    printf("Quanti elementi?");
21
    scanf("%d", &n);
22
    for(i = 0; i < n; i++){
23
        printf("Elemento n.%d: ", i+1);
24
        scanf("%d", &x);
25
	insert(a,x);
26
    }
27
    stampa(a);
28
    return 0;
29
}
30
31
Array newArray(){
32
    Array new = (Array)malloc(sizeof(array));
33
    new->vect = (int *)malloc(sizeof(int));
34
    new->lenght = 0;
35
    return new;
36
}
37
38-
    a->dim++;
38+
39-
    a->vect = (int *)realloc(a->dim,sizeof(int));
39+
    a->lenght++;
40-
    a->vect[a->dim-1] = elem;
40+
    a->vect = (int *)realloc(a->vect,a->lenght * sizeof(int));
41
    a->vect[a->lenght-1] = elem;
42
}
43
44
45
void stampa(Array a){
46
    int i;
47
    for(i = 0; i < a->lenght; i++)
48
        printf("%d -> ", a->vect[i]);
49
    printf("STOP.\n");
50
}