Thread: How can I rename the variable(duh I am not even sure what to ask)

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    18

    How can I rename the variable(duh I am not even sure what to ask)

    The title is hard to choose because maybe I don't exactly know what to ask.

    But I will try to explain as much as I could.

    So I have this certain function
    Code:
    uint8_t pins_control[2];
    uint8_t pins_data[8];
    
    void setPins(uint8_t rs, uint8_t en, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
    
    void setPins(uint8_t rs, uint8_t en, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
    {
        //instrucion register pins
        pins_control[0] = rs; 
        pins_control[1] = en;
        
        //data register pins
        pins_data[0] = d0;
        pins_data[1] = d1;
        pins_data[2] = d2;
        pins_data[3] = d3;
        pins_data[4] = d4;
        pins_data[5] = d5;
        pins_data[6] = d6;
        pins_data[7] = d7;
    }
    If I want to use d0, I would have to call pins_data[0] inside a statement. This is hard to know what pin it is pointing to. I would like to create a name for every variable in pins_control[] and pins_data[].

    So, if I ever want to use them, I would just type "somefunction(someName)" instead of "somefunction(pins_data[0])"

    Is there a way to solve this(there should be right?)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    An enum perhaps?
    Code:
    enum {
      pin_select,
      pin_enable
    };
    
    ...
    
        pins_control[pin_select] = rs; 
        pins_control[pin_enable] = en;
    You can assign your own values to enums if you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    18
    Great solution! I should have really learned enum first before creating a library...

    Btw, how do you know the name of the pins(select, enable). They are really the REAL name of the pins...

    EDIT:

    Do I declare enum in header file(LCD.h) or in the main(LCD.c) file?
    Last edited by cyberjupiter; 09-21-2017 at 11:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 01-26-2009, 08:01 PM
  2. Rename Error
    By kempy1000 in forum C++ Programming
    Replies: 5
    Last Post: 12-17-2007, 07:57 AM
  3. Rename a file
    By Deb in forum C Programming
    Replies: 3
    Last Post: 04-16-2007, 02:43 PM
  4. is there away to rename windows api ,
    By Anddos in forum Windows Programming
    Replies: 5
    Last Post: 08-20-2006, 11:12 PM
  5. Rename a value in the registry?
    By cyberpuck52 in forum C++ Programming
    Replies: 10
    Last Post: 01-22-2002, 09:58 PM

Tags for this Thread