Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9170483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:01:34+00:00 2026-06-17T16:01:34+00:00

I am finding myself again confused by C strings, chars, etc. Here is some

  • 0

I am finding myself again confused by C strings, chars, etc.

Here is some code I am using to test the syntax on the Arduino. I know that (*message)buff will give me a pointer (I still don’t really know why I need to use pointers, but I can do some research on that!), I convert the *message_buff to a String (just for something to do, but note that later on when I try and print this string to serial I only get a single ‘c’ character).

I set an array pointer three elements long (three bytes long?? I don’t really know):

char *mqtt_command[3] = {};

And later on when I try and add a value to the array using:

*mqtt_command[i] = str;

I get the error:

error: invalid conversion from ‘char*’ to ‘char’

If I change that to:

mqtt_command[i] = str;

(without the *) it compiles fine. I don’t know why…

Here is my code:

char *message_buff = "command:range:1";
char *str;
String msgString = String(*message_buff);
char *mqtt_command[3] = {};
int i = 0;

void setup()
{
  Serial.begin(9600);
  delay(500);

  while ((str = strtok_r(message_buff, ":", &message_buff)) != NULL)
  {
    Serial.println(str);
    mqtt_command[i] = str;
    i++;
  }

  delay(1000);

  Serial.print("Command: ");
  Serial.println(mqtt_command[1]);

  Serial.print("MQTT string: ");
  Serial.println(msgString);
}

void loop()
{
  // Do something here later
}

And here is the output:

command
range
1
Command: range
MQTT string: c

How can I understand chars, strings, pointers, and char arrays? Where can I go for a good all round tutorial on the topic?

I am passing in a command string (I think it is a string, maybe it is a char array????) via MQTT, and the message is:

command:range:1

I am trying to build a little protocol to do things on the Arduino when an MQTT message is received. I can handle the MQTT callbacks fine, that not the problem. The issue is that I don’t really understand C strings and chars. I would like to be able to handle commands like:

command:range:0
command:digital:8
read:sensor:2

etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:01:35+00:00Added an answer on June 17, 2026 at 4:01 pm

    You need a C (and/or C++) primer first, you need to work more on your understanding of the declarations and the syntax for pointer access and so on.

    This:

    char *mqtt_command[3] = {};
    

    means “mqtt_command is an array of 3 char *“, i.e. three pointers to characters. Since strings are represented as pointers to characters, this can be called “an array of three strings”. There’s no actual space for the characters themselves though, so this is not enough to work with but it’s a good start.

    Then, your first error is this code:

    *mqtt_command[i] = str;
    

    The problem the compiler is complaining about is that you’re dereferencing things too many times. Just mqtt_command[i] is enough, that evaluates to the i:th value of the array, which has type char *. Then, your initial asterisk dereferences that pointer, meaning the type of the left-hand expression is now char, i.e. it’s a single character. You can’t assign a pointer into a character, it (typically) won’t fit.

    Drop the initial asterisk to solve this.

    To analyze further, this:

    char *message_buff = "command:range:1";
    String msgString = String(*message_buff);
    

    is also wrong, for the same reason. You’re dereferencing the message_buff pointer, so the argument to the String() constructor is merely the first character, i.e. c. Again, drop the initial asterisk, you mean:

    String msgString = String(message_buf);
    

    which can be written as just:

    String msgString(message_buf);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm finding myself repeating the same snippets of code again and again, is it
I'm finding myself coding C3 for the first time, and using Visual Studio for
I'm finding myself doing more C/C++ code against Win32 lately, and coming from a
I'm finding myself repeatedly writing and rewriting the following kind of code: my %default
Using a proprietary framework, I am frequently finding myself in the situation where I
In my latest quest to learn some assembly language I'm finding myself constantly going
I'm finding myself writing this line for every query: using ( MyDataModel thisDataContext =
Yes, I have seen this question, but I am still finding myself confused: How
I find myself writing the same verbose DOM manipulation code again and again: Element
I'm finding myself writing very similar code in two places, once to define a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.