2017-02-04 60 views
0

我試着編譯下面的代碼,但是我經常得到這個錯誤。「錯誤:賦值給數組類型」是什麼意思?

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

出現此錯誤:「錯誤:賦值給表達式與陣列型」

+0

這意味着你試圖分配給一個數組,這是不可能的。並且['sprintf'(和相關函數)](http://en.cppreference.com/w/c/io/fprintf)不會返回任何字符串。你不需要那個。而你應該真的使用'snprintf'來代替。 –

+0

,因爲在LHS中,您使用的數組類型不可分配。 – 2017-02-04 13:08:22

回答

2

您的sprintf()的值分配給具有陣列類型的變量。數組不是可修改的左值;所以你不能分配給他們。 sprintf()返回int - 因此您需要將其值分配給int。但是,我建議避免sprintf()並使用snprintf()來代替。因爲sprintf()容易發生緩衝區溢出。

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