Go Down

Topic: Can a pointer be null and if so can it be tested for? (Read 6538 times) previous topic - next topic

Dyslexicbloke

I have just written a function that returns a pointer.
After much mucking about and a great deal of advice it is doing what I need, well almost, and as is the way with these things I am left wondering what took me so long.

That aside ...
The function is indexing through an array of structs looking for a text string. when it find it it returns a pointer to a variable that had that name at design time.

What I want to do is have it return something I can test for in the event that no match is found.
Can a pointer be set null and then tested for?

The only way I can think of doing my check right now is to have a default variable with some distinctive value and set the pointer to that but that is its hardly slick.

Any thought will be appreciated.
If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

Coding Badly

Can a pointer be set null...


Yes.

[font=Courier New]  MyPtr = NULL;[/font]

Quote
...and then tested for?


Yes.

[font=Courier New]  if ( MyPtr == NULL )
 {
   // Do your thing.
 }[/font]


The other possibility is to use "pass by reference"...

Code: [Select]
boolean GetThePointer( int* & MyPtr )
{
 if ( NameIsFound )
 {
   MyPtr = PointerToStruct;
   return( true );
 }
 else
 {
   MyPtr = NULL;
   return( false );
 }
}

void loop( void )
{
 int* MyPtr;

 if ( GetThePointer( MyPtr ) )
 {
   // Do your thing.
 }
}

rockwallaby

Hi Al,
Quote
I am left wondering what took me so long

Yes, I have this much of the time, and after a pause, I realise it is because I was learning.
Learning takes time, and with much learning my head hurts  :~

Now you mention
Quote
What I want to do is have it return something I can test for in the event that no match is found.

How about the idea that your function, which I know has a loop in it, knows the size of your array and so if you get to the end of checking the array you haven't found a match, then return something letting the calling routine know that is the case.

Paul - VK7KPA

PaulS

Quote
Yes.

if ( MyPtr == NULL )
  {
    // Do your thing.
  }

Or, more usually,
Code: [Select]
if(MyPtr) // NOT equal NULL
{
   // dereference the pointer safely
}

Coding Badly


PaulS

Quote
Old habit.

No problem. Usually, I prefer explicit comparisons (myPtr != NULL), except in the case of pointer (myPtr). I'm not sure why.

Dyslexicbloke

Thanks folks.... much appreciated.

I am sorry to be asking such basic questions ... its mostly due to a lack of basic knowledge.
C makes VB look like cat in the hat!

One the one hand ...
I am struggling with basic constructs, syntax and in the case of strings the slightest clue about haw to deal with them effectively, beginners stuff.
And on the other ...
Conceptually I am thinking OO and treating my minor network nodes as out of process threads.

It is a frustrating and unsettling situation  :smiley-red:
If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

rockwallaby

You're doing great, really.
Some of the things you are working with I haven't yet.

I know when I start to feel frustrated it can be very unsettling and I often wonder where my direction is.
What I have learnt to do better is to go easy on myself, to let things slide a little for a while, like today.
I haven't done any programming today, instead went for a spin on the motorcycle, set my eyes on something different from the screen for part of the day.
And I start to feel a little refreshed.
Now with glass of red in hand, I am not worried about the things I had thoughts about earlier that I should be doing.

If you need to take a break do so,
When your ready, let's come back to look at the core of what you are wanting to achieve and try to best as possible see how that might be possible without too much hurty head.
My suggestion is to stay with the model of a single master node, that's where you can better handle the tasks at this stage I think.
Then as the project grows, start to include the remote nodes as they are needed.
You can use your Mega, it has lots of I/O to play with, stick with a simple single system first would be my suggestion.
That is what I am doing with my project, which maybe others out there don't know, shares a lot in common with your project, remote renewable power monitoring and control system.

Paul
Paul - VK7KPA

Coding Badly


Bear in mind that no one is born knowing C++.  Like any other learned skilled, it takes patience, practice, and perseverance.

To add to the difficulty, the language is grossly overloaded and bears little resemblance to human languages.

graynomad

Quote
remote renewable power monitoring and control system.

I'm working on a system of connected Arduinos as well (heck who isn't :)) for such monitoring. Using RS-485, plug-in IO modules and dedicated chips for the network code. Just about to start laying out the PCBs.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

