Simple Pointers

Hi!

Got a small error when trying to compile the following:

/***** LIBRARIES AND HEADERS *****/
#include <string.h>


void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  char buffer[50];
  char *prt;
  int i;
  int length;

  strcpy(buffer, "When in the course of human events");
  char ptr = buffer;
  length = strlen(buffer);
  Serial.print("The lvalue forptr is: ");
  Serial.print((unsigned int)&ptr);
  Serial.print(" and the rvalue is ");
  Serial.println((unsigned int)ptr);

  while (*ptr)
    {
      Serial.print(*ptr++);
    }

  Serial.flush();  // Make sure all the data is sent...
  exit(0);
}

I’m getting this error: ‘ptr’ was not declared in this scope

So if I humored the compiler, and I modified the line where the error occurred:

prt = buffer;
to
char ptr = buffer;

I get the following error:

pointer_math_1:32:11: error: invalid type argument of unary '*' (have 'char')
   while (*ptr)
           ^~~
pointer_math_1:34:24: error: invalid type argument of unary '*' (have 'char')
       Serial.print(*ptr++);
                        ^~
exit status 1
invalid type argument of unary '*' (have 'char')

Any idea why I’m getting these errors?

prt is not the same as ptr char* ptr = buffer;

You also define prt as a pointer in one place and then define non-pointer one with a similar name at a different place

void loop()
{
  char buffer[50];
  char *prt;                                                       // definition of prt
  int i;
  int length;

  strcpy(buffer, "When in the course of human events");
  char ptr = buffer;                                               // definition of ptr
  length = strlen(buffer);
  Serial.print("The lvalue forptr is: ");
  Serial.print((unsigned int)&ptr);
  Serial.print(" and the rvalue is ");
  Serial.println((unsigned int)ptr);

Which to you really want to use? If this is a typo, that's one issue. If it's not, then why select two variables with such similar names??