• Português (Brasil)
  • IoT Cloud API

The Arduino Reference text is licensed under a Creative Commons Attribution-Share Alike 3.0 License .

Find anything that can be improved? Suggest corrections and new documentation via GitHub.

Doubts on how to use Github? Learn everything you need to know in this tutorial .

Last Revision: Searching...

Last Build: 2024/08/17

Description

A data type used to store a character value. Character literals are written in single quotes, like this: 'A' (for multiple characters - strings - use double quotes: "ABC").

Characters are stored as numbers however. You can see the specific encoding in the ASCII chart . This means that it is possible to do arithmetic on characters, in which the ASCII value of the character is used (e.g. 'A' + 1 has the value 66, since the ASCII value of the capital letter A is 65). See Serial.println reference for more on how characters are translated to numbers.

The size of the char datatype is at least 8 bits. It’s recommended to only use char for storing characters. For an unsigned, one-byte (8 bit) data type, use the byte data type.

char var = val;

var : variable name. val : the value to assign to that variable.

Example Code

LANGUAGE Serial.println

Get the Reddit app

An unofficial place for all things Arduino! We all learned this stuff from some kind stranger on the internet. Bring us your Arduino questions or help answer something you might know! 😉

Compare if char array changed

Hello. I'm working on a project where I ask a device through serial, wait for response and then read the variables, then I'm gathering all of the variables by sprintf function to send them to my graph visualising software. For now I used fixed time of 100ms to send all of the variables, even if they didn't change yet but I want to send them as soon as anything changes. My plan was to check if "text" was different from "old_text" and then send it through Serial but I've got an error. Is there any easy way to check this without listing every variable because there will be almost 100 of them, and this sprintf function is one good place to check anything but supposedly I can't copy char arrays to check if it changed.

error: invalid array assignment

old_text=text;

Code snippet:

I'm thinking maybe if I sum all of the values and check if this value changed I can then send data but if there's any other way that I'm not aware of?

By continuing, you agree to our User Agreement and acknowledge that you understand the Privacy Policy .

Enter the 6-digit code from your authenticator app

You’ve set up two-factor authentication for this account.

Enter a 6-digit backup code

Create your username and password.

Reddit is anonymous, so your username is what you’ll go by here. Choose wisely—because once you get a name, you can’t change it.

Reset your password

Enter your email address or username and we’ll send you a link to reset your password

Check your inbox

An email with a link to reset your password was sent to the email address associated with your account

Choose a Reddit account to continue

invalid conversion from 'char' to 'char*' [-fpermissive]

Hi, can someone explain me why this code fails to compile?

edit: I'm using ARDUINO IDE, AVRISP mkll for generic ESP8266 module

I can't understand what I'm doing wrong. If you could give me some documentation to read and explain me the problem that would be the best.

EDIT: Now it seems to be ok. I've added the operator & before c . So it looks like this. buffer[length] = &c;            // save current char Can someone explain me what I just did? It would be really important to me to understand this thoroughly.

Furthermore I've another question: why I can't write something like this:

the compiler gives me this error: exit status 1, invalid array assignment. So what would be the correct way to handle this?

You declared buffer as holding pointers to characters (char *) and then attempted to put a char there. &c is a pointer to a character so of course that is allowed.

You probably intended to declare buffer as char and not as char* .

You cannot just copy arrays like that. You will need a loop or a function such as memcpy(...).

vaj4088: You declared buffer as holding pointers to characters (char *) and then attempted to put a char there. &c is a pointer to a character so of course that is allowed. You probably intended to declare buffer as char and not as char* .

mmh ok. So I ask you what are the differences between pointers and characters? Are there any advantages in using one instead of other?

vaj4088: You cannot just copy arrays like that. You will need a loop or a function such as memcpy(...).

Not like other languages. I thougth that if the arrays were been the same size, I would have been allowed to copy in that way. I was wrong. So i need to copy item by item.

There is a BIG difference between characters and pointers. If you need to ask the question, it is time to learn. Take a course or read some books about C or C++.

vaj4088: There is a BIG difference between characters and pointers. If you need to ask the question, it is time to learn. Take a course or read some books about C or C++.

:confused:

Related Topics

Topic Replies Views Activity
Programming Questions 5 2125 May 6, 2021
Programming Questions 4 5310 May 5, 2021
Programming Questions 5 4699 May 5, 2021
Deutsch 19 1074 June 23, 2022
Programming Questions 4 714 May 6, 2021
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
  • OverflowAI GenAI features for Teams
  • OverflowAPI Train & fine-tune LLMs
  • Labs The future of collective knowledge sharing
  • About the company Visit the blog

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Get early access and see previews of new features.

Assigning value to char array [duplicate]

I get no error when I type

But when I change it to the following, I get an error: Array type char is not assignable.

