“错误:赋值给表达式与数组类型”是什么意思?

时间:2017-02-04 13:04:30

标签: c linux ubuntu gcc

我尝试编译以下代码,但我经常遇到此错误。

    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);

出现此错误:“错误:分配给具有数组类型的表达式”

1 个答案:

答案 0 :(得分:2)

您要将sprintf()的值赋给具有数组类型的变量。数组不是可修改的左值;所以你不能分配给他们。 sprintf()会返回int - 因此您需要将其值分配给int。不过,我建议您避免使用sprintf()并使用snprintf()。因为sprintf()容易出现缓冲区溢出。

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