rockwallaby

Ah, just the person who might be able to help me,
Rob, I have just bought some cute little ASC712 current sensing chips, they're 8 legged SMD, so their small ok.
It's been years since I played with PCB programs, I think protel was the program I used to use.
But, I use Linux and OSX these days.

Any advice regarding software or anything.

Paul
Paul - VK7KPA

Dyslexicbloke

Good advice I think ...
I spent yesterday at one of the mills, there was a flood came through last week and we had to lift a huge log out of the wheel.

We also designed a continuous belt cleaning system to replace some of the fixed screens because the output has been down lately.
With all the rain here the river is very dirty and it autumn here so leaves are a huge issue right now.

The turbine, the mill has both, was ding about 7.5kW yesterday with clean screens, but it dropped to less than 4 in two hours!!
I would have to live there to keep the screens clear hence the new design.

New chain required on the wheel at the weekend, 'O' joy, 20' of 1'' simplex is heavy, I will need blocks to pull it on.
Not a thought of coding all day. :D

Rob ...
I think I have worked out how to add an RTS line to the UART based serial if your interested.
If you go that way you can use hardware serial and convert, properly, to RS585.

There is an interrupt available that fires when the last bit transmitted is done, as oppose to when the buffer is empty.
If the hardware serial Lib was modified slightly so it set a pin high when the buffer was first populated and then low gain on transmit complete you would have the enable signal required by most RS485 chips.
I think this approach would be faster, less processor load, than building/finding a filly soft RS485 solution using other pins.

That said there are 485 shields available .... Down to cost and complexity I guess.
Al
If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

graynomad

Quote
I think I have worked out how to add an RTS line to the UART based serial if your interested.

My links are full duplex with two UARTs so I don't need to turn the line around but this is often a gotcha for people who think that the last byte has gone when it's really still in the chamber.

Quote
There is an interrupt available that fires when the last bit transmitted is done,

The "USART, Tx Complete" interrupt. I think there could be a mod to the hardware serial code, maybe you should create an RS485 library.

Quote
That said there are 485 shields available

Do they handle the TE signal as you have mentioned though? I've never looked at them.

What I'm doing is a custom board with the RS485 transceivers built in so I don't need a shield.

Quote
they're 8 legged SMD

SOIC8 IIRC, pretty big by SMD standards :)

Quote
I think protel was the program I used to use.

I cut my teeth on the original DOS-based Protel, we used to get pre-release versions from the guys in Tassie.

Most people use Eagle these days, the UI is total crap but at least there's plenty of people around to help. I don't know if it works on Linux or OSX though.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Dyslexicbloke

All the 485 transceivers I have looked at need a TE. High when data is valid and low when it inst.

There are a couple of 555 circuits out there which toggle TE but they all have issues, of one form or another.
If you are going to implement something which is 'only' a hardware standard you might as well do it right  :D

Library ... I fancy a half duplex bus that will work Master-Master, using dominant byte collision handling like CanBus but with 485 electrical resilience.
Al
If I knew where the box was I would probably still want to think outside it!

Feel free to be blunt ... Its how I learn.

graynomad

Quote
There are a couple of 555 circuits out there which toggle TE but they all have issues, of one form or another.

The MAX13414 and MAX13487E-MAX13488E have auto direction control.

However you would normally still need to know when the last byte has gone if you are writing a protocol and the auto turnaround doesn't help with that.

Quote
using dominant byte collision handling like CanBus but with 485 electrical resilience.

Easy if you wire the transceivers in a non-standard way so the 1 level is hi-z and use fail safe biasing. But you won't get full speed.

Quote
using dominant byte collision

If you use byte collision detection it's destructive and you will lose all data being transmitted. Bit level detection can be non-destructive, but that's a lot harder to code and will probably stop the processor from doing anything else when it's transmitting.

It's mostly for these reasons I'm using a ring topology with a dedicated processor for the network. Also I think it's fair to say that a ring is more robust than a net, at the expense of more wires.


EDIT: I think I just thought of a way to make byte-level detection non-destructive, as long as you only have 8 nodes or priority levels.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Go Up