Why is array type char not assignable? Is it because the language is written that way on purpose or am I missing a point?

Rutvij Kotecha's user avatar

  • For responses: Since this is tagged C++, is there a non -strcpy "more C++" way to do this while dealing with char[] ? –  user166390 Commented Feb 16, 2013 at 22:56
  • A "more C++ way" would be to use string or vector<char> instead of char[]. By using char[] you're basically accepting all the C-ness that goes with it. –  Cogwheel Commented Feb 16, 2013 at 23:07
  • Nominated to reopen because this is tagged c++ and the "duplicate" says "in C". I know the answer is the same either way, but they're still different languages and the answer is subject to change or have subtleties worth pointing out here. –  Jim Hunziker Commented Feb 16, 2018 at 15:31

7 Answers 7

An array is not a modifiable lvalue

learnvst's user avatar

The C++ way of doing this, as I commented above, would be to use std::string instead of char[] . That will give you the assignment behavior you're expecting.

That said, the reason you're only getting an error for the second case is that the = in these two lines mean different things:

The first is an initialization, the second is an assignment.

The first line allocates enough space on the stack to hold 10 characters, and initializes the first three of those characters to be 'H', 'i', and '\0'. From this point on, all a does is refer to the position of the the array on the stack. Because the array is just a place on the stack, a is never allowed to change. If you want a different location on the stack to hold a different value, you need a different variable.

The second (invalid) line, on the other hand, tries to change a to refer to a (technically different) incantation of "Hi" . That's not allowed for the reasons stated above. Once you have an initialized array, the only thing you can do with it is read values from it and write values to it. You can't change its location or size. That's what an assignment would try to do in this case.

Cogwheel's user avatar

  • 1 Would you mind including that you could use a std::string and get the behavior the OP wants? I think that would be a good addition to a good answer. –  NathanOliver Commented Mar 11, 2016 at 16:25
  • 1 Yeah, I usually try to give two tier answers to questions like this (one direct, the other second-guessing the intent). Since the asker had seen my comment and this thread got closed as dupe, I guess I didn't bother. –  Cogwheel Commented Mar 11, 2016 at 17:29

The language does not allow assigning string literals to character arrays. You should use strcpy() instead:

NPE's user avatar

  • 1 Correct -- for a situation like this you should use strcpy . Forgetting you ever even heard of strncpy would probably be no loss at all -- it's almost as useless as gets . –  Jerry Coffin Commented Feb 17, 2013 at 0:37
  • Related Difference between 'strcpy' and 'strcpy_s'? –  Quazi Irfan Commented Mar 3, 2017 at 5:45

a is a pointer to the array, not the array itself. It cannot be reassigned.

You tagged with C++ BTW. For that case better use std::string. It's probably more what you're expecting.

  • 1 a is the array itself, not a pointer to the array. It can be implicitly converted to a pointer to the first element. –  Joseph Mansfield Commented Feb 16, 2013 at 22:48

Simple, the

is a little "extra feature", as it cannot be done like that on run-time.

But that's the reason for C/C++ standard libraries.

This comes from the C's standard library. If using C++ you should use std::string, unless you really want to suck all the possible performance from your destination PC.

SGH's user avatar

this is because initialization is not an assignment. the first thing which works is an initialization, and the second one, which does not work, as expected, is assignment. you simply cant assign values to arrays you should use sth like strcpy or memcpy . or you can alternatively use std::copy from <algorithm>

Hayri Uğur Koltuk's user avatar

It is so simple,(=) have two different mean assignment and initialization. You can also write your code like that

in this code you have no need to write a difficult code or function and even no need of string.h

Muhammad Shujauddin's user avatar

Not the answer you're looking for? Browse other questions tagged c++ or ask your own question .

  • The Overflow Blog
  • Scaling systems to manage all the metadata ABOUT the data
  • Navigating cities of code with Norris Numbers
  • Featured on Meta
  • We've made changes to our Terms of Service & Privacy Policy - July 2024
  • Bringing clarity to status tag usage on meta sites
  • Feedback requested: How do you use tag hover descriptions for curating and do...

Hot Network Questions

  • How did Jason Bourne know the garbage man isn't CIA?
  • Dial “M” for murder
  • DIN Rail Logic Gate
  • Is a *magnetized* ferrite less ideal as a core?
  • Did the Space Shuttle weigh itself before deorbit?
  • Is an invalid date considered the same as a NULL value?
  • Has anybody replaced a LM723 for a ua723 and experienced problems with drift and oscillations
  • Does the First Amendment protect deliberately publicizing the incorrect date for an election?
  • Has technology regressed in the Alien universe?
  • Does the expansion of space imply anything about the dimensionality of the Universe?
  • How to invoke italic correction in ConTeXt LMTX?
  • Age is just a number!
  • If Venus had a sapient civilisation similar to our own prior to global resurfacing, would we know it?
  • Linear Algebra Done Right, 4th Edition, problem 7.D.11
  • 90/180 day rule with student resident permit, how strict it is applied
  • Many and Many of - a subtle difference in meaning?
  • Why do these finite group Dedekind matrices seem to have integer spectrum when specialized to the order of group elements?
  • Making wobbly 6x4’ table stable
  • What majority age is taken into consideration when travelling from country to country?
  • Guitar amplifier placement for live band
  • What is the purpose of toroidal magnetic field in tokamak fusion device?
  • Are there jurisdictions where an uninvolved party can appeal a court decision?
  • What does it mean to have a truth value of a 'nothing' type instance?
  • QGIS selecting multiple features per each feature, based on attribute value of each feature

