user3018708
user3018708

Reputation: 21

C language definition of this variable means

const u_int16  myDeviceDescriptor [] = "\p"
"\x12" // Length
 "\x01" // Type (Device Descriptor)

what it means, \x12 or \p

Upvotes: 1

Views: 950

Answers (3)

damienfrancois
damienfrancois

Reputation: 59170

Your code snippets seems to be part of an implementation of the USB protocol. The uint_16 array is written as the concatenation of several small strings to ease readability. Each hexadecimal value is followed by its meaning written as a comment.

The \p is to indicate a 'Pascal string'. It is a rather old construct, not available on all compilers. It requires the -fpascal-strings.

Upvotes: 3

Filipe Gonçalves
Filipe Gonçalves

Reputation: 21213

It is doing a compile time string concatenation. When you write something like:

"hello" " world" "\n"

It is equivalent to

"hello world\n"

Thus, the string you have in this code is "\p\x12\x01".

However, I believe this is invalid code: there is no such thing as \p in standard C. It can be an extension, but it's not defined by the standard. \xhh is valid for hexadecimal representation. But keep in mind that you're assigning a pointer to a characters sequence (more specifically, a pointer to a constant string initializer) to a u_int16 array, which is really an odd thing to do. Unless this code is targeted to a specific platform, or you're working on some embedded system or something like that, you have to be careful with it.

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 222923

In standard C, "\x12" means a string containing a character with value 18 (hexadecimal 1216), and "\x01" is a string containing a character with value 1. "\p" is not a standard C escape sequence, but it could be an extension.

In standard C, three successive strings like this are concatenated into one string (and terminated by a null character). The resulting string can be used to initialize a character array (an array of char, unsigned char, or signed char).

myDeviceDescriptor is declared as an array of u_int16. Unless u_int16 is an alias (via typedef) for char, unsigned char, or signed char, then initializing myDeviceDescriptor with this string is not defined by the C standard. Although it is possible that u_int16 is such an alias, it is unlikely unless this code was written for a special environment with 16-bit bytes.

It is possible that this syntax is an extension specific to the C implementation it was designed for, an extension that allows initializing arrays of u_int16 with string literals.

You would need documentation for the C implementation or other specific information about this code to determine what "\p" is and whether this extension is supported.

Another possibility is that the code is wrong.

Upvotes: 1

Related Questions