0

Could you please explain why, in order to convert a char array like this:

char strarr[5] = {65,83,67,73,73}; //ASCII

Into LPCSTR to be accepted by GetModuleHandleA() and GetProcAddress(), I have to first append 0 to the end ?

i.e. I have:

char strarr[6] = {65,83,67,73,73,0};

And only then convert as (LPCSTR)&strarr.

For some reason I don't get the first one works only sometimes (i.e. if I do not add 0 at the end), while if I do add zero at the end - this work all the time. Why do I have to add zero?

Oh and a side question - why in C++ do I have to explicitly state the size of array in [], when I am initializing it with elements right away? (If I don't state the size, then it does not work)

Thanks.

ereOn
  • 53,676
  • 39
  • 161
  • 238
Fit Dev
  • 3,413
  • 3
  • 30
  • 54
  • I made your question a little bit more readable. Please, do it yourself next time to give your questions better chances of being answered. – ereOn May 26 '12 at 08:09

1 Answers1

3

Those functions expect NULL terminated strings.

Since you only give them a pointer to a char array, they can't possibily know its size, hence the need for a particular value (the terminating NULL character) to indicate the end of the string.

ereOn
  • 53,676
  • 39
  • 161
  • 238