Trouble Including Externally Declared Enumeration - C Code











up vote
-1
down vote

favorite












Update: The issue is resolved. Here is code that compiles properly.






---instruction.h---
#ifndef INSTRUCTION_H
#define INSTRUCTION_H

typedef enum OPCODE {ADD = 0x20,ADDI = 0x8,SUB = 0x22,MULT = 0x18,BEQ = 0x4,LW = 0x23,SW = 0x2B} opcode;
/*opcode is OPCODEs alias*/
typedef struct INSTRUCTION {
opcode op;
int rs;
int rt;
int rd;
int Imm;
} inst;
/*inst is INSTRUCTIONs alias*/
#endif // INSTRUCTION_H

---parser.c---
#include <stdio.h>
#include "instruction.h"
void parser(char *instruction)
{
/*Parse character string into instruction components*/
inst set1 = {LW,0,1,2,0};
printf("parsing");
};

int main()
{
char *instruction;
instruction = NULL;
parser(instruction);
};
/*pass in pointer for instruction being passed in*/
/*pointing to address of instruction being passed in*/
/*Parser return type is struct inst*/





I cannot seem to get my enumeration type "opcode" to be recognized in my main c file. I included the header file. I am fairly new to C, so haven't made much ground on the issue for some time now and wanted to see if anyone knew why I was getting the error messages below. My guess is the linking the header file is not working properly. Any help is much appreciated.enter image description here



---instruction.h----

#ifndef INSTRUCTION_H
#define INSTRUCTION_H

typedef enum {add = 32,addi = 8,sub = 34,mult = 24,beq = 4,lw = 35,sw = 43}opcode;
extern opcode oper;
typedef struct {
opcode op;
int rs;
int rt;
int rd;
int Imm;
}inst;
#endif // INSTRUCTION_H

---Parser.c---

#include <stdio.h>
#include "instruction.h"
void parser(char *inst)
{
/*Parse character string into instruction components*/
struct inst{lw,0,1,2,0};

};

int main()
{
char *instruction;
instruction = NULL;
parser(instruction);
};









