Continue to Site

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.

  • 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.

C Programming: pointers

Status
Not open for further replies.

CroneA

New Member
Is there a difference between this code:

U8 xdata *pUART_buffer_val;

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

Thanks!
 
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...
 
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;
 
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.
 
generic pointer physically located in xdata space..
unsigned char*__xdata p;

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

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:
Nice, but those do not quite match with the original "code"..

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...
 
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:
https://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)
 
Fast google tells me that "xdata" is a 16bit address base. It tells the compiler to use 16bit pointers. Or something that nature..
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..
 
From experience...excessive use of pointers is a way to tell one programmer you are better than the next....
 
  • Like
Reactions: 3v0
From experience...excessive use of pointers is a way to tell one programmer you are better than the next....

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...
 
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...
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.
 
3V0 said:
Anyway there is no point in using pointers in c where you can use an array. Pun intended.

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.
 
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.
 
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.
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..
C:
void LCDprint(const char * str)
   {
   while(*str != 0)
      LCDdata(*str++);
   }
Using a global buffer...
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!!
 
Some people just make every variable "global".

How about functions like this:

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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top