Char and Char*

Hi, I have a question, Im new in the Arduino and I decided to learn about this with so much free time, but I stumbled upon something...

I have no idea what the difference is between char and char *, search the internet and the way they explain it is confusing to me. If it is not too much trouble, could you explain these two variables and some examples? :slight_smile:

I would be very grateful!!!

We need to understand the difference between char and char (char) by examples:

char myData1[5];
char *myData2;

Google again to understand the difference between the above two instructions.

On your arduino a char is a data type that takes up 1 byte of memory and that stores a character value. So it is a small chunk of memory that will be reserved to store a number.

The standard does not specify if a plain char is signed or unsigned so technically it could be only positive (0 to 255) or signed (-128 to 127) in one byte but on arduino char are signed, so would be -128 to 127

a char* is a type. The * states it's a pointer and char defines the type of the pointed data. So your char* is the memory address that has been reserved to store your number.

char c = 'A'; // one byte reserved in memory to hold the ASCII code of the letter A
char* cPtr = &c; // the address in memory of the variable c (a pointer to c)

Is there any difference between the following two kinds of styles of declarations?

char* cPtr;
char *cPtr;

If not, then in both cases -- cPtr is a pointer variable. But what I have learnt is this: when the symbol *(asterisk) is placed just before a variable, the variable becomes a pointer variable and refers to a byte (in this case it is char = signed 8-bit) oriented/accessible memory space whose size is unspecified.

GolamMostafa:
Is there any difference between the following two kinds of styles of declarations?

char* cPtr;

char *cPtr;




If not, then in both cases -- cPtr is a pointer variable. But what I have learnt is this: when the symbol *(asterisk) is placed just before a variable, the variable becomes a pointer variable and refers to a byte (in this case it is char = signed 8-bit) oriented/accessible memory space whose size is unspecified.

You're talking rubbish. There's no difference as to where the * is placed.

All of the following are syntactically equivalent:

char* cPtr1;
char*cPtr2;
char *cPtr3;
char * cPtr4;

pozole:
Hi, I have a question, Im new in the Arduino and I decided to learn about this with so much free time, but I stumbled upon something...

I have no idea what the difference is between char and char *, search the internet and the way they explain it is confusing to me. If it is not too much trouble, could you explain these two variables and some examples? :slight_smile:

I would be very grateful!!!

A variable of type "char *" is a pointer. That means it holds the memory address of some other variable or array element or parameter or whatever, and in that address is a char.

The only things you can do with a pointer is dereference it, index off it, or add/subtract an offset index, as in *p, p[j], p+j

*(p+i) is exactly the same as p[j], it means add j as an index to the pointer, then access the memory location there.

In C and C++ there is no checking of any sort for indexing operations such as p[j], *(p+j), so you can accidentally refer to the wrong bit of memory if you have a bug, perhaps overwriting something unexpected.
This is why they are regarded as low-level languages, you can shoot yourself in the foot metaphorically.

There is a special value that any pointer variable can have, NULL, which means nothing is pointed to. Dereferencing NULL is another way you can shoot yourself in the foot - the results of doing so are undefined
in the language.

The key things to take from this: pointers are potentially dangerous, and arrays are implemented as pointers.

char * itself is the type used for c-strings, with the added rule that the end of the string is indicated by a char with
a zero value.

"ab" [ 0 ] returns 'a'
"ab" [1] returns 'b'
"ab" [2] returns 0
"ab" [3] is undefined.

When the compiler sees a string constant like "ab", it allocates a char array large enough for the string and the zero char at the end, and returns a pointer to the start of it as the value used in the code.

1 Like

pcbbc:
You're talking rubbish. There's no difference as to where the * is placed.

All of the following are syntactically equivalent:

char* cPtr1;

char*cPtr2;
char *cPtr3;
char * cPtr4;

To a compiler and not to a human being who never makes a declaration like either of the following:

byte                  *                       x;
byte*x;

GolamMostafa:
Is there any difference between the following two kinds of styles of declarations?

char* cPtr;

char *cPtr;

there are no differences

When you want to declare a variable, the syntax is

type variableName;

So it's more a best practice / convention to attach the * to the type. This way you clearly show your intent and that you understand this is the type of the variable you are declaring.

But - there is a drawback to this notation though and it's not that simple: If you writechar* a, b;only a is actually a pointer, b is a char.... that's why some people argue you should have the star attached to the variable name. And my personal preference would be to declare in two lines so that you can have nice comments :slight_smile:

char* a; // some explanation of what is this pointer
char* b; // some other explanation

but It's a matter of personal preference and such discussion quickly turn to a holy war just like brace style

1 Like

Except when you want to declare two pointers:

char *x, *y;

wildbill:
Except when you want to declare two pointers:

char *x, *y;

Yes I was editing my answer as you typed. that's part of the "holy war" fun :slight_smile: — which I've no intention to start here. peace in the world please!

1 Like

Be careful where you place the '*' though.
You might be tempted to define two pointers like this

char* thisIsaPointer, butThisIsNot;

Edit: beaten to it

I've always grouped it this way:

char *x, y;

So, I think to myself -- "if I dereference x, I get a char" and "y is a char"
YMMV