Question

I have been wondering, why can I not write my code like so:

char myChar[50];
myChar = "This is a really cool char!";

Or at least like this:

char myChar[50];
myChar[0] = "This is a really cool char!";

The second way makes more sense that it should work, to me, seeing that I would start the array at the point I want it to start moving the letters into each spot in the array.

Does anyone know why C++ does not do this? And can you show me the reasoning behind the right way to do it?

Thank you all in advance!

Était-ce utile?

La solution

The first line:

char myChar[50];

...allocates an array of 50 characters on the stack. The second line:

myChar = "This is a really cool char!";

Is attempting to assign a const static string (which exists in read-only memory in the text segment of your code) to the address of the beginning of the array. This is an incompatible LVALUE/RVALUE matcing/assignment. This approach:

const char* myChar = "This is a really cool char";

Will work, as the assignment of a pointer to address a string literal must be done at initialization time. There are potential exceptions, as in assigning a const char* pointer to a string literal like so:

/*******************************************************************************
 * Preprocessor Directives
 ******************************************************************************/
#include <stdio.h>


/*******************************************************************************
 * Function Prototypes
 ******************************************************************************/
const char* returnErrorString(int iError);


/*******************************************************************************
 * Function Definitions
 ******************************************************************************/
int main(void) {
   int i;
   for (i=(-1); i<3; i++) {
      printf("i=%d - Error String:%s\n", returnErrorString(i));
   }
   return 0;
}

const char* returnErrorString(int iError) {
   const char* ret = NULL;
   switch (iError) {
      case 0:
         ret = "No error";
         break;
      case (-1):
         ret = "Invalid input";
         break;
      default:
         ret = "Unknown error";
         break;
   }
   return ret;
}

You might benefit from reading the post in my references below. It will give you some info on how code, variables, constants, etc, are broken into different segments of the final binary, and why some approaches don't even make sense. Also, it would be beneficial to read up a bit on terminology like integer literals, string literals, l-values, r-values, etc.

Good luck!

References


  1. Difference between declared string and allocated string, Accessed 2014-05-01, <https://stackoverflow.com/questions/16021454/difference-between-declared-string-and-allocated-string>

Autres conseils

You must initialise the array of chars inside the declaration of the array. There is actually no reason for not doing so, as if not, your array will contain garbage values until you initialise it. I advise you to look at this link:

Char array declaration and initialization in C

Also, you are allocating a char array of size 50 but only using 28 elements of it, this would appear to me to be a waste...

Try the following for simple string initialisations:

char mychar[11] = "hello world";

Or...

char *mychar = "hello world";

I hope this helps...

If you want to think of this in holistic terms, the reason is because myChar isn't a string -- it's just an array of char. Hence "FooGHBar" and char [50] are completely different types. Related in a sense, but really not.

Now some might say, "but "FooBar" is a string, and char [50] is really just a string too." But that is going on the assumption that myChar is the same as "FooBar", and it's not. It's also assuming that the compiler understands that both char[50] and char* are pointers to strings. The compiler doesn't understand that. There could be any manner of thing stored in those places that have nothing to do with strings.

"But myChar is just a pointer?"

That is the reason why people think that the assignment should be a natural thing -- but the fundamental premise is wrong. myChar is not a pointer. It is an array. A name which refers to an array will decay into a pointer at the drop of a hat, but an array is not a pointer.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top