As will be obvious from my question -- I'm pretty new to this! Please excuse any ignorance.
I can't understand how to pull this off from within a function I'm writing. Here is my goal:
Create a static array (of length X) of char arrays (all of equal length Y). It should be static because I'll be accessing it from outside the function. The array will look like something like: items = {"abc123", "def456", "ghi789"}
Then I'd like to make changes to the array (according to a number of conditions that aren't important now) in the following manner: items[0][2] = "z";. This would change "abc123" to "abz123".
Finally, the function this is all happening in will return a pointer to the array items so that it can be referenced from the main loop.
Here are the problems I'm facing for each point: (1) I'm not sure how to properly initialize the array of char arrays. Currently I'm doing:
// Create array
static char items[X];
// Initialize array values by filling with char arrays of equal length
for (int i=0; i<X; i++) {
items[i] = "000000";
}
But I'm seeing warning: invalid conversion from 'const char*' to 'char'. What am I doing wrong here?
(2) When I try to change the contents of the char arrays within items I see the following error:error: invalid types 'char[int]' for array subscript. I'm coming from Python so my instinct is that items[i][j] should let me access individual characters within the sub-arrays of items, but apparently this isn't how it's done in C++.
(3) Actually not having issues with this yet. I'm doing the following and it seems to be working:
char *handle_items() {
// Create items array and do a bunch of stuff to it
return items;
}
If anyone has insight as to points 1 & 2 I'd love to hear! Thanks in advance.
Casting a text constant to char* avoids the compiler warning/error messages, but not the effects of later altering the text. If you ever use an identical text constant in the code you will get some unexpected results:
Actually, as has been pointed out, you don't get a warning because of the cast to a char , but I have always found it amusing that without the case the the compiler tells you that
ISO C++ forbids converting a string constant to 'char*
Feel free to declare a constant string, using a pointer or not, but that is not what is happening here
Here is my understanding of the problem
When you declare a pointer to a string then the pointer has to be stored somewhere as does the actual string. Now suppose that you make a change to that string such that it may get longer, then how can the longer string be stored in the same place as the original one ?
This I believe is the cause of the warning that is given if you try to change the contents of the string. Other members with more knowledge than me may know better and can comment, but I am still bemused by something that is forbidden is allowed
as you said, it make sense to warn/prevent operations that change the length of a constant char string; what if you make it longer
i often get this warning when i define a function that takes a const char * argument which doesn't modify that string (e.g. print()). but that function may be called with both const or non-const argument.
so it seems the compiler just wants the developer to explicitly indicate what the compiler should do. the developer has some control over the "guard rails"