herophant
herophant

Reputation: 680

What does something like int *array[99] = {0}, *u do?

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.

Upvotes: 0

Views: 99

Answers (4)

David C. Rankin
David C. Rankin

Reputation: 84599

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.

Upvotes: 1

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.

Upvotes: 0

John Bode
John Bode

Reputation: 123558

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;

Upvotes: 0

Andreas DM
Andreas DM

Reputation: 11018

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 */

Upvotes: 1

Related Questions