Multipart question on casting

After being confused, I decided to do some experimentation.
Problem is I don't know how to evaluate the results.

Essentially I am trying to combine 2 variables, one will have a set value and the other will be dynamic.
At the end of the day I want to transmit the combined variable as an RF message.
Variable 1 (fixed) = "001"
Variable 2 (dynamic) = "A" or "B" or "C".
Variable 3 (combination) = "001A" or "001B" or "001C".

When casting, should I be using : char var1[] = "001";

char sid[] = "001";

void setup()
  {
    // Start Serial
    Serial.begin(9600);	
    while (!Serial) {;}              // wait till port connects
    Serial.println("Serial started."); // test print
  }
  
void loop()
  {
    Serial.println(sid);

    char activity[] = "A";
    Serial.println(activity);

    String report = sid;
    report += activity;
    Serial.println(report);
    delay (10000);
    
//    const char *msg = report;
  }

or should I be using char *var1 = "001";

char *sid = "001";

void setup()
  {
    // Start Serial
    Serial.begin(9600);	
    while (!Serial) {;}              // wait till port connects
    Serial.println("Serial started."); // test print
  }
  
void loop()
  {
    Serial.println(sid);

    char *activity = "A";
    Serial.println(activity);

    String report = sid;
    report += activity;
    Serial.println(report);
    delay (10000);
    
//    const char *msg = report;
  }

FWIW both methods compile and can be concatenated into a string, giving me the required combined variable.

However, the next step to create the variable to be transmitted is a problem. When I add the line : const char *msg = report; it does not compile.

I am not sure if the message has to be cast as a const char *, but that what the sample code had.

Is there a method other than concatenating as a string to join the variables?

A layman's explanation of when to use "char var" as opposed to "char *var" would also be appreciated. the google discussions assume a knowledge of C++ which I just don't have.

TIA for any assistance.

@Delta_G : Works like a charm - thanks a bunch.
Yes. I realized what the problem was, I just didn't know how to fix it.

I note u set the length to [5] and not [4]...
FWIW as test I set the value of activity to "ABc", and report came out as "001ABc", so it seems there is no restriction on length, which is good to know.

Keep in mind a string of chars takes up 1 extra char. So the string of "001A" is an array of 5 elements with the last being NULL.

char myString = "001A";
myString[0] => '0'
myString[1] => '0'
myString[2] => '1'
myString[3] => 'A'
myString[4] => NULL (which is 0, which is not the same as '0')

septillion:
Keep in mind a string of chars takes up 1 extra char. So the string of "001A" is an array of 5 elements with the last being NULL.

Delta_G:
Yes, there is a restriction on length. If you get lucky and nothing else is using the memory location right after the char array then you might still see the right thing printed. But this will not always be the case. If something else needs that memory location and you are using it without permission then all sorts of undefined behavior can happen.

You need to make report one character larger than the largest thing you intend to put in there. You need to reserve room for all the characters and one more for the terminating null character. If you want three numbers and three letters then it needs to be 7 elements wide.

Noted. I was aware of the terminating null for a String, I did not realize it applied to arrays a well.
I will remember that :slight_smile:

Noted. I was aware of the terminating null for a String

Strings do not have terminating NULLS. They are instances of a class. A string, on the other hand, DOES, by definition, have a terminating NULL. It is that NULL that distinguishes between an array of chars and a string.

aisc:
Noted. I was aware of the terminating null for a String string, I did not realize it applied to arrays a well.
I will remember that :slight_smile:

String = (the stupid) class
string = NULL terminated c++ string = array of char

Thanks for the explanations/clarifications.

Is there any difference between "char *var" and char var[]"?

Nope :slight_smile:
Yes :stuck_out_tongue: Got confused with (the non declaring) myChar[] and *myChar.

aisc:
Is there any difference between "char *var" and char var[]"?

Yes. One defines a pointer and the other defines an array.

septillion:
Nope :slight_smile:

There most certainly is. Precisely what each does depends on what follows the snippets.

char buffer[80];

defines an array that can hold 80 characters.

char *pBuff;

defines a pointer that points, if (later) initialized, to some location in memory that may, or may not, be writable.

PaulS:
There most certainly is. Precisely what each does depends on what follows the snippets.

char buffer[80];

defines an array that can hold 80 characters.

char *pBuff;

defines a pointer that points, if (later) initialized, to some location in memory that may, or may not, be writable.

I understand the difference between a pointer and an array - from my PHP coding experience.

Are u saying char *pBuff; is in fact a memory address?

FWIW if I declare and initialize var1 as : char *var1 = "abc";
I am able to use var1 (as a variable) with it's assigned value of "abc" - or should I be regarding the value "abc" as a memory address location (i.e. pointer value) and not a variable value?

Still a bit confusing...

Are u saying char *pBuff; is in fact a memory address?

pBuff is assigned to a memory address. The value at that address is an address.

FWIW if I declare and initialize var1 as : char *var1 = "abc";

There is a location in memory where the string literal is stored. The location is stored in the memory location referred to be the name var1.

I am able to use var1 (as a variable)

This is the part of the statement that is nowhere near technically accurate. What do you mean by "use var1"? What do you mean by "as a variable"? There are situations where a function expects to be passed an address. There are others where the function expects to be passed a value. When a function expects a value, it can be told to use the value in the memory location that a pointer points to (rather than using the address as a value).

Understanding the various addressing methods that assembly language supports goes a long way towards understanding pointers in C, but that isn't often covered in a C programming class.

This is the part of the statement that is nowhere near technically accurate.

FWIW it was not so much a statement - but rather formed part of a question.

Maybe I don't speak C very good :slight_smile:

What do you mean by "use var1"?

I mean if I declare x=1 then I can use the variable x.

What do you mean by "as a variable"?

I mean if I declare x=1, then I can use x (the variable) as a variable to represent the value of 1 in any calculation e.g. 2+x=3.

Yes, but if a function like Serial.print() needs to "use" a string variable, it is expecting to be given the address of the string, since you can't actually send the contents of the string as an argument in the function.

char *var1="abc";
Serial.print(var1);

Is this "using a variable" according to your definition?

Yes, that is essentially how I think of a variable.
However, I now know that "char *" is creating a pointer and not a variable.

From your explanation I now understand that since var1 was declared as "char *" when passed to a function, var1 represents the address of where the variable value "abc" is stored.

However, I now know that "char *" is creating a pointer and not a variable.

That is wrong. It IS creating a variable. The type of the variable is a pointer. The value of the variable is an address.

Think of it a bit like this. You have some cards. On one side is a name, like bob or pBob. On the other side is a statement. The statement is either "The value of interest is ______" or "The value of interest is at _________".

The first kind of card is what you are referring to as a variable. The second kind of card is what you are referring to as a pointer. You can see that both cards are variables, though, since the value in the blank can change.

Sorry for my wrong answer. I got confused with the non declaring use.

char myChar[3];
myChar; //pointer to first entry of myChar array
&myChar[0] //also pointer to first entry of myChgar array