C - Creating and stacking pointers

Hello.
I want to fill an array-based stack that takes void* with int* and have the values those pointers point to match the "i" variable in a loop. The problem is that pointers all must have a different name to them, and the amount of them I need is not set. Therefore, I thought of creating them, allocating memory and setting their values within a loop? Like:

1
2
3
4
5
6
size_t* pointer1 = malloc (size_t);
*pointer1 = 1;
size_t* pointer2 = malloc (size_t);
*pointer2 = &2;
size_t* pointer3 = malloc (size_t);
etc.


But I don't know how can I get to name them dynamically (and if it's possible I doubt it'd be good practice). This is all being done just to test if the stack works. If there's an alternative, easier solution, let me know.

KEEP IN MIND THIS IS C! Last time I asked I forgot to write that :/

My code in case it's necessary:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
struct stack {
    void **data;
    size_t capacity;
    size_t n_of_elements;
};

//This is the function I intend to create the pointers on.
//Right now it only uses one pointer whose pointed value changes and that
//doesn't work because I need to pop the values later on and I can't do it
//if the memory address doesn't change whilst values are being removed.

bool stack_add_numbers (stack_t* stack, size_t qty)
{
    size_t* iter_point; 
    for (size_t i=0; i<=qty;i++)
    {
        iter_point = &i;
        stack_push(stack, iter_point); 
        if (stack_peek_top(stack)!=NULL)
        {
           void* value = stack_peek_top(stack);
           printf("%i", *((int*)value));
        }
     }
    return true;
}

void* stack_peek_top(const stack_t* stack)
    {return stack->data[(stack->n_of_elements)-1];}

bool stack_push(stack_t* stack, void* value)
{
    if((stack->n_of_elements)>=(stack->capacity))
        {return (stack_redimension(stack, stack->n_of_elements, stack->capacity, value));}
    else
    {
        stack->data[stack->n_of_elements]=value;
        stack->n_of_elements++;
        return true;
    }
}

void* stack_pop(stack_t* stack)
{
    if(!stack_is_empty(stack))
    {
        void* give_back = stack_ver_tope(stack);
        stack->data[(stack->n_of_elements-1)]=NULL;
        stack->n_of_elements--;

        if ((stack->n_of_elements)<((stack->capacity)/4))
            {(stack_redimension(stack, stack->n_of_elements, stack->capacity, give_back));}

        return give_back;
    }
    else return NULL;
}
Topic archived. No new replies allowed.