Slash
Slash

Reputation: 9

What does "error: assignment to expression with array type" mean?

I try to compile the following code, but I constantly get this error.

    char command[100];
    FILE *fp;
    command = sprintf(command, "sudo asterisk -rx \"pjsip show aor %s\"", row[i]);
    fp = popen (command, "r");
    if (fp == NULL) {
        printf("Failed to run command\n" );
        exit(1);

This error appears: "error: assignment to expression with array type"

Upvotes: 0

Views: 4231

Answers (1)

P.P
P.P

Reputation: 121387

You are assigning the value of sprintf() to a variable which has array type. Arrays are not modifiable lvalues; so you can't assign to them. sprintf() returns an int -- so you need to assign its value to an int. However, I'd suggest to avoid sprintf() and use snprintf() instead. Because sprintf() is prone to buffer overflow.

int rc = snprintf(command, sizeof command, "sudo asterisk -rx \"pjsip show aor %s\"", row[i]);

Upvotes: 2

Related Questions