[英]GCC compile error: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
[英]expect argument char in C with gcc
嗨,我在 C 中有這個腳本,我用 gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 編譯
但是回來
warning: format ‘%[0-9 ’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[1]’ [-Wformat=]
92 | sscanf(resultato,"%[0-9 ]",&SecondPart);
| ~~~~~^ ~~~~~~~~~~~
| | |
| | char (*)[1]
| char *
或者當不返回錯誤時(因為我修改了腳本)不返回任何腳本
RING_FUNC(ring_binapiceckservertime)
{
char Address[100] = {0};
const char *UrlSpec = "/time";
char *FirstPart[30] = {0};
char SecondPart[] = "";
size_t sz = strlen(cmDOriG) + strlen(UrlApi)+ strlen(UrlApiV3) + strlen(UrlSpec) + 1;
char destination[sz];
strcpy(destination, cmDOriG);
strcat(destination, UrlApi);
strcat(destination, UrlApiV3);
strcat(destination, UrlSpec);
//printf("%s\n", destination);
//int status = system(destination);
const char* mode = "r";
FILE *cmd=popen(destination, mode);
char result[50]={0x0};
//while (fgets(result, sizeof(result), cmd) !=NULL)
const char *resultato = fgets(result, sizeof(result), cmd);
// printf("%s\n", resultato);
sscanf(resultato,"%[0-9 ]",&SecondPart);
pclose(cmd);
//
printf("%s\n", &SecondPart[13]);
printf("value of b_static: %.*s\n", (int)sizeof(SecondPart), SecondPart);
}
如果我設置
printf("%s\n", &SecondPart); or printf("%s\n", SecondPart)```
not give me error but not return nothing
scanf
系列函數中的字符串的處理方式與其他數據類型不同。 您不需要傳遞指向它們的指針,因為它們已經是一個指針。 所以你只需要
sscanf(resultato,"%[0-9 ]",SecondPart);
但! 這是行不通的,因為 SecondPart 不足以容納除空字符串之外的任何內容,因為這是您分配的所有空間。
你需要像這樣指定它的大小......
char SecondPart[100];
...但這是假設輸入的最大字符串長度為 99 個字符。
由於您可以檢查resultato
的內容,因此更好的方法是計算您首先需要的字符串的長度。
char *SecondPart;
size_t length = strspn(resultato, "01234567890 ");
SecondPart = malloc(sizeof(char) * (length+1)); // Note sizeof(char) is pretty much guarenteed to be 1, but it makes the code clearer what you're allocating.
sscanf(resultato, "%[0-9 ]", SecondPart);
分配內存確實意味着在以后的某個時候您當然需要free
內存。
free(SecondPart);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.