While hangs on string pointer

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

AVR studio 6 C++ project to print string to uart.

This Hangs at the whie without doing the send
Puts_While("USART Test\r\n");
Puts_IF("USART Test\r\n");

void Puts_While(char *Buffer)
{
while(*Buffer > 0); // Send string
{
USART_SendByte( *Buffer++); // never hit
}
} // loops back to while here

This works..
void Puts_IF(char *Buffer)
{
sloop:
sbyte = *Buffer;
if (sbyte >> 0)
{
USART_SendByte( *Buffer++);
goto sloop;
}
}

char sbyte;
//char Buffer[4];

Thanks
suspect a optimization issue but don't know how to test. :?

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

Delete the semicolon you put after your "while (*Buffer > 0)" statement.

Also, in your Puts_IF() function, your "if (sbyte >> 0)" statement might not be doing what you intend

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

Use code tags when posting code.

Adding to what Levenkay told you, you should consider replacing

while(*Buffer > 0)

with

while(*Buffer)

and

if (sbyte >> 0)

with

if (sbyte)

Also, it is good practice to declare string parameters const when your functions don't change them. E.g. replace

void Puts_IF(char *Buffer)

with

void Puts_IF(const char *Buffer)

This tells the compiler that it is okay to pass a const string to the function, and it also tells anybody reading the source code that the function does not change the string.

Sid

Life... is a state of mind

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

Thanks Sid

I tried most of the options you suggested and a couple others with no change. .
Ill try the const addition as you suggest.

I have other "while" code (Identical sequence) that worked with this same coding that fails here.

Have not deleted other unrelated code from the project yet.. Dropping back to simple minimum code to because I know it does work at times.

The 'If' work around is ok and will do all I need.

I was hoping some one else has seen this.

Mr_Fred

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

But did you remove the semicolon that Levenkay told you about ? If you don't, you're asking for a hang.

Sid

Life... is a state of mind

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

Code tags ??

   Is this it ??

Have not figured out that works..\
Makes easier reading.

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

ChaunceyGardiner wrote:
But did you remove the semicolon that Levenkay told you about ? If you don't, you're asking for a hang.

Thanks guys the semicolon was the problem...

Both routines now work.

Leave to to me to figure out how to confuse a compiler....
I have not seen a code hang like that before.

Great talent out there...

Mr_Fred

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

Those things are hard to spot, we've all been there - repeatedly.

Sid

Life... is a state of mind

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

ChaunceyGardiner wrote:
Those things are hard to spot, we've all been there - repeatedly.
Speak for yourself.
Though I've done other silly things,
that silly thing is one I've managed to avoid.
I don't put my socks over my shoes and I don't put an open
brace immediately after a semicolon on the same line.
For those who do and are of a mind to automate such things,
the sequence semicolon whitespace openbrace on the same line,
might be worth putting in as a no-no for one's syntax checker.

Moderation in all things. -- ancient proverb

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

You're missing the point, Michael. I don't do that myself because I don't ever use a semicolon to terminate conditionals or loops. But I have repeatedly overlooked that mistake when others have posted problem code. I see many of the other experienced developers missing those every now and then, too.

Sid

Life... is a state of mind