Go Down

Topic: Problem with assigning a single character string to an array slot. (Read 580 times) previous topic - next topic

widget

Hi,

I have an array declared like this (I know don't judge me), it's for storing each character of the lcd as a sort of cache:

char* lcd_characters[] = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};

Then later on I'm trying to write to a specific slot of the array like this to save that letter to it:

new_char = String(message.charAt(i);
...blah blah blah...
lcd_characters[pos] = char(new_char); << error here

However it is giving me this error:

error: cannot convert 'String' to 'char*' in assignment

The funny thing is when I do this (below) it do assign the letter to it, however I have a var which is a single letter but can't assign it.

lcd_characters[pos] = "H";

Can someone help me out please. Thanks!!!

widget

Sorry the error line should have read as follows:

lcd_characters[pos] = new_char; << error here

lar3ry

Right, you can't convert a String to a char. Imagine if I wanted to convert "Hello World" to a char.

The problem is, a String is not a string. A String (note the capital 'S') is an object that encapsulates a string, (again, note the 's'), which is an array of char. Strings, on an Arduino are an extremely bad idea. They will fragment your memory faster than you can blink. Everry time you change a String, a copy is made, and the old one dereferenced, leaving a 'hole' in your memory.

Use C-style strings... arrays of char. Yes, there are some nice things you can do with Strings, but the cost is way too high. They are very nice on machines with Gigs of memory, but really don't belong on an Arduino.
There are 10 kinds of people in the world,
those who understand binary, and those who don't.

widget

I'm sorry I don't really understand your answer or how it fixes the code above.

Basically I want an  array of characters  and then I want to write on the array positions with a new value.

Why does it even matter what type of string I'm writing to that array position, I should be able to write a number or boolean there too and call it later. Is there something wrong with the way the array is declared initially? 

PaulS

Quote
Basically I want an  array of characters  and then I want to write on the array positions with a new value.

Then create an array of characters.

Code: [Select]
char* lcd_characters[] = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
is NOT an array of characters. It is an array of pointers to characters. Lose the * to make an array of characters.

Of course, then your initialization will be all wrong. But, if you think about what the compiler might default the value of a character to, you'd see that you don't really need to provide an initializer.

Of course, the real reason that you need to provide one now is because you expect the compiler to count them for you. Tell the compile how big the array should be, and skip the initializers.

lar3ry


I'm sorry I don't really understand your answer or how it fixes the code above.

Sorry, I didn';t understand the part of your code that read:

Code: [Select]
new_char = String(message.charAt(i);
...blah blah blah...
lcd_characters[pos] = char(new_char); << error here


I was especially confused by the fact that you didn't declare new_char, and the line that reads ...blah blah blah...

Read the 'sticky' threads at the top of this forum and try again.

Code: [Select]
Is there something wrong with the way the array is declared initially?

Absolutely, though I didn't wade through it because it was all in proportional font, having not been put into code tags along with the rest of your program. We put the posting instructions up there for a reason.

There are 10 kinds of people in the world,
those who understand binary, and those who don't.

PeterH


I have an array declared like this (I know don't judge me), it's for storing each character of the lcd as a sort of cache:

char* lcd_characters[] = {"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};


That isn't an array of characters. It is an array of character pointers, which you have initialised to point to some empty strings.

You would declare a character array like this:

Code: [Select]

char lcd_characters[40] = {"Your initial text here"};

If you leave the square brackets empty then the array size is not defined explicitly so the array size would be defined by your initialisation text.


Then later on I'm trying to write to a specific slot of the array like this to save that letter to it:

new_char = String(message.charAt(i);
...blah blah blah...
lcd_characters[pos] = char(new_char); << error here


I suggest you not use the problematic String class, but the code to assign a character into a char array is correct, once the array has been declared correctly as shown above. Note that when using c-strings the text is held as a null terminated array of characters and when you access individual elements in the array it is up to you to ensure that you stay within the bounds of the array and that your string remains null terminated.
I only provide help via the forum - please do not contact me for private consultancy.

michinyon

A char is a single character.

An array of chars holds a single char at each array location.

You are trying to stick a whole bunch of chars into a single array location,   of course it won't work.

Unless you set up some kind of array of ponters to other arrays.

westfw

Is there a reason you just can use an array of chars:
Code: [Select]
char lcd_characters[24];

lcd_characters[pos] = message.charAt(pos);


(you still might want to to consider avoiding "String" elsewhere, for the reasons other people have been giving, but there isn't any need to use either String or strings in your lcd_character arary, as you've described.)

Go Up