简体   繁体   English

“错误:对具有数组类型的表达式的赋值”是什么意思?

[英]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" 出现此错误:“错误:分配给具有数组类型的表达式”

You are assigning the value of sprintf() to a variable which has array type. 您正在将sprintf()的值分配给具有数组类型的变量。 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 . sprintf()返回一个int ,因此您需要将其值分配给一个int However, I'd suggest to avoid sprintf() and use snprintf() instead. 但是,我建议避免使用sprintf()而应使用snprintf() Because sprintf() is prone to buffer overflow. 因为sprintf()容易出现缓冲区溢出。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM