user2568508
user2568508

Reputation:

interpreting string literal

I had method which required const unsigned char * as parameter, e.g.

func(const unsigned char* p);

I called it like this: func("\x34\x21\x00\x00"); (value passed is decimal 8500 in little endian).

My question is why I could pass "\x34\x21\x00\x00" as char*?

Let me give brief explanation and please correct me if I am wrong.

What was passed to the function was the address of the first byte of the string "\x34\x21\x00\x00". The string "\x34\x21\x00\x00" is stored in memory, and basically I passed the address of the first byte of this string. Am I right?

Upvotes: 0

Views: 92

Answers (2)

concept3d
concept3d

Reputation: 2278

This is legal in C, pointer will be implicitly casted. In C++ though this will give an error (just tried in MSVC2010).

Upvotes: 1

RichieHindle
RichieHindle

Reputation: 281875

Yes, you're right. It doesn't matter what the string contains - you're passing the address of its first byte.

Upvotes: 2

Related Questions