arduino invalid array assignment char

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Error: invalid types 'int[int]' for array subscript

I am trying to get an Arduino to work with some Adafruit Motor shields. Everything seemed to work fine, but when I tried to create a function, I get the error in the subject. As far as I can tell, everything was declared properly (the array is initialized and is indexed elsewhere with without issue). Only the function seems to cause an issue.

Would anyone have a guess at what's wrong?

  • arduino-uno

Greenonline's user avatar

  • 2 Please post the full error message from the compiler. And the code where it occurs. –  Mikael Patel Commented Feb 11, 2016 at 20:04

2 Answers 2

Take a close look at this line:

Looks like it should be:

Otherwise it is an illegal index (as there is only one shield).

The next error is in the function switchMotor(). Again, have a close look at the definition of "motor". There is a local variable with that name but you want to also access the global array with the same name.

My guess is that this is where the compiler give you the error message!

Mikael Patel's user avatar

  • To be able to help you must add the compiler error output with line, file, and the code. It is too much guessing otherwise. –  Mikael Patel Commented Feb 11, 2016 at 21:00
  • Changing the int motor = and the j == motor to xmotor instead of motor allows it to compile perfectly fine for me. Just get rid of that variable naming conflict and it should be fine. –  Majenko Commented Feb 11, 2016 at 22:56
  • Thanks Mikael, the motor naming conflict was the problem! –  Pedro Almada Commented Feb 15, 2016 at 12:23

I get a warning flag in UECIDE for the line:

‣ operation on 'address' may be undefined [-Wsequence-point]

Increment address, and then assign the new value from address into address. That seems a bit odd to me. I think you just meant to increment:

Also it flags up the 1 instead of i that Mikael found:

‣ array subscript is above array bounds [-Warray-bounds]

And of course there is the (somewhat hard to spot - Kudos to Mikael for finding it - it took me a bit to see it myself) naming conflict:

‣ invalid types 'int[int]' for array subscript

It was only when I used the advanced editing facilities of UECIDE to highlight all occurrences of motor that I found there was extra ones that shouldn't have been there causing the problem.

enter image description here

So just changing the integer variable name from motor to something else fixes it:

Majenko's user avatar

  • Cheers, that was the issue! –  Pedro Almada Commented Feb 15, 2016 at 12:24

Your Answer

Sign up or log in, post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .

Not the answer you're looking for? Browse other questions tagged arduino-uno or ask your own question .

  • The Overflow Blog
  • Scaling systems to manage all the metadata ABOUT the data
  • Navigating cities of code with Norris Numbers
  • Featured on Meta
  • We've made changes to our Terms of Service & Privacy Policy - July 2024
  • Bringing clarity to status tag usage on meta sites

Hot Network Questions

  • If Venus had a sapient civilisation similar to our own prior to global resurfacing, would we know it?
  • Why isn't openvpn picking up my new .conf file?
  • Many and Many of - a subtle difference in meaning?
  • 90/180 day rule with student resident permit, how strict it is applied
  • How to read data from Philips P2000C over its serial port to a modern computer?
  • Stargate "instructional" videos
  • Has anybody replaced a LM723 for a ua723 and experienced problems with drift and oscillations
  • What was the reason for not personifying God's spirit in NABRE's translation of John 14:17?
  • How do you "stealth" a relativistic superweapon?
  • Why does the definition of a braided monoidal category not mention the braid equation?
  • Did the Space Shuttle weigh itself before deorbit?
  • Non-linear recurrence for rational sequences with generating function with radicals?
  • Manuscript rejected after all reviewers indicate it is "good match" to publish in journal. Is this common?
  • QGIS selecting multiple features per each feature, based on attribute value of each feature
  • What majority age is taken into consideration when travelling from country to country?
  • Venus’ LIP period starts today, can we save the Venusians?
  • Can I cast True Strike, then cast Message to give someone else advantage?
  • Does the Ghost achievement require no kills?
  • What are these commands in the code?
  • Dial “M” for murder
  • How to express degrees of understanding in Chinese:
  • Difference between "backpedal" and "change your tune"
  • Will the US Customs be suspicious of my luggage if i bought a lot of the same item?
  • Is there a French noun equivalent for "twofer"?