share|improve this question




























    up vote
    -1
    down vote

    favorite












    Update: The issue is resolved. Here is code that compiles properly.






    ---instruction.h---
    #ifndef INSTRUCTION_H
    #define INSTRUCTION_H

    typedef enum OPCODE {ADD = 0x20,ADDI = 0x8,SUB = 0x22,MULT = 0x18,BEQ = 0x4,LW = 0x23,SW = 0x2B} opcode;
    /*opcode is OPCODEs alias*/
    typedef struct INSTRUCTION {
    opcode op;
    int rs;
    int rt;
    int rd;
    int Imm;
    } inst;
    /*inst is INSTRUCTIONs alias*/
    #endif // INSTRUCTION_H

    ---parser.c---
    #include <stdio.h>
    #include "instruction.h"
    void parser(char *instruction)
    {
    /*Parse character string into instruction components*/
    inst set1 = {LW,0,1,2,0};
    printf("parsing");
    };

    int main()
    {
    char *instruction;
    instruction = NULL;
    parser(instruction);
    };
    /*pass in pointer for instruction being passed in*/
    /*pointing to address of instruction being passed in*/
    /*Parser return type is struct inst*/





    I cannot seem to get my enumeration type "opcode" to be recognized in my main c file. I included the header file. I am fairly new to C, so haven't made much ground on the issue for some time now and wanted to see if anyone knew why I was getting the error messages below. My guess is the linking the header file is not working properly. Any help is much appreciated.enter image description here



    ---instruction.h----

    #ifndef INSTRUCTION_H
    #define INSTRUCTION_H

    typedef enum {add = 32,addi = 8,sub = 34,mult = 24,beq = 4,lw = 35,sw = 43}opcode;
    extern opcode oper;
    typedef struct {
    opcode op;
    int rs;
    int rt;
    int rd;
    int Imm;
    }inst;
    #endif // INSTRUCTION_H

    ---Parser.c---

    #include <stdio.h>
    #include "instruction.h"
    void parser(char *inst)
    {
    /*Parse character string into instruction components*/
    struct inst{lw,0,1,2,0};

    };

    int main()
    {
    char *instruction;
    instruction = NULL;
    parser(instruction);
    };









    share|improve this question


























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      Update: The issue is resolved. Here is code that compiles properly.






      ---instruction.h---
      #ifndef INSTRUCTION_H
      #define INSTRUCTION_H

      typedef enum OPCODE {ADD = 0x20,ADDI = 0x8,SUB = 0x22,MULT = 0x18,BEQ = 0x4,LW = 0x23,SW = 0x2B} opcode;
      /*opcode is OPCODEs alias*/
      typedef struct INSTRUCTION {
      opcode op;
      int rs;
      int rt;
      int rd;
      int Imm;
      } inst;
      /*inst is INSTRUCTIONs alias*/
      #endif // INSTRUCTION_H

      ---parser.c---
      #include <stdio.h>
      #include "instruction.h"
      void parser(char *instruction)
      {
      /*Parse character string into instruction components*/
      inst set1 = {LW,0,1,2,0};
      printf("parsing");
      };

      int main()
      {
      char *instruction;
      instruction = NULL;
      parser(instruction);
      };
      /*pass in pointer for instruction being passed in*/
      /*pointing to address of instruction being passed in*/
      /*Parser return type is struct inst*/





      I cannot seem to get my enumeration type "opcode" to be recognized in my main c file. I included the header file. I am fairly new to C, so haven't made much ground on the issue for some time now and wanted to see if anyone knew why I was getting the error messages below. My guess is the linking the header file is not working properly. Any help is much appreciated.enter image description here



      ---instruction.h----

      #ifndef INSTRUCTION_H
      #define INSTRUCTION_H

      typedef enum {add = 32,addi = 8,sub = 34,mult = 24,beq = 4,lw = 35,sw = 43}opcode;
      extern opcode oper;
      typedef struct {
      opcode op;
      int rs;
      int rt;
      int rd;
      int Imm;
      }inst;
      #endif // INSTRUCTION_H

      ---Parser.c---

      #include <stdio.h>
      #include "instruction.h"
      void parser(char *inst)
      {
      /*Parse character string into instruction components*/
      struct inst{lw,0,1,2,0};

      };

      int main()
      {
      char *instruction;
      instruction = NULL;
      parser(instruction);
      };









      share|improve this question















      Update: The issue is resolved. Here is code that compiles properly.






      ---instruction.h---
      #ifndef INSTRUCTION_H
      #define INSTRUCTION_H

      typedef enum OPCODE {ADD = 0x20,ADDI = 0x8,SUB = 0x22,MULT = 0x18,BEQ = 0x4,LW = 0x23,SW = 0x2B} opcode;
      /*opcode is OPCODEs alias*/
      typedef struct INSTRUCTION {
      opcode op;
      int rs;
      int rt;
      int rd;
      int Imm;
      } inst;
      /*inst is INSTRUCTIONs alias*/
      #endif // INSTRUCTION_H

      ---parser.c---
      #include <stdio.h>
      #include "instruction.h"
      void parser(char *instruction)
      {
      /*Parse character string into instruction components*/
      inst set1 = {LW,0,1,2,0};
      printf("parsing");
      };

      int main()
      {
      char *instruction;
      instruction = NULL;
      parser(instruction);
      };
      /*pass in pointer for instruction being passed in*/
      /*pointing to address of instruction being passed in*/
      /*Parser return type is struct inst*/





      I cannot seem to get my enumeration type "opcode" to be recognized in my main c file. I included the header file. I am fairly new to C, so haven't made much ground on the issue for some time now and wanted to see if anyone knew why I was getting the error messages below. My guess is the linking the header file is not working properly. Any help is much appreciated.enter image description here



      ---instruction.h----

      #ifndef INSTRUCTION_H
      #define INSTRUCTION_H

      typedef enum {add = 32,addi = 8,sub = 34,mult = 24,beq = 4,lw = 35,sw = 43}opcode;
      extern opcode oper;
      typedef struct {
      opcode op;
      int rs;
      int rt;
      int rd;
      int Imm;
      }inst;
      #endif // INSTRUCTION_H

      ---Parser.c---

      #include <stdio.h>
      #include "instruction.h"
      void parser(char *inst)
      {
      /*Parse character string into instruction components*/
      struct inst{lw,0,1,2,0};

      };

      int main()
      {
      char *instruction;
      instruction = NULL;
      parser(instruction);
      };





      ---instruction.h---
      #ifndef INSTRUCTION_H
      #define INSTRUCTION_H

      typedef enum OPCODE {ADD = 0x20,ADDI = 0x8,SUB = 0x22,MULT = 0x18,BEQ = 0x4,LW = 0x23,SW = 0x2B} opcode;
      /*opcode is OPCODEs alias*/
      typedef struct INSTRUCTION {
      opcode op;
      int rs;
      int rt;
      int rd;
      int Imm;
      } inst;
      /*inst is INSTRUCTIONs alias*/
      #endif // INSTRUCTION_H

      ---parser.c---
      #include <stdio.h>
      #include "instruction.h"
      void parser(char *instruction)
      {
      /*Parse character string into instruction components*/
      inst set1 = {LW,0,1,2,0};
      printf("parsing");
      };

      int main()
      {
      char *instruction;
      instruction = NULL;
      parser(instruction);
      };
      /*pass in pointer for instruction being passed in*/
      /*pointing to address of instruction being passed in*/
      /*Parser return type is struct inst*/





      ---instruction.h---
      #ifndef INSTRUCTION_H
      #define INSTRUCTION_H

      typedef enum OPCODE {ADD = 0x20,ADDI = 0x8,SUB = 0x22,MULT = 0x18,BEQ = 0x4,LW = 0x23,SW = 0x2B} opcode;
      /*opcode is OPCODEs alias*/
      typedef struct INSTRUCTION {
      opcode op;
      int rs;
      int rt;
      int rd;
      int Imm;
      } inst;
      /*inst is INSTRUCTIONs alias*/
      #endif // INSTRUCTION_H

      ---parser.c---
      #include <stdio.h>
      #include "instruction.h"
      void parser(char *instruction)
      {
      /*Parse character string into instruction components*/
      inst set1 = {LW,0,1,2,0};
      printf("parsing");
      };

      int main()
      {
      char *instruction;
      instruction = NULL;
      parser(instruction);
      };
      /*pass in pointer for instruction being passed in*/
      /*pointing to address of instruction being passed in*/
      /*Parser return type is struct inst*/






      c enums header header-files enumeration






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 at 20:36

























      asked Nov 18 at 20:16









      Cormac Kennedy

      115




      115
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          struct inst{lw,0,1,2,0};


          This looks like it's supposed to be a variable declaration, but I don't see a name for the variable. Try:



          struct inst name_of_the_variable = {lw,0,1,2,0};


          As a side note, enum values are global constants, so it's probably not a good idea to give them names like lw that can be confused for variables. Standard practice would be to use all-caps for the names and give them a prefix… say, OPCODE_ADD, OPCODE_LW, etc.






          share|improve this answer





















          • Thank you! This solved the issue!
            – Cormac Kennedy
            Nov 18 at 20:37


















          up vote
          0
          down vote













          This is not a valid variable definition:



          struct inst{lw,0,1,2,0};


          There's no struct inst defined, only inst, there's no variable name, and you need = to use an initializer. To create a variable of this type an initialize it, you need:



          inst myinst = {lw,0,1,2,0};


          Also, your function has a parameter named inst which masks the type inst. You need to give it a different name:



          void parser(char *instruction)





          share|improve this answer





















          • Thank you! This solved the issue!
            – Cormac Kennedy
            Nov 18 at 20:37






          • 1




            @CormacKennedy Glad I could help. Feel free to accept this answer if you found it useful.
            – dbush
            Nov 18 at 20:39











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365024%2ftrouble-including-externally-declared-enumeration-c-code%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          struct inst{lw,0,1,2,0};


          This looks like it's supposed to be a variable declaration, but I don't see a name for the variable. Try:



          struct inst name_of_the_variable = {lw,0,1,2,0};


          As a side note, enum values are global constants, so it's probably not a good idea to give them names like lw that can be confused for variables. Standard practice would be to use all-caps for the names and give them a prefix… say, OPCODE_ADD, OPCODE_LW, etc.






          share|improve this answer





















          • Thank you! This solved the issue!
            – Cormac Kennedy
            Nov 18 at 20:37















          up vote
          0
          down vote













          struct inst{lw,0,1,2,0};


          This looks like it's supposed to be a variable declaration, but I don't see a name for the variable. Try:



          struct inst name_of_the_variable = {lw,0,1,2,0};


          As a side note, enum values are global constants, so it's probably not a good idea to give them names like lw that can be confused for variables. Standard practice would be to use all-caps for the names and give them a prefix… say, OPCODE_ADD, OPCODE_LW, etc.






          share|improve this answer





















          • Thank you! This solved the issue!
            – Cormac Kennedy
            Nov 18 at 20:37













          up vote
          0
          down vote










          up vote
          0
          down vote









          struct inst{lw,0,1,2,0};


          This looks like it's supposed to be a variable declaration, but I don't see a name for the variable. Try:



          struct inst name_of_the_variable = {lw,0,1,2,0};


          As a side note, enum values are global constants, so it's probably not a good idea to give them names like lw that can be confused for variables. Standard practice would be to use all-caps for the names and give them a prefix… say, OPCODE_ADD, OPCODE_LW, etc.






          share|improve this answer












          struct inst{lw,0,1,2,0};


          This looks like it's supposed to be a variable declaration, but I don't see a name for the variable. Try:



          struct inst name_of_the_variable = {lw,0,1,2,0};


          As a side note, enum values are global constants, so it's probably not a good idea to give them names like lw that can be confused for variables. Standard practice would be to use all-caps for the names and give them a prefix… say, OPCODE_ADD, OPCODE_LW, etc.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 at 20:20









          duskwuff

          144k19174225




          144k19174225












          • Thank you! This solved the issue!
            – Cormac Kennedy
            Nov 18 at 20:37


















          • Thank you! This solved the issue!
            – Cormac Kennedy
            Nov 18 at 20:37
















          Thank you! This solved the issue!
          – Cormac Kennedy
          Nov 18 at 20:37




          Thank you! This solved the issue!
          – Cormac Kennedy
          Nov 18 at 20:37












          up vote
          0
          down vote













          This is not a valid variable definition:



          struct inst{lw,0,1,2,0};


          There's no struct inst defined, only inst, there's no variable name, and you need = to use an initializer. To create a variable of this type an initialize it, you need:



          inst myinst = {lw,0,1,2,0};


          Also, your function has a parameter named inst which masks the type inst. You need to give it a different name:



          void parser(char *instruction)





          share|improve this answer





















          • Thank you! This solved the issue!
            – Cormac Kennedy
            Nov 18 at 20:37






          • 1




            @CormacKennedy Glad I could help. Feel free to accept this answer if you found it useful.
            – dbush
            Nov 18 at 20:39















          up vote
          0
          down vote













          This is not a valid variable definition:



          struct inst{lw,0,1,2,0};


          There's no struct inst defined, only inst, there's no variable name, and you need = to use an initializer. To create a variable of this type an initialize it, you need:



          inst myinst = {lw,0,1,2,0};


          Also, your function has a parameter named inst which masks the type inst. You need to give it a different name:



          void parser(char *instruction)





          share|improve this answer





















          • Thank you! This solved the issue!
            – Cormac Kennedy
            Nov 18 at 20:37






          • 1




            @CormacKennedy Glad I could help. Feel free to accept this answer if you found it useful.
            – dbush
            Nov 18 at 20:39













          up vote
          0
          down vote










          up vote
          0
          down vote









          This is not a valid variable definition:



          struct inst{lw,0,1,2,0};


          There's no struct inst defined, only inst, there's no variable name, and you need = to use an initializer. To create a variable of this type an initialize it, you need:



          inst myinst = {lw,0,1,2,0};


          Also, your function has a parameter named inst which masks the type inst. You need to give it a different name:



          void parser(char *instruction)





          share|improve this answer












          This is not a valid variable definition:



          struct inst{lw,0,1,2,0};


          There's no struct inst defined, only inst, there's no variable name, and you need = to use an initializer. To create a variable of this type an initialize it, you need:



          inst myinst = {lw,0,1,2,0};


          Also, your function has a parameter named inst which masks the type inst. You need to give it a different name:



          void parser(char *instruction)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 at 20:21









          dbush

          90k12100131




          90k12100131












          • Thank you! This solved the issue!
            – Cormac Kennedy
            Nov 18 at 20:37






          • 1




            @CormacKennedy Glad I could help. Feel free to accept this answer if you found it useful.
            – dbush
            Nov 18 at 20:39


















          • Thank you! This solved the issue!
            – Cormac Kennedy
            Nov 18 at 20:37






          • 1




            @CormacKennedy Glad I could help. Feel free to accept this answer if you found it useful.
            – dbush
            Nov 18 at 20:39
















          Thank you! This solved the issue!
          – Cormac Kennedy
          Nov 18 at 20:37




          Thank you! This solved the issue!
          – Cormac Kennedy
          Nov 18 at 20:37




          1




          1




          @CormacKennedy Glad I could help. Feel free to accept this answer if you found it useful.
          – dbush
          Nov 18 at 20:39




          @CormacKennedy Glad I could help. Feel free to accept this answer if you found it useful.
          – dbush
          Nov 18 at 20:39


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365024%2ftrouble-including-externally-declared-enumeration-c-code%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ottavio Pratesi

          Tricia Helfer

          15 giugno