0
struct list{
    int *items;
    int length;
};
    
void add(int a, struct list input);
    
void print(struct list input);
    
int main(void){
    int size=0;
    
    struct list mine;
    mine.items=(int*) malloc(size * sizeof(int));
    mine.length=size;
    
    add(4,mine);
    add(5, mine);
    add(6, mine);
    
    print(mine);
    
}
    
void add(int a, struct list input){
    static int index=0;
    printf("%d\n", input.length);
    if(index>=input.length){
        input.items=(int*)realloc(input.items, (input.length+1)*sizeof(int));
        input.length++;
    }
    input.items[index]=a;
    index++;
}
    
void print(struct list input){
    for(int i = 0; i<input.length ;i++){
        if(input.items[i]>0 && input.items[i]<16){
            if(input.items[i]<10){
                printf("L0%d ",input.items[i]);
            }
            else{
                printf("L%d ",input.items[i]);
            }
        }
        else if(input.items[i]>15 && input.items[i]<31){
            printf("I%d ", input.items[i]);
        }
        else if(input.items[i]>30 && input.items[i]<46){
            printf("N%d ", input.items[i]);
        }
        else if(input.items[i]>45 && input.items[i]<61){
            printf("U%d ", input.items[i]);
        }
        else if(input.items[i]>60 && input.items[i]<76){
            printf("X%d ", input.items[i]);
        }
    }
}

This program is supposed to print a number added to an array in a structure with a prefix 'L', 'I', 'N', 'U', 'X' depending on how big the number is. The issue is that input.length does not seem to want to increment in the add() function which causes the print() function to not print anything. Any clues as to why this is happening?

Barmar
  • 741,623
  • 53
  • 500
  • 612
cedlcc
  • 1
  • Arguments are passed by value. You're incrementing a copy of the structure variable. – Barmar Nov 13 '21 at 23:18
  • Welcome to Stack Overflow! [dont cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Nov 13 '21 at 23:18

0 Answers0