0
bool checkSubarraySum(int* nums, int numsSize, int k) {
    int i, s, found = 0;

    e_t buff[10000];
    int n;

    e_t *set[SZ] = { 0 }, *e;

    put(set, &buff[n ++], 0, -1);

    s = 0;
    for (i = 0; i < numsSize; i ++) {
        s += nums[i];
        if (k) s = s % k;
        e = lookup(set, s);
        if (e) {
            if (i - e->idx >= 2) {
                found = 1;
                break;
            }
        } else {
            put(set, &buff[n ++], s, i);
        }
    }

    return found;
}

What is e_t *set[SZ] = { 0 }, *e; doing? e_t is a user defined type but I don't think that matters. e is not a pointer that has been defined anywhere in global scope to my knowledge, and I tried something like the following:

int *array[5] = {0}, *u;

and no syntax errors were given. The first part, i.e. int *array[5] = {0} initializes all five elements of this array to 0. But what is the purpose of *u? You can't just assign an array to something else, right, it's an address, not a pointer. And u has never even been defined, so, I would expect some sort of NameError...

Thanks for any help in advance.

herophant
  • 642
  • 7
  • 16

4 Answers4

1
int *array[5] = {0}, *u;

Is a declaration of two int objects. The first:

int *array[5] = {0}

declares an array-of-pointers to int [5] (meaning an array of 5 pointers to int) initialized to NULL by virtue of using the "universal initializer" {0}. The equivalent, but more intuitive initialization would be:

int *array[5] = {NULL}

The ',' is simply a separator here that allows the second declaration *u to be included in the same line without a separate int *u; declaration.

(not to be confused with the comma-operator that simply discards expressions to the left of the final ',' evaluating the last expression. See What does the comma operator , do? -- thank you @AnttiHaapala)

So:

..., *u;

declares a single (uninitialized) pointer-to int.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
1

It is similar to typing:

int x, y;

but notice the types when typing something like:

int a,  *b,  **c;
/*  ^    ^     ^
*  int  int*  int**
*/

therefore

int *array[5] = {0}, *u;
                   /* ^ is pointer to int */
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • So the star is tied to the variable name, not the type `int` or `bool` itself, right? So something like `int x, *y, z, **t` declares variables `x` and `z` of type int, `y` of type pointer to int, and `t` of type `int **` (pointer to pointer of int), right? That's why `*u` is not a pointer to a pointer of an int? – herophant Jul 28 '19 at 22:54
0
e_t *set[SZ] = { 0 }, *e;

is a declaration of two objects; set is an array of pointers to e_t, while e is a pointer to a single e_t. It may also be written as:

e_t *set[SZ] = {0};
e_t *e;
John Bode
  • 119,563
  • 19
  • 122
  • 198
0

e_t *set[SZ] = { 0 }, *e; should be read as "the programmer hereby declares that the following are of type e_t: the objects pointed to by each SZ elements in set; and the object pointed to by e."

= {0} causes each element in set to be initialized to null pointers - the first explicitly and the remaining implicitly.