Thread: How does this interesting syntax work?

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    2

    Question How does this interesting syntax work?

    I am working on following the execution flow of the yesCrypt C code from openwall to see where the starting point is. While doing so I came across some interesting syntax I have not encountered before.

    Code:
    yescrypt_binary_t key = {.uc={1,2,3,4,5,6,7,8,9 . . . 128,64,32}};
    The 'yescrypt_binary_t' data type in the yescrypt.h file:
    Code:
    typedef union {
        unsigned char uc[32];
        uint64_t u64[4];
    } yescrypt_binary_t;
    The '.uc' is the curious part. How does this work?

    Walt

  2. #2
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    The 'uc' is a member of the yescrypt_binary_t union type. The syntax '.uc=...' is a "designated initialization". That means the following values are used to explicitly initialize the 'uc' member (independedly of the member order in the yescrypt_binary_t union).

  3. #3
    Registered User
    Join Date
    Dec 2022
    Posts
    2
    Ah! I should have seen that. 'uc' represents the array in the struct.

    That is some really useful shorthand. I haven't worked (played) with C programming in 3 or 4 years now.

    Thanks

  4. #4
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    ... in the struct.
    Because it seems you're trying to understand the code - It isn't a struct, it's a union. The whole point of a union is that the members share the same memory space. The initialization of the 'uc' member makes that you can access the written bytes reinterpreted as array of uint64_t in the 'u64' member.
    FWIW I'm pretty sure the developer of the lib made some assumptions. 1. An unsigned char has a width of 8 bits. 2. The target platform has Little Endian byte order. However, don't be afraid using it. You would for sure know if you work on one of those exotic computers that don't meet these preconditions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array Syntax Versus Pointer Syntax
    By LyTning94 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2011, 10:56 AM
  2. Replies: 7
    Last Post: 11-15-2007, 01:36 AM
  3. Interesting Problem: sprintf does not work
    By mangoMan in forum C++ Programming
    Replies: 15
    Last Post: 05-05-2004, 08:04 PM
  4. interesting...
    By ober in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-11-2002, 02:18 PM
  5. Interesting Day
    By gnu-ehacks in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 02-23-2002, 08:49 AM

Tags for this Thread