arduino invalid array assignment char

COMMENTS

  1. Assigning char arrays

    Using Arduino Programming Questions system June 23, 2012, 8:49am 1 Hi, as memory is critical for me I want to use limited char arrays instead of Strings, but how do I pass on such a char array?

  2. char array help

    char array help - Syntax & Programs - Arduino Forum. Forum 2005-2010 (read only) Software Syntax & Programs. system December 13, 2009, 11:46pm 1. I am fairly new to coding and arduino. I am trying to have arduino write "On" or "Off" to a char variable depending on state of digital out pin. I keep getting invalid array assignment.

  3. Invalid types 'char [int]' for array subscript

    I get the error: invalid types 'char[int]' for array subscript```. wildbill June 13, 2021, 2:11pm 8. Try: char charectersInstr[strLenght]; sterretje June 13, 2021, 2:17pm 9. charectersInstr is a single character; you will need a pointer to a character: char *charectersInstr = new char[strLenght];

  4. c

    Neither can you assign an array's address; x = y; doesn't work either when x and y have types char[1] for example. To copy the contents of b to a[2], use memcpy:

  5. Simple word translator, return error: invalid array assignment

    Simple word translator, return error: invalid array assignment Ask Question Asked 9 years, 6 months ago Modified 9 years, 6 months ago Viewed 2k times

  6. Can't create an array of type const char*

    1. I have something being returned as a const char* and would like to save it to an array. I've tried this: const char* book[amtBooks] = ""; and get this error: error: array must be initialized with a brace-enclosed initializer. array. data-type. Share.

  7. array

    Description An array is a collection of variables that are accessed with an index number. Arrays in the C++ programming language Arduino sketches are written in can be complicated, but using simple arrays is relatively straightforward.

  8. arduino ide

    This is the error message that I keep getting: Incompatible types in assignment of uint8_t {aka unsigned char}' to 'uint8_t [1] {aka unsigned char [1]} .

  9. char array handling guide for beginners

    Using Arduino Programming Questions. frappl December 11, 2017, 8:58am 1. Hi, because i remember to my own confusion and frustration with char arrays I hope to help some with the following handling exambles. char array[12]="asdfgh"; //the max. string length is 11 characters. // and Null as string-terminator.

  10. char

    The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

  11. arduino ide

    The location where you are calling the function begin, has as first parameter a parameter of type const char* instead of char* ... remove the const from this argument type.

  12. assign variable to char array

    However i want to change it to use a variable as per below : #define STRING_LEN 128. char stringParamValue3[STRING_LEN]; IotWebConfParameter stringParam3 = IotWebConfParameter("Telegram API Number", "Telegram API", . stringParamValue3, STRING_LEN, "password"); const char BotToken[] = stringParamValue3; // Basically just taken away the "" and ...

  13. c++

    Here you're only swapping pointers to the char arrays (elements in the array of char array, i.e. 2D char array called name ), so a char* is what you're looking for.

  14. error loading an array of strings

    You are confusing the concept of a "string" (C string - char array) with a "String" - the Arduino-specific class that people who don't know better use.

  15. Problem with arrays

    i want to store in an array 25 matrix 8x8, each matrix position is an integer, You probably can't. That would be 3200 bytes, which is more than exists on an Arduino. If you want to have an array of references to bitmaps, suitable for displaying on dot-matrix displays, that usually comes out somewhat differently.

  16. Arduino, understanding invalid conversion warning, unsigned char to char*

    When you pass width_per_character to const char* it causes the warning because the variable is a uint8_t.

  17. Compare if char array changed : r/arduino

    Is there any easy way to check this without listing every variable because there will be almost 100 of them, and this sprintf function is one good place to check anything but supposedly I can't copy char arrays to check if it changed. error: invalid array assignment old_text=text; ^ Code snippet:

  18. arduino uno

    I am making an Arduino alarm. This is the error: exit status 1 invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+' Here is the code that triggers it: lcd.print("0"+

  19. invalid conversion from 'char' to 'char*' [-fpermissive]

    It says. invalid conversion from 'char' to 'char*' [-fpermissive] buffer[length] = c; // save current char. ^. I can't understand what I'm doing wrong. If you could give me some documentation to read and explain me the problem that would be the best.

  20. c++

    char a[10] = "Hi"; a = "Hi"; The first is an initialization, the second is an assignment. The first line allocates enough space on the stack to hold 10 characters, and initializes the first three of those characters to be 'H', 'i', and '\0'. From this point on, all a does is refer to the position of the the array on the stack.

  21. arduino uno

    Error: invalid types 'int [int]' for array subscript Ask Question Asked 8 years, 6 months ago Modified 5 years, 6 months ago Viewed 9k times