Skip to main content
Arduino

Return to Answer

Example:

void foo (char * s)
 {
 Serial.println (s);
 }
 
void setup ()
 {
 Serial.begin (115200);
 Serial.println ();
 foo ("bar");
 } // end of setup
void loop ()
 {
 } // end of loop

Warning:

sketch_jul14b.ino: In function ‘void setup()’:
sketch_jul14b.ino:10: warning: deprecated conversion from string constant to ‘char*’

The function foo expects a char* (which it can therefore modify) but you are passing a string literal, which should not be modified.

The compiler is warning you not to do this. Being deprecated it might turn from a warning into an error in a future compiler version.


Solution: Make foo take a const char *:

void foo (const char * s)
 {
 Serial.println (s);
 }

I don't get it. Do you mean can not be modified?

Older versions of C (and C++) let you write code like my example above. You could make a function (like foo) that prints something you pass down to it, and then pass down a literal string (eg. foo ("Hi there!");)

However a function that takes char * as an argument is allowed to modify its argument (ie. modify fooHi there! in this case).

You might have written, for example:

void foo (char * s)
 {
 Serial.println (s);
 strcpy (s, "Goodbye");
 }

Unfortunately, by passing down a literal, you have now potentially modified that literal so that "Hi there!" is now "Goodbye" which is not good. In fact if you copied in a longer string, you might overwrite other variables. Or, on some implementations you would get an access violation because "Hi there!" might have been put in read-only (protected) RAM.

So the compiler-writers are gradually deprecating this usage, so that functions to which you pass down a literal, must declare that argument as const.

Example:

void foo (char * s)
 {
 Serial.println (s);
 }
 
void setup ()
 {
 Serial.begin (115200);
 Serial.println ();
 foo ("bar");
 } // end of setup
void loop ()
 {
 } // end of loop

Warning:

sketch_jul14b.ino: In function ‘void setup()’:
sketch_jul14b.ino:10: warning: deprecated conversion from string constant to ‘char*’

The function foo expects a char* (which it can therefore modify) but you are passing a string literal, which should not be modified.

The compiler is warning you not to do this. Being deprecated it might turn from a warning into an error in a future compiler version.


Solution: Make foo take a const char *:

void foo (const char * s)
 {
 Serial.println (s);
 }

I don't get it. Do you mean can not be modified?

Older versions of C (and C++) let you write code like my example above. You could make a function (like foo) that prints something you pass down to it, and then pass down a literal string (eg. foo ("Hi there!");)

However a function that takes char * as an argument is allowed to modify its argument (ie. modify foo in this case).

You might have written, for example:

void foo (char * s)
 {
 Serial.println (s);
 strcpy (s, "Goodbye");
 }

Unfortunately, by passing down a literal, you have now potentially modified that literal so that "Hi there!" is now "Goodbye" which is not good. In fact if you copied in a longer string, you might overwrite other variables. Or, on some implementations you would get an access violation because "Hi there!" might have been put in read-only (protected) RAM.

So the compiler-writers are gradually deprecating this usage, so that functions to which you pass down a literal, must declare that argument as const.

Example:

void foo (char * s)
 {
 Serial.println (s);
 }
 
void setup ()
 {
 Serial.begin (115200);
 Serial.println ();
 foo ("bar");
 } // end of setup
void loop ()
 {
 } // end of loop

Warning:

sketch_jul14b.ino: In function ‘void setup()’:
sketch_jul14b.ino:10: warning: deprecated conversion from string constant to ‘char*’

The function foo expects a char* (which it can therefore modify) but you are passing a string literal, which should not be modified.

The compiler is warning you not to do this. Being deprecated it might turn from a warning into an error in a future compiler version.


Solution: Make foo take a const char *:

void foo (const char * s)
 {
 Serial.println (s);
 }

I don't get it. Do you mean can not be modified?

Older versions of C (and C++) let you write code like my example above. You could make a function (like foo) that prints something you pass down to it, and then pass down a literal string (eg. foo ("Hi there!");)

However a function that takes char * as an argument is allowed to modify its argument (ie. modify Hi there! in this case).

You might have written, for example:

void foo (char * s)
 {
 Serial.println (s);
 strcpy (s, "Goodbye");
 }

Unfortunately, by passing down a literal, you have now potentially modified that literal so that "Hi there!" is now "Goodbye" which is not good. In fact if you copied in a longer string, you might overwrite other variables. Or, on some implementations you would get an access violation because "Hi there!" might have been put in read-only (protected) RAM.

So the compiler-writers are gradually deprecating this usage, so that functions to which you pass down a literal, must declare that argument as const.

