Search code examples
carrayslinuxvariables

C doesn't put data in arrays or variables


I'm the beginner in C programming, for now I'm using Linux and tried both gcc and clang compilers. However, I'm facing the one problem, sometimes C doesn't put data in the array or variable. For example, there is one of my simple codes, that doesn't work completely:

    #include <stdio.h>
    #define size 10

    struct stack{
        int structTop;
        int elemNum[size];
    };

    int create (struct stack s);
    int full (struct stack s);
    void push (int elem, struct stack s);


    void main() {

struct stack s1;
struct stack s2;
struct stack s3;
int a = 545;
create(s1);
push(a, s1);
push(5, s1);
push(a, s1);
push(1, s1);
push(6, s1);
push(4, s1);
push(7, s1);
push(8, s1);
int i = 0;
while (i<4){
    printf("%d\n", s1.elemNum[i]);
    i++;
}
    }

    int create (struct stack s){
    s.structTop = -1;
    return 0;
    }

    int full(struct stack s){
if(s.structTop == size-1) {
    return 1;
}
else {
    return 0;
}
    }
    void push(int elem, struct stack s){
if(full(s)){
    printf("Stack is full");
}
else {
    s.structTop++;
    s.elemNum[s.structTop]=elem;
}
    }

As output I'm getting data, which was inside array from the beginning (zeros or random numbers). Also it was only one of the codes, I have a couple of larger ones, that have the same problem. Variables and arrays inside them are working 50/50, sometimes yes, sometimes no, even if declarations and functions are the same. Someone told me, that it could be the matter of compiler, but I tried different ones and also have a friend with the same Kali Linux as me facing this problem on a different PC.


Solution

  • You need to pass pointers to the struct, i.e. int create (struct stack *s) instead of int create (struct stack s). The same for push. Otherwise, you pass a copy and in the function, you will alter a copy and not the originally passed object from main.

    The thing why it sometimes worked, at least partially, is that when passing objects by value, these values will be put temporarily on a stack; It seems that the same object from main had several time been pushed right on the same position on the stack, such that it seemed as if it was always the same object. But - as you recognised - this really occasional.

    The signatures of your methods should look as follows:

    int create (struct stack *s);
    int full (const struct stack *s);
    void push (int elem, struct stack *s);
    

    Note that - since passing now pointers - you have to access the elements of s using -> (and not .), e.g. s->structTop = -1 instead of s.structTop = -1; and that you have to pass the address of a stack (not the stack itself, e.g. push(a, &s1) instead of push(a, s1). Note further that in int full (const struct stack *s), I declared s as const, as the function does not intend to alter any value of the members of s.

    #include <stdio.h>
    #define size 10
    
    struct stack{
        int structTop;
        int elemNum[size];
    };
    
    int create (struct stack *s);
    int full (const struct stack *s);
    void push (int elem, struct stack *s);
    
    
    int main() {
    
        struct stack s1;
        int a = 545;
        create(&s1);
        push(a, &s1);
        push(5, &s1);
        push(a, &s1);
        push(1, &s1);
        push(6, &s1);
        push(4, &s1);
        push(7, &s1);
        push(8, &s1);
        int i = 0;
        while (i<4){
            printf("%d\n", s1.elemNum[i]);
            i++;
        }
    }
    
    int create (struct stack *s){
        s->structTop = -1;
        return 0;
    }
    
    int full(const struct stack *s){
        if(s->structTop == size-1) {
            return 1;
        }
        else {
            return 0;
        }
    }
    void push(int elem, struct stack *s){
        if(full(s)){
            printf("Stack is full");
        }
        else {
            s->structTop++;
            s->elemNum[s->structTop]=elem;
        }
    }