Referencing Pointer from another function

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Basically, I need to access the data of a variable from another function but seems I'm having brain freeze at the moment.

Main.c

uchar globalRegister1;

void setUp()
{
  periperhalSetup(&globalRegister1);
}

Peripheral.c

uchar *localRef_gRegister;

void setUp(uchar *globalRegisterRef)
{
  *localRef_gRegister = *globalRegisterRef;

  *(localRef_gRegister) = 0x01;  // Does not change globalRegister1 value in Main.c
  *(globalRegisterRef) = 0x01;  // Changes globalRegister1 value in Main.c
}

Thanks.

Once an engineer, forever an engineer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It seems that you have reduced the situation to a small test program. Post that complete program. Tell which AVR model you are working with. Tell which toolchain and version. Post the generated code, at least for the function in question.

Quote:

*localRef_gRegister = *globalRegisterRef;

Don't you want pointer-to-pointer assignment?

  localRef_gRegister = globalRegisterRef; 

Let's say that globalRegister1 is at address 123. The char at that address is 0x41.
localRef_gregister might be at address 321. Its contents are unknown.
Your statement moves 0x41 to an unknown address.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you sure you gave the variables complicate3d enough names? It's not clear to me what the purpose of localRef_gRegister is supposed to be?

But given that it's a global apparently defined outside a function it will be initialized to 0. So when you execute:

void setUp(uchar *globalRegisterRef)
{
  *localRef_gRegister = *globalRegisterRef;

That is a write to location 0x0000. I'm guessing that's not what you actually wanted?

Either this localRef_gRegister is what you have defined it as - a POINTER to a uchar and you should use it as such, for example you could use:

localRef_gRegister = globalRegisterRef;

which is taking a local (though global) copy of the passed pointer or perhaps it's supposed to be a uchar and not a pointer to uchar? Or maybe it's supposed to be a pointer to a pointer to uchar (uchar **)?

as the name contains the word "Ref" I'm assuming the plan was that it's a pointer? If so the * dereference when writing it IS the error.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  *localRef_gRegister = *globalRegisterRef;

This sets the value of what localRef_gRegister is pointing to to the value of what globalRegisterRef is pointing to. Since you have never set localRef_gRegister to point to anything, you end up clobbering a random memory location.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you all for your replies. Had been debugging the whole day & posting for help is always my last resort. But ... should have done after 2 hours of failed debugging...

theusch wrote:

  localRef_gRegister = globalRegisterRef; 

Anyway, I could have bet without a shadow of a doubt that I had tried this before but now trying it again, worked! Confirmed after checking the values in debugging. I just needed to do a pointer-to-pointer assignment.

Thanks again!

Once an engineer, forever an engineer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The question still stands why you
i) receive the pointer as a parameter, and then
ii) copy the pointer value to a global pointer (that you chose to name "local", which it is not), and finally
iii) use that pointer to alter what both pointers are now pointing to.

Why not just

void setUp(uchar *registerRef)
{
  *registerRef = 0x01;
} 

?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Johan,

Methinks he needs this in "setup()" because some other functionality is later going to mess with that variable now knowing where it is.

I know this is a terrible way to implement it as variables are just going to suddenly change as a result of calling functions that don't apparently I have anything to do with the variable. But I suspect that's what OP is attempting.

At the very least, if there is a "per instance" set of data to be remembered about a device I would keep it all together in a struct and always pass around a struct pointer when dealing with that device. But I guess OP has his own ideas ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

(sigh) that's what happens, at least to me, when I'm on a project that requires cross-platform developments & short of time... Clawson is right. I do need to mess around with that data but I should, at least, construct them in a struct. Will re-look again.

Thanks again.

Once an engineer, forever an engineer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dare I even mention it but in this case ASF is a good model. They have a struct for something like a UART that collects together all it's settings. There are functions to initialise it, functions you pass it to that then init the UART, then other functions dealing with the UART to which you also pass the struct pointer. It holds all the data about that particular "instance". In a sense this is "poor man's C++".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

In a sense this is "poor man's C++".

Yes, mimicking the this-pointer.

(With the ugliness of putting it on display out in the open.. C++ has more decency in this case. :wink:)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]