Added more explanations.
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 69
  • 125

Example:

void foo (char * s)
 {
 Serial.println (s);
 }
 
void setup ()
 {
 Serial.begin (115200);
 Serial.println ();
 foo ("bar");
 } // end of setup
void loop ()
 {
 } // end of loop

Warning:

sketch_jul14b.ino: In function ‘void setup()’:
sketch_jul14b.ino:10: warning: deprecated conversion from string constant to ‘char*’

The function foo expects a char* (which it can therefore modify) but you are passing a string literal, which should not be modified.

The compiler is warning you not to do this. Being deprecated it might turn from a warning into an error in a future compiler version.


Solution: Make foo take a const char *:

void foo (const char * s)
 {
 Serial.println (s);
 }

I don't get it. Do you mean can not be modified?

Older versions of C (and C++) let you write code like my example above. You could make a function (like foo) that prints something you pass down to it, and then pass down a literal string (eg. foo ("Hi there!");)

However a function that takes char * as an argument is allowed to modify its argument (ie. modify foo in this case).

You might have written, for example:

void foo (char * s)
 {
 Serial.println (s);
 strcpy (s, "Goodbye");
 }

Unfortunately, by passing down a literal, you have now potentially modified that literal so that "Hi there!" is now "Goodbye" which is not good. In fact if you copied in a longer string, you might overwrite other variables. Or, on some implementations you would get an access violation because "Hi there!" might have been put in read-only (protected) RAM.

So the compiler-writers are gradually deprecating this usage, so that functions to which you pass down a literal, must declare that argument as const.

Example:

void foo (char * s)
 {
 Serial.println (s);
 }
 
void setup ()
 {
 Serial.begin (115200);
 Serial.println ();
 foo ("bar");
 } // end of setup
void loop ()
 {
 } // end of loop

Warning:

sketch_jul14b.ino: In function ‘void setup()’:
sketch_jul14b.ino:10: warning: deprecated conversion from string constant to ‘char*’

The function foo expects a char* (which it can therefore modify) but you are passing a string literal, which should not be modified.

The compiler is warning you not to do this. Being deprecated it might turn from a warning into an error in a future compiler version.


Solution: Make foo take a const char *:

void foo (const char * s)
 {
 Serial.println (s);
 }

Example:

void foo (char * s)
 {
 Serial.println (s);
 }
 
void setup ()
 {
 Serial.begin (115200);
 Serial.println ();
 foo ("bar");
 } // end of setup
void loop ()
 {
 } // end of loop

Warning:

sketch_jul14b.ino: In function ‘void setup()’:
sketch_jul14b.ino:10: warning: deprecated conversion from string constant to ‘char*’

The function foo expects a char* (which it can therefore modify) but you are passing a string literal, which should not be modified.

The compiler is warning you not to do this. Being deprecated it might turn from a warning into an error in a future compiler version.


Solution: Make foo take a const char *:

void foo (const char * s)
 {
 Serial.println (s);
 }

I don't get it. Do you mean can not be modified?

Older versions of C (and C++) let you write code like my example above. You could make a function (like foo) that prints something you pass down to it, and then pass down a literal string (eg. foo ("Hi there!");)

However a function that takes char * as an argument is allowed to modify its argument (ie. modify foo in this case).

You might have written, for example:

void foo (char * s)
 {
 Serial.println (s);
 strcpy (s, "Goodbye");
 }

Unfortunately, by passing down a literal, you have now potentially modified that literal so that "Hi there!" is now "Goodbye" which is not good. In fact if you copied in a longer string, you might overwrite other variables. Or, on some implementations you would get an access violation because "Hi there!" might have been put in read-only (protected) RAM.

So the compiler-writers are gradually deprecating this usage, so that functions to which you pass down a literal, must declare that argument as const.

Source Link
Nick Gammon
  • 38.9k
  • 13
  • 69
  • 125

Example:

void foo (char * s)
 {
 Serial.println (s);
 }
 
void setup ()
 {
 Serial.begin (115200);
 Serial.println ();
 foo ("bar");
 } // end of setup
void loop ()
 {
 } // end of loop

Warning:

sketch_jul14b.ino: In function ‘void setup()’:
sketch_jul14b.ino:10: warning: deprecated conversion from string constant to ‘char*’

The function foo expects a char* (which it can therefore modify) but you are passing a string literal, which should not be modified.

The compiler is warning you not to do this. Being deprecated it might turn from a warning into an error in a future compiler version.


Solution: Make foo take a const char *:

void foo (const char * s)
 {
 Serial.println (s);
 }
lang-c

AltStyle によって変換されたページ (->オリジナル) /