'How do I use typeof with dynamic types?

I'm slightly confused about typeof and how to use it properly. I know typedefs exist but I imagine typedefs are more for static/compile time types where as typeof is dynamic?

In this code

int main()
{
    int t[] = {0,1,2,3,4,5};
    typeof(t) a;
    for(int i = 0; i < 6; ++i){
        a[i] = i;
    }
    return 0;
}

Since t is an array allocated on the stack is then typeof(t) a also an array allocated on the stack? Or what if I made a pointer point to t, then made a type of p ?

int main()
{
    int t[] = {0,1,2,3,4,5};
    int* p = t;
    typeof(p) a;
    
    return 0;
}

Is a now just an integer pointer that hasn't been allocated?

Or if I allocate t dynamically then make a typeof t

int main()
{
    int* t = malloc(6*sizeof(int));
    for(int i = 0; i < 6; ++i){
        t[i] = i;
    }
    typeof(t) a;
    for(int i = 0; i < 6; ++i){
        a[i] = i;//crashes
    }
    return 0;
}

Is this again just an integer pointer and allocation does not play a role?

Or what if I wanted to take a pointer but make a 2d array out of that pointer?

int main()
{
    int* t = malloc(6*sizeof(int));
    for(int i = 0; i < 6; ++i){
        t[i] = i;
    }
    typeof(t) a[2][3];
    printf("%d\n",a[0][1]);
    return 0;
}

Is that valid?

int main()
{

    
    int x  = 2;
    int y = 3;
    typeof(int[x][y]) a;
    for(int i = 0; i < 2; ++i){
        for(int j = 0; j < 3; ++j){
            a[i][j] = i+j;
        }
    }
    return 0;
}

Or is that valid? Is a already allocated on the stack here ? If it is, then is it using VLA to do a stack based allocation for the array? Am I just over thinking what typeof does?

c


Solution 1:[1]

There is no such thing as a "dynamic type" in C. Types are fully resolved in the compiler and then cease to exist. typeof is substituted as though it were a typedef for the declared type of its argument, which doesn't have to be a simple variable.

In your fourth example:

int main()
{
    int* t = malloc(6*sizeof(int));
    for(int i = 0; i < 6; ++i){
        t[i] = i;
    }
    typeof(t) a[2][3];
    printf("%d\n",a[0][1]);
    return 0;
}

typeof(t) is precisely int* because that's what you declared t to be. So the new variable a has type int* [2][3] -- that is, an array of arrays of pointers to int. That's probably not what you want, since you try to print one of its (uninitialised) elements as though it were an integer. That suggest that the declaration you were looking for was:

    typeof(*t) a[2][3];

In any event, how a variable is allocated and what its type is are two orthogonal concepts (although certain restrictions apply, notably that you cannot declare a VLA at global scope).

As another take-away, always enable compiler warnings. I suspect that reading the error messages would have helped with at least some of those examples.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1