Pages: [1]   Go Down
Author Topic: String question  (Read 845 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 62
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I am getting this error:
error: invalid conversion from 'const char*' to 'char'
on this code:
Code:
char input[100];
 input[0] =  "key1 M";

whats the problem please?

Thank you. smiley
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The input variable is an array of chars. The input[0] variable can hold one character. You are trying to store 7 characters in that one position in the array.

The compiler is telling you that you can't store an array of characters ("key1 M\0") in a character variable (input[0]).

You can use strcat like so:
Code:
input[0] = '\0'; // Null terminate array
strcat(input, "key1 M"); // Add "key1 M" to array, moving the NULL to the end

After this, input[0] through input[6] will have values.
Logged

0
Offline Offline
Edison Member
*
Karma: 8
Posts: 1249
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry to jump in here..
but being new, I havent even used an array yet.


So you can NOT just dump/push a full 'string' into an Array index?

I know syntax is not correct for C

but example:

char someStuff[6] = {"hello", "my", "name", "is", "slim", "shady"};


doesnt NOT work? is here anyway to make/create an array of 'strings'?

I see it works similar to that for number/integers?

int number[4] = {1, 2, 3, 4};


can you explain you 'solution/example' a bit too?

I dont understand it a I guess.. (sorry)


input[0] = '\0';


^  create a new, but empty array?  (why the backslash? espcaping?)


strcat(input, "key1 M"); // Add "key1 M" to array, moving the NULL to the end


Where are you getting strcat() from? I dont see it reference page?

but that function seems to push/dump the data into the array?
 is this correct? (in front..not @ end of array)



thanks



Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char someStuff[6] = {"hello", "my", "name", "is", "slim", "shady"};
This does not work because someStuff is an array of characters, not an array of strings. An array of strings could be declared using a 2D array of characters (char someStuff[6][6]) or an array of pointers to string (char *someStuff[6]). Initialization of such an array is left as an exercise for the student.

Code:
input[0] = '\0';
If this was input[0] = '0'; the ascii value for the digit 0 would be placed in the array, which is not a NULL. It could be written input[0] = 0;, but the usual convention for setting a character array position is to enclose the value in single quotes. To get a NULL, the \0 does that.

This code does not create a new but empty array. It initializes an existing array to an empty string.

The strcat function is documented in an C book, and in dozens (or more) web pages. Google it. It concatenates a string onto the end of an existing string. If the existing string is empty, the result is that of assigning a string to an array.
Logged

0
Offline Offline
Edison Member
*
Karma: 8
Posts: 1249
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ahh..

I didnt know any 'C' functions..etc were 'legal' to use.. I thought only what was listed in the reference link(s) was what was available to use Arduino users..


Quote
Initialization of such an array is left as an exercise for the student.

what do you mean by that?


When you say 'pointers'.. do you mean like variables? I dont understand the * in the statement/function

I do know 2D+ arrays..

but I have never had to 'deal' with characters and the arrays are defined a bit different (and we have datatyping too)

example of what Im used to doing...

var someArray:Array = new Array();
someArray.push("Some string");

or:

var someArray:Array = new Array("Jon", "Billy", "Mandy");

or even

var someArray = ["value1", "value2", "value3"];

object arrays: (named values/pairs)

var someArray = [{name1:"value1", name2:"value2", name3:"value3"})];

and of course call or taget any value as such:

var valueCheck = someArray[0]   //outputs: value1

for named pairs:
var valueCheck = someArray[0].name1   //outputs: value1

so Im just trying to bridge how arrays are used in the Arduino (C) environment.

I cant picture your 2D arrays..or you pointer type arrays.

Thanks

Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not every C function can be used. Some don't make sense, like fopen. But, if there is memory available, you can use the function (although some, like sprintf, are limited).

Quote
Initialization of such an array is left as an exercise for the student.
Quote
what do you mean by that?
It means that you decide whether to use pointers or 2D arrays, and then, based on that choice, figure out how to initialize the variable, rather than having me tell you.

Quote
When you say 'pointers'.. do you mean like variables? I dont understand the * in the statement/function
Need to learn more C, then. Get a C book, and work through all the exercises on the PC. Nearly everything you learn will then be applicable to the Arduino.
Logged

Pages: [1]   Go Up
Jump to: