1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

C Programming: pointers

Discussion in '8051/8951' started by CroneA, Nov 5, 2013.

  1. CroneA

    CroneA New Member

    Joined:
    Oct 23, 2013
    Messages:
    19
    Likes:
    0
    Is there a difference between this code:

    U8 xdata *pUART_buffer_val;

    ... and this code?
    U8* xdata pUART_buffer_val;

    Thanks!
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Do they both compile without warning..

    xdata is a modifier, so I'm not sure if you can do both.. You probably can..
    The compilers parser will see the modifier and the linker will allocate the character pointer accordingly.

    As it isn't ANSI C then without the documentation, I don't know...
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    I've just consulted the SDCC manual.... Its slightly different but the xdata modifier can be used

    generic pointer physically located in xdata space..
    unsigned char*__xdata p;

    But if the pointer isn't located in xdata

    pointer physically in internal ram pointing to object in external ram..
    __xdata unsigned char* __data p;
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland

    I don't know either, for the same reasons Ian explained. But, I can give you an example in ANSI C:

    This code:
    char* orange, apple; // Here "orange" is a pointer to char, but "apple" is a char.

    And this:
    char* orange, *apple; // Here both variables "orange" and "apple" are pointers to char.
     
  6. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Nice, but those do not quite match with the original "code".. I'm not saying that my previous post helped anything, but .. eh. I don't know.

    I mean.. what is the "__data" in the last one? That does not seem to fit.

    Edit: I bail out.. trying to get involved in things I have no idea :)
     
    Last edited: Nov 5, 2013
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    I know, I don't know either... All I know is with data modifiers on the pic... The modifier can be place anywhere in the declaration.... The only two "close" declarations in SDCC were those two...
     
  8. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Fast google tells me that "xdata" is a 16bit address base. It tells the compiler to use 16bit pointers. Or something that nature..
    Found this link:
    http://www.keil.com/support/man/docs/c51/c51_le_memspecificptrs.htm

    Is that helpful/related at all? (To me it looks like it explains it, but now my head is spinning too much to read it through)
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Kinda!! There is a command on the 8051 to use external memory, data memory or code memory... All these do (as you so rightly say ) is include the High address register if need be...

    mov
    movc
    movx

    All the above can use the dptr register..
     
  10. CroneA

    CroneA New Member

    Joined:
    Oct 23, 2013
    Messages:
    19
    Likes:
    0
    Hmmm I think I will just try to be careful to declare all pointers the same way, just in case :p
     
  11. WTP Pepper

    WTP Pepper Active Member

    Joined:
    Jan 24, 2012
    Messages:
    648
    Likes:
    41
    Location:
    UK
    From experience...excessive use of pointers is a way to tell one programmer you are better than the next....
     
    • Like Like x 1
  12. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    I agree. C also has arrays which are much easier for the average joe to follow and as I understand it should generate the same machine code.
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    You think they are showing prowess??

    I don't think that!! All you need to do is look at ASM code written by a professional against code written by a novice.
    The advanced coder will code using indirect access, for speed and agility... Pointer make access simpler once you know how to use them. Pointers are, after all, using indirect addressing mode...
     
  14. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    Good programmers do not like their code being looked at :)
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Damn!! That's where I'm going wrong...

    Ah! well I must be a bad programmer.:joyful:
     
  16. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    Exceptions prove the rules ... Don't they?
     
  17. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    It is not often I disagree with you but this is major! LOL

    One of the advantages of using a compiler is that, if it is worth its salt, it will optimize the code. Generally the more straight forward the source the better job the optimizer can do on it.

    Anyway there is no point in using pointers in c where you can use an array. Pun intended.
     
  18. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Pointers are for other things as well as arrays... Optimization isn't the issue here..

    Lets move to structures, unions and indeed functions themselves... I find it much easier working on array's and structures using pointers...
    I know its personal preference, It's not a " I can code you can't" kinda thing. Its how I learned to code in the first place.

    I did most of my C code on a PC.... Not just on a PC..On MSDOS using DOS4GW extender... You have to use pointers as that's how the library is set up. Most libraries require pointers, take the string library, every function uses pointers!!

    Unfortunately They are used and that's that.
     
  19. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    For example, if you have a variable and a routine that needs to modify the variable, instead of passing the variable to the routine, you pass the pointer to the variable. I have absolutely no idea how you can do this simple thing without pointers.
     
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,310
    Likes:
    914
    Location:
    Rochdale UK
    Some people just make every variable "global".. This is the case with small ram deficient micro's. I entirely agree that, in these situations, pointers are not required as you have no stack to worry about... Having said that, if you are used to using them, what does it matter if you do use them..

    look at these two functions..

    using a pointer..
    Code (c):

    void LCDprint(const char * str)
       {
       while(*str != 0)
          LCDdata(*str++);
       }
     
    Using a global buffer...
    Code (c):

    void LCDprint(void)
       {
       char x = 0;
       while(LCDbuf[x] != 0)
          LCDdata(LCDbuf[x++]);
       }
     
    In the first example you can just call the function...
    LCDprint("Hello world!!");
    In the second I would need to fill the buffer first.

    The only thing that causes upset is if the pointer is pointing to RAM and the function expects it to point to code!!
     
  21. NorthGuy

    NorthGuy Well-Known Member

    Joined:
    Sep 8, 2013
    Messages:
    1,218
    Likes:
    206
    Location:
    Northern Canada
    How about functions like this:

    Code (c):
    void process_string_before_displaying(char * str) {
       // complicated conversion here
    }
    If not for the pointer, you would have to write a separate routine for each variable.
     

Share This Page