String to char* conversion

Hi all,
I am trying to convert a string to char*. I use the Serial.println function to examine the values inside the pointer and the first character is unknown (as in inverted ?). The serial monitor shows temp: 1010, lookUpvar:?010. Anyone knows why?

void loop(){
   String temp = "1010";
   char* lookup_var;

    Serial.print("temp:");Serial.println (temp);
    strcpy(lookup_var, temp.c_str());
    Serial.print("lookUpvar:");Serial.println (lookup_var);
}

The line:

char* lookup_var;

Only creates a variable which is a pointer to a character. It does not create an array of characters.
In other words, no space has been allocated for a string.
for the strcpy() called later to produce a valid C string, you need to use a pointer to an array of characters.
try declaring lookup_var as a character array, sized to be one character larger than the largest string you need to work with. eg:

char[5] lookup_var;

Make that mod to your code and try again.

Said another way, lookup_var is pointing at an arbitrary address and your call to strcpy() is shoving data in that memory location as if you own it. The results will be unpredictable.

Thank you so much guys! Got it to work, but I have another question.

Sorry, I have another question. Although I got the serial monitor to show that lookup_var holds 1010. I added an extra line after that to make sure that it is what I want. The following serial.print did not execute. Anyone knows why?

 strcpy(lookup_var, temp.c_str());
 Serial.print("lookUpvar:");Serial.println (lookup_var);
 if (lookup_var == "1010")
      Serial.println("CORRECT!");

lookup_var is now a char array, you cant use == "" to compare, try strcmp().

lookup_var is not a c-string so you can't compare using ==

Use strcmp instead.

Thank you!

23.11.3.17 int strcmp ( const char * s1, const char * s2 )
Compare two strings.
The strcmp() function compares the two strings s1 and s2.
Returns
The strcmp() function returns an integer less than, equal to, or greater than zero if s1 is found, respectively,
to be less than, to match, or be greater than s2. A consequence of the ordering used by strcmp() is that if s1
is an initial substring of s2, then s1 is considered to be "less than" s2.

Did you read the link that I provided? strcmp() returns 0 on a match.

Return Value
The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.

I've added the emphasis.

I see that DKWatson did beat me again :wink:

Yeah sorry, I thought it'd return 1. Thank you again everyone!

junpun95:
Yeah sorry, I thought it'd return 1. Thank you again everyone!

Don't think; do a little research (the reason why I posted the link).

We try to give you pointers so that you sort it out on your own, that way you remember how it was resolved. If you just got canned answers, you'd be asking the same question a month from now.

junpun95:
Thank you so much guys! Got it to work, but I have another question.

We would like to see your codes that have turned the pointer variable lookup_var to point the base address of a character array (say, char s1[4]) into which the source data (temp.c_str()) would be copied.

darrob:
The line:

char* lookup_var;

Only creates a variable which is a pointer to a character.

Is the pointer not pointing the base address of a character type array?

In the following prototype of the itoa() function, we observe the reflection of the above sentence.

char   *_CType itoa(int __value, char *__string, int __radix);

What does the function do? Assume that the argument-3 (radix = base) is 10; the itoa() function transforms the value of argument-1 into decimal digits (the decimal number); converts the digits of the decimal number into their respective ASCII code; saves the ASCII codes in a character type array pointed by argument-2; places a null-character/null-byte (‘\0’/0x00) as the last element of the array. The function will also return a numerical value (known as pointer) which is the base address of the array being pointed by argument-2.

lookup_var is not a c-string so you can't compare using ==

The second part of that statement is true. The first part is NOT. lookup_var IS a string - a NULL terminated array of chars (or a pointer to the same thing, although presumably the way OP fixed the problem was to actually use an array).

GolamMostafa:
Is the pointer not pointing the base address of a character type array?

It CAN point to the base address of a character array. But, like any other uninitialized local variable, its value on entry to the loop() function is indeterminate. It's what ever happens to be in those memory locations (on the stack). You probably don't own the memory it's pointing to. The strcpy() function will gladly allow you to crap all over that memory.

Is the pointer not pointing the base address of a character type array?

If the pointer is properly initialized, that is one possible place where it can point. But, pointers can be incremented, to point to the second element, the third element, or 100 elements beyond the end of the array.

You can not assume that a pointer points to anywhere logical UNLESS you explicitly initialize it.

Even if you do, you can not assume, at some other point in the code, that the pointer points to the 0th element of an array. ALL that you can assume is that the pointer currently points to where the programmer wants to operate on some data that the pointer points to.

The prototype of strcpy() function is --

char * _CType strcpy(char *__dest, const char *__src);

The function demands that the 1st argument must be a pointer variable which will point to the base address of a character type array into which the source data characters (coming from another character type array being pointed by a pointer variable) would be copied.

To comply with the syntax of the strcpy() function, we have to give the following fashion to the sketch of the OP:

void setup()
{
  Serial.begin(9600);
  String temp = "1010";
  char *lookup_var;
  char s1[4];
  int x = &s1;
  lookup_var = x;

  Serial.print("temp:");
  Serial.println (temp);
  strcpy(lookup_var, temp.c_str());
  Serial.print("lookUpvar:");
  Serial.println (lookup_var);
}

void loop()
{

}

Now, my question is:

Is it syntactically correct to write strcpy(s1, temp.c_str(); other than strcpy(lookup_var, temp.c_str(); when both forms give the same result? (s1 is a not a pointer variable though it could be thought like that.)

GolamMostafa:

void setup()

{
 Serial.begin(9600);
 String temp = "1010";
 char *lookup_var;
 char s1[4];
 int x = &s1;
 lookup_var = x;

Serial.print("temp:");
 Serial.println (temp);
 strcpy(lookup_var, temp.c_str());
 Serial.print("lookUpvar:");
 Serial.println (lookup_var);
}

void loop()
{}

That code is nonsense. What are you hoping to accomplish by (incorrectly) passing an address though ‘int x’? When you use the name of an array in a function call, the compiler treats it as a correctly-typed pointer to the first element in that array. It has been that way since K met R.