Peter N Peter N - 1 month ago 15
C Question

Expected expression before

What does this error mean?

error: expected expression before 'Addr'


typedef struct sockaddr_in Addr;

int main() {
WSADATA Winsock;
SOCKET Socket, Sub;

ZeroMemory(&Addr, sizeof(Addr)); // Error occurs here
return 0;
}


I added a comment where the error was occurring at.

Answer

You're not actually creating a variable you can call the function with, you're just typedef 'ing a type of variable that suits your needs. You actually need to declare a variable of that struct type in order for it to work.

Like this:

typedef struct sockaddr_in Addr; // Defining what a variable (struct) of type Addr is

int main() {
    WSADATA Winsock;
    SOCKET Socket, Sub;
    Addr testAddr; // Actually declaring a variable of type Addr

    ZeroMemory(&testAddr, sizeof(Addr));
    return 0;
}