0

How do I convert from char to const char? How do I implement type casting without changing the char SendBuf? Here is my code:

.cpp

// main()    
char SendBuf[6] = "Hello";
sendto(..., SendBuf, ...);

// sendto structure
int sendto(
__in  SOCKET s,
__in  const char *buf,
__in  int len,
__in  int flags,
__in  const struct sockaddr *to,
__in  int tolen
);

Error

Error   1   error C2440: '=' : cannot convert from 'const char [6]' to 'char [6]'

Thank you.

activout.se
  • 6,058
  • 4
  • 27
  • 37
Chicko Bueno
  • 337
  • 2
  • 11
  • 25
  • 1
    That code should not produce that error message. Maybe you left something relevant out. – aschepler Mar 18 '11 at 18:10
  • please create the smallest program that you can that still has the problem, and copy and paste that code into your question. Please do not re-type your code, but rather paste the exact text in. In summarizing and retyping your program, you left out an important detail. – Robᵩ Mar 18 '11 at 20:25
  • possible duplicate of [Array Assignment](http://stackoverflow.com/questions/5279082/array-assignment) – Ben Voigt Mar 18 '11 at 21:28
  • @Adams this is my simplest coding what `sendto` from socket parameter should take. i just need to know the correct assignment for my variable. – Chicko Bueno Mar 19 '11 at 04:59

4 Answers4

4

I don't have a Microsoft compiler handy to test this theory, but I suspect that the OP has code like this:

int main() {
    char SendBuf[6];
    SendBuf = "Hello";
    // sendto(..., SendBuf, ...);
}

I suspect that the error he is seeing, cannot convert from 'const char [6]' to 'char [6]' is occuring on assignment, not initialization nor the sendto call.

Can someone with a Microsoft compiler check compile the above program and confirm the error message?

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
3

Write the assignment like this:

const char *SendBuf = "Hello";

You don't have to do anything about the function call. (A const char* parameter will take a char* variable as input.)

activout.se
  • 6,058
  • 4
  • 27
  • 37
  • The error is in the assignment, not the function call. I'll update. – activout.se Mar 18 '11 at 18:10
  • what does a `const char` is actually means? – Chicko Bueno Mar 18 '11 at 18:23
  • @Chicko: In a variable declaration, it means it creates a constant variable (can never be written to). On a pointer, it means the pointer is a read-only view of the memory, but some other part of the program might still change to pointed-to variable. – Ben Voigt Mar 18 '11 at 18:35
  • @divide: I wouldn't recommend this fix, maybe he needs to write into the buffer before transmitting it. – Ben Voigt Mar 18 '11 at 18:36
  • @divide: What assignment? The assignment that's in the real code, but not in the question? (Your answer and the question both have *initialization*, not *assignment*) – Ben Voigt Mar 18 '11 at 21:31
  • Actually my buffer would be a dynamic buffer. Means, each time the user needs to specify buffer data before sending. Not just constant variable all the time. – Chicko Bueno Mar 19 '11 at 04:47
  • Your question is related with http://stackoverflow.com/questions/6024574/how-to-convert-a-const-char-to-simply-char – Mihai8 Jan 08 '13 at 21:48
2

Try

char SendBuf[6] = { "Hello" };

Note the braces, which tell the compiler you're initializing an aggregate (in this case an array).

And then, instead of

SendBuf = "Hello";

which is obviously your real code, use

strcpy(SendBuf, "Hello");

or

memcpy(SendBuf, "Hello", 6);
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • +1: ignores the misleading title & questions resolves the compile error and actual problem ;) – AJG85 Mar 18 '11 at 19:18
  • -1: It is legal to initialize `char[]` with a string. The braces don't tell the compiler anything it doesn't already know. – Robᵩ Mar 18 '11 at 20:29
  • @Rob: Well, there's apparently some compiler (probably some older Visual C++) that has a problem with it... actually just tried assignment, since that's what the compiler message talks about, verified that's the issue, and noted you've guessed that as well. – Ben Voigt Mar 18 '11 at 21:27
  • @Rob: And `char[]` is an exception to the general rule. Putting braces around array initializers is a good habit for consistency, even if skipping it is specifically allowed for `char[]` (only). – Ben Voigt Mar 18 '11 at 21:29
  • @Rob: Edited answer to address what is obviously the real code. – Ben Voigt Mar 18 '11 at 21:32
1

Is there a reason why you can't pass your string literal into the method? sendTo(...,"Hello",...);

If yes then why not simple declare const char* SendBuf = "Hello"; instead of using the char array as one alternative.

Or perhaps better yet start exploring STL and use std::string SendBuf("Hello"); and pass SendBuf.c_str() into the method.

There is always more than one way to skin a cat just pick the knife that is most comfortable for you.

AJG85
  • 15,849
  • 13
  • 42
  • 50
  • Yuck. `std::string` is not really appropriate for storing network packets. And `c_str` is definitely not, `sendto` doesn't work on the basis of a NUL terminator. – Ben Voigt Mar 18 '11 at 18:37
  • I wasn't jumping to the conclusion of usage although you're probably right even though OP only asked about conversion and `char` vs `const char` ... the point remains the same use the best tool for the job whatever that may be. – AJG85 Mar 18 '11 at 19:03