1

My question is: How to printf the "pMessage","aiLength","szDataSize"?

EventInfo* pEventInfo

typedef struct {
        char* pMessage;
        SocketHeader* pSocketHeader;
        PipeHeader* pPipeHeader;
} EventInfo;

typedef struct {
        apr_uint32_t aiLength;
} PipeHeader;

typedef struct {
        apr_uint32_t szDataSize;
} SocketHeader;

What is the "apr_uint32_t"?

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
y_wang
  • 15
  • 1
  • 4

5 Answers5

2

apr_uint32_t is a portable 32-bit unsigned integer from the Apache Portable Runtime project.

You ought to format it the same way you'd printf a native unsigned integer that you know for certain is 32 bits wide: use the PRIu32 format specifier as recommended in this answer.

(As a side note, other apr portable types come with their own platform-specific printf specifiers, e.g., apr_uint64_t has a corresponding APR_UINT64_T_FMT. However, this type does not.)

Community
  • 1
  • 1
pilcrow
  • 56,591
  • 13
  • 94
  • 135
2

While I think @pilcrow's answer - using C99 format string conversion specifiers in <inttypes.h> - is probably the most elegant, the unsigned long type is guaranteed to be at least 32 bits, so you could simply use the %lu specifier with a cast:

printf("%lu\n", (unsigned long) value);

Which doesn't require C99. That's not much of an issue today, but IIRC, APR doesn't assume a C99 compiler either, or they wouldn't have bothered rolling their own 'exact' types.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
  • I tried it. The result is the same to Navnath. But I think your way better. So thank you very much. – y_wang Jun 06 '13 at 05:47
  • It depends on `typedef` is for what datatype, If library Say's it is `typedef` to given datatype then why to assume, And does it `unsigned int` equal to `unsigned long` ? And what is difference between `apr_uint32_t` and `apr_ino_t` – Navnath Godse Jun 06 '13 at 06:07
  • +1 and clever. Also, [C99 certainly isn't everywhere](http://stackoverflow.com/questions/16908899). – pilcrow Jun 06 '13 at 13:37
0

Actually apr_uint32_t is a typedef of unsigned int

http://www.cplusplus.com/reference/cstdio/printf/ for format specifiers of printf().

Refer this code http://svn.haxx.se/dev/archive-2004-04/att-0788/intl.c

one more sample http://src.gnu-darwin.org/ports/sysutils/ftwin/work/ftwin-0.8.0/src/checksum.c.html

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0

Try this

EventInfo eventInfo;
SockHeadExample sHead;
PipeHeader pipeHeader
printf("%s", eventInfo.pMessage);  // <- pMessage
printf("%u", sHead.szDataSize);    // <- szDataSize
printf("%u", pipeHeader.aiLength); // <- aiLength

Working

apr_uint32_t is unsigned int it is typedef by Apache library

Reference

Community
  • 1
  • 1
Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
  • That same reference you link to warns: *"The actual values of macros and typedefs on this page are platform specific and should NOT be relied upon!"* – pilcrow Jun 06 '13 at 05:20
  • if the are always scaler data you should be able to safely printf("%lld",(long long)aprVal ); – Grady Player Jun 06 '13 at 05:30
  • Thank you very much Navnath. I have tried. His answer was right. – y_wang Jun 06 '13 at 05:33
  • This might work, but it is not portable - the `unsigned int` type is not guaranteed to be 32 bits. It kind of defeats the purpose of a 'portable runtime' to make these assumptions. – Brett Hale Jun 06 '13 at 05:36
  • @BrettHale: If we are printing `unsigned int` use `%u`, If we are printing `unsigned long` use `%lu` and it is portable, as machine know how to print `unsigned int`(%u) and `unsigned long`(%lu). Here we are not assuming, the documentation specify the type of `apr_uint32_t` is `unsigned int` so no need to print it as `long` if it work fine for '%u'. – Navnath Godse Jun 06 '13 at 06:17
  • You are still ignoring @pilcrow's comment above. The reference you provided states that these *"typedefs... should NOT be relied upon!"* – Brett Hale Jun 06 '13 at 06:24
  • @BrettHale: Yes you are right, i doesn't mean that is it 32 bit, i am saying is at runtime machine knows `unsigned int` and also know how to print it by format specifier `%u` then why it need to be convert to long ? – Navnath Godse Jun 06 '13 at 06:33
  • @BrettHale:The "apr_uint32_t" type is long unsigned int.If I use the 32-bit platform. I need 4 byte, like type int. But If I use the 64-bit platform. I need 8 byte.Is that right? – y_wang Jun 06 '13 at 06:46
  • @y_wang - promoting to a wider (unsigned) type always works as expected, even if `unsigned long` is 8 bytes. But consider Win64, which defines `unsigned long` as a 32-bit type, even on a 64-bit platform: the [LLP64](http://en.wikipedia.org/wiki/64-bit_computing#64-bit_data_models) data model. – Brett Hale Jun 06 '13 at 06:52
0

apr_uint32_t is unsigned int, so you should know what to do next.

Charles0429
  • 1,406
  • 5
  • 15
  • 31
  • 1
    Nope, the typedef is platform specific and explicitly *not* guaranteed to be an unsigned int. – pilcrow Jun 06 '13 at 05:22
  • yeah but it should be pretty close to uint32_t if you get to be C99 which apr doesn't, so the define their own... if you are on any system in current usage, with the exceptions of embedded and research platforms, you will have a 32 bit int. – Grady Player Jun 06 '13 at 05:37