Hot!Use of a pointer

Page: 123 > Showing page 1 of 3
Author
TucsonDon
Senior Member
  • Total Posts : 137
  • Reward points : 0
  • Joined: 2015/08/07 13:32:43
  • Location: Tucson AZ
  • Status: offline
2017/11/22 06:27:26 (permalink)
0

Use of a pointer

I am trying to use a pointer to point at a enum list, the issue that I have is when I step through it the code *pdisplay shows "null". I am hoping that someone can point out what I am doing wrong.

unsigned char DisplayText[3];
char *pdisplay;
typedef enum
{
OpMenu,
          item1,
          item2,
          item3,
}Operation;
void MenuList (void)
 {
Opmenu = pdisplay;
DisplayText[0] = *pdisplay;
DisplayText[1] = *pdisplay + 1;
 
LCDDisplay();
}

post edited by TucsonDon - 2017/11/22 06:38:52

“Remember what may seem elementary to you may be very complex to someone else.”
#1

42 Replies Related Threads

    Jim Nickerson
    User 452 _
    • Total Posts : 4360
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: online
    Re: Use of a pointer 2017/11/22 06:57:58 (permalink)
    0
     
    TucsonDon
    I am trying to use a pointer to point at a enum list, the issue that I have is when I step through it the code *pdisplay shows "null". I am hoping that someone can point out what I am doing wrong.

    unsigned char DisplayText[3];
    char *pdisplay;
    typedef enum
    {
    OpMenu,
              item1,
              item2,
              item3,
    }Operation;
     
    void MenuList (void)
     {
    Opmenu = pdisplay;
    DisplayText[0] = *pdisplay;
    DisplayText[1] = *pdisplay + 1;
    LCDDisplay();
    }


    I do not think you have used typedef correctly.


    typedef creates an "alias".
    the compiler must have thrown errors ?
     
     
    post edited by Jim Nickerson - 2017/11/22 07:00:24
    #2
    TucsonDon
    Senior Member
    • Total Posts : 137
    • Reward points : 0
    • Joined: 2015/08/07 13:32:43
    • Location: Tucson AZ
    • Status: offline
    Re: Use of a pointer 2017/11/22 07:00:50 (permalink)
    0
    JANickerson
    I do not think you have used typedef correctly. 


    How do I correct it?

    “Remember what may seem elementary to you may be very complex to someone else.”
    #3
    Jim Nickerson
    User 452 _
    • Total Posts : 4360
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: online
    Re: Use of a pointer 2017/11/22 07:01:22 (permalink)
    0
    What errors did the compiler display ?
    #4
    TucsonDon
    Senior Member
    • Total Posts : 137
    • Reward points : 0
    • Joined: 2015/08/07 13:32:43
    • Location: Tucson AZ
    • Status: offline
    Re: Use of a pointer 2017/11/22 07:06:51 (permalink)
    0
    Right now I haw tried

    pdisplay -> Operation OpMenu;

    I get "error:(195) expression syntax"

    “Remember what may seem elementary to you may be very complex to someone else.”
    #5
    Jim Nickerson
    User 452 _
    • Total Posts : 4360
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: online
    Re: Use of a pointer 2017/11/22 07:15:45 (permalink)
    +1 (1)
    I think it might be a good idea to have a look in "K & R" for "typedef".
    Why is it you have suddenly developed this interest in "pointers" ?
    #6
    rodims
    Super Member
    • Total Posts : 932
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: Use of a pointer 2017/11/22 07:17:39 (permalink)
    0
    This was not part of your first question, you should stick to your question.
     
    the issue that I have is when I step through it the code *pdisplay shows "null"

    What do you expect, if you never assign any value to pdisplay ?
     
    I am trying to use a pointer to point at a enum list

    Also completely unclear what you have in mind (also when looking at your code, which looks weird).
     
    #7
    TucsonDon
    Senior Member
    • Total Posts : 137
    • Reward points : 0
    • Joined: 2015/08/07 13:32:43
    • Location: Tucson AZ
    • Status: offline
    Re: Use of a pointer 2017/11/22 07:59:15 (permalink)
    +1 (1)
    rodims
     
    the issue that I have is when I step through it the code *pdisplay shows "null"

    What do you expect, if you never assign any value to pdisplay ?
     



    I guess I expect is people to understand that maybe I haven't been doing this as long as others and to show me how fix it and not just that I am doing it wrong. If I knew the answer I would not have posted the question.

    “Remember what may seem elementary to you may be very complex to someone else.”
    #8
    Jim Nickerson
    User 452 _
    • Total Posts : 4360
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: online
    Re: Use of a pointer 2017/11/22 08:05:09 (permalink)
    +1 (1)
    Yes it is apparent you have not been doing this for long.
    I doubt if doing the task for you will help you to learn how to code in c.
    You have yet to describe what this code you are writing is to accomplish.
    You are using terms in your question you do not know the meaning of.
    The first step is to look up all the terms and understand them, typedef, pointers....
    #9
    TucsonDon
    Senior Member
    • Total Posts : 137
    • Reward points : 0
    • Joined: 2015/08/07 13:32:43
    • Location: Tucson AZ
    • Status: offline
    Re: Use of a pointer 2017/11/22 08:27:16 (permalink)
    0
    JANickerson
    Yes it is apparent you have not been doing this for long.
    I doubt if doing the task for you will help you to learn how to code in c.
    You have yet to describe what this code you are writing is to accomplish.
    You are using terms in your question you do not know the meaning of.
    The first step is to look up all the terms and understand them, typedef, pointers....


    You are right. I am self taught from books and internet and no formal classes, but I also learn from doing more then reading. Having said that I know that enumeration is a structure. I also know that pdisplay as no value, what I can't figure out is how to correct that so that it point to the elements of the enumeration. 

    “Remember what may seem elementary to you may be very complex to someone else.”
    #10
    Jim Nickerson
    User 452 _
    • Total Posts : 4360
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: online
    Re: Use of a pointer 2017/11/22 08:30:19 (permalink)
    +1 (1)
    If you "know that enumeration is a structure" then please un know this and have a look at https://en.wikipedia.org/wiki/Enumerated_type
    #11
    JanJansen
    Super Member
    • Total Posts : 504
    • Reward points : 0
    • Joined: 2012/08/25 03:26:34
    • Location: 0
    • Status: offline
    Re: Use of a pointer 2017/11/22 08:35:51 (permalink)
    0
    Look : enum is to replace a list of defines without having to fill all the numbers when changing the list.
    #12
    Jim Nickerson
    User 452 _
    • Total Posts : 4360
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: online
    TucsonDon
    Senior Member
    • Total Posts : 137
    • Reward points : 0
    • Joined: 2015/08/07 13:32:43
    • Location: Tucson AZ
    • Status: offline
    Re: Use of a pointer 2017/11/22 08:51:51 (permalink)
    0
    JANickerson
    If you "know that enumeration is a structure" then please un know this and have a look at https://en.wikipedia.org/wiki/Enumerated_type


    I have read this several times in the past and unsure what you are trying to point out, that it is not a structure? Enumeration is used in MCC for I2C and I have been using that as an example and others on the internet.
     
    I know that "typedef" is defining a type and (in the code above) that "Operation" is the alias but unsure of purpose of "OpMenu" as to the format that MPLab lays it out.
     
    I am trying to build a menu, is my use of enumeration not correct?

    “Remember what may seem elementary to you may be very complex to someone else.”
    #14
    Jim Nickerson
    User 452 _
    • Total Posts : 4360
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: online
    Re: Use of a pointer 2017/11/22 09:00:08 (permalink)
    +1 (1)
    Yes an enum is not a structure
    Yes your use of enumeration is not correct
    maybe you could start by describing what it is you intend to do with a flowchart or in pseudo code.
    Please do bear in mind you are using words that have specific meanings the majority of us are aware of.
    When you misuse the words it is very confusing as we do not know what it is you mean.
    This is a structure https://en.wikipedia.org/..._programming_language)
    #15
    andersm
    Super Member
    • Total Posts : 2255
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: Use of a pointer 2017/11/22 09:07:58 (permalink)
    +1 (1)
    In C, an enumeration only declares a symbolic alias for an integer value.
     
    As an early Christmas gift, treat yourself to a copy of "The C Programming Language" (the "K&R" book). It's a bit old, but still one of the best tutorials for the language.
    #16
    1and0
    Access is Denied
    • Total Posts : 7197
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Use of a pointer 2017/11/22 09:08:42 (permalink)
    0
    TucsonDon
    I am trying to build a menu, is my use of enumeration not correct?

    It is not correct. Enumeration (enum) is a user defined data type used to assign names to integral constants, where the names are used to make the code more readable.
     
    #17
    1and0
    Access is Denied
    • Total Posts : 7197
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    TucsonDon
    Senior Member
    • Total Posts : 137
    • Reward points : 0
    • Joined: 2015/08/07 13:32:43
    • Location: Tucson AZ
    • Status: offline
    Re: Use of a pointer 2017/11/22 09:11:26 (permalink)
    0
    andersm
    In C, an enumeration only declares a symbolic alias for an integer value.
     
    As an early Christmas gift, treat yourself to a copy of "The C Programming Language" (the "K&R" book). It's a bit old, but still one of the best tutorials for the language.


    Thanks. I found the 2nd edition on line in PDF and will read thourgh

    “Remember what may seem elementary to you may be very complex to someone else.”
    #19
    NorthGuy
    Super Member
    • Total Posts : 4600
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: Use of a pointer 2017/11/22 09:46:46 (permalink)
    +3 (3)
    You define a type:
     
    typedef struct {
      int a;
      int b;
    } my_t, *pmy_t;

     
    This have created two types for you to use - my_t which is a structure and pmy_t which is a pointer to this structure.
     

    my_t my_v;

     
    This creates a variable. Some amount of memory is reserved. You can now use your variable by writing or reading data from this memory, such as.
     

    my_v.a = 1;
    mu_v.b = my_v.a + 500;

     
    The pointer:
     

    pmy_t pmy_v = &my_v;

     
    Now you have reserved another memeory chunck for a pointer variable. All it can do is to point to a variable of type my_t. You also initialize it so that the address of my_v is now stored inside your pointer variable. Now you can access the variable it points to:
     

    pmy_v->a = 1;
    pmu_v->b = pmy_v->a + 500;

     
    It does the same as above, but note that dots have changed to arrows.
     
    Or, you can point your pointer to another variables:
     

    my_t my_vx, my_vx;
    pmy_t pmy_v;
     
    my_vx.a = 1;
    my_vy.a = 7;
     
    pmy_v = &my_vx; // now it points to my_vx
     
    printf("%d\n",pmy_v->a); // this prints "1"
     
    pmy_v = &my_vy; // now it points to my_vy
     
    printf("%d\n",pmy_v->a); // this prints "7" despite this is exact the same line as above.

     
    Makes sense?
     
     
     
    #20
    Page: 123 > Showing page 1 of 3
    Jump to:
    © 2017 APG vNext Commercial Version 4.5