Understanding Spark.variable() function

Good question @flashyourface! This is a basic thing in C/C++, but I totally get how it’s confusing from a web language! :smiley:

Two things need to change.

Reserve memory for the string

Strings take up a variable amount of space. Each letter adds more memory that you need to reserve. The word used by C programmers for this is that you have to “allocate” memory for the string.

At the top, change char *msg; which only allocates enough space for the pointer to the string to char msg[100]; to reserve space for up to 99 characters. C strings always end with a null byte.

Use strcpy instead of equals

To make a copy of a string variable in C, you should use the strcpy function. Also String objects have a c_str() method to pull out the char*, so you should do this:

strcpy(msg, message.c_str());

Your integer code should work fine! :+1: What error are you getting?

2 Likes