0

I am currently trying to build a simple calculator using a linked list in C. My idea is to have every node hold the data types: number, operator and parenthesis. Since I'm still new to structs I'm now wondering if it would cause issues to just initialize one data type and leave the other two uninitialized when when creating a node?

struct node {     
    int number; 
    char operator; 
    char parenthesis;    
    struct node* next;
};

  • 1
    As long as you don't use any member variable without initializing it, it will be fine. You can assign to the members at any rtime. – Some programmer dude Dec 10 '22 at 12:07
  • If you initialize some elements of a struct but not all, the others are initialized to zero. So doing `struct node n = { 3 };` will make `n.operator == '\0'`, `n.parenthesis == '\0'`, and `n.next == NULL`. You can also use [designated initializers](https://stackoverflow.com/questions/47202557/what-is-a-designated-initializer-in-c) if you don't want to keep track of the order of the members. – Nate Eldredge Dec 10 '22 at 16:24
  • Tip: It would be better to design your program based on a hierarchy rather than a list... Simple "1+1" is a parent node (the operation '+') with two child nodes (the operands)... – Fe2O3 Dec 10 '22 at 19:46

0 Answers0