打印字符串数组的内容
printng the content of an array of strings
我正在编写一些简单的字符串相关代码(我是这方面的初学者),当我执行这段代码时,我收到一条我不明白的警告。这是代码。
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#define Extension ".txt"
#define LOG_MIN_FILENAME_SIZE sizeof(double) + sizeof(Extension) + 2
char* buffer[LOG_MIN_FILENAME_SIZE];
int timez = 0;
int minutes = 0;
int main()
{
char _acBuff[LOG_MIN_FILENAME_SIZE];
char* ListOfFiles[14];
for(int i=0; i<14; i++){
sprintf(_acBuff, "%d" "%d"Extension, timez, minutes);
ListOfFiles[i]= _acBuff;
}
for(int i=0; i<14; i++){
sprintf(buffer, "%s", ListOfFiles[i]);
printf("%s", buffer);}
}
这是警告:
warning: Format "%s" expects Arguments of type char* but Argument 2 has type "char**"
据我所知,我使用了正确的格式说明符,那么问题到底是什么?
你想要这个:
#include <stdio.h>
#include <stdlib.h> // needed for malloc
#include <string.h> // needed for strcpy
#define Extension ".txt"
#define LOG_MIN_FILENAME_SIZE sizeof(double) + sizeof(Extension) + 2
char buffer[LOG_MIN_FILENAME_SIZE]; // you want an array of char, not an array of
// pointers to char
int timez = 0;
int minutes = 0;
int main()
{
char _acBuff[LOG_MIN_FILENAME_SIZE];
char* ListOfFiles[14];
for (int i = 0; i < 14; i++) {
sprintf(_acBuff, "%d" "%d"Extension, timez, minutes);
ListOfFiles[i] = malloc(strlen(_acBuff) + 1); // allocate memory for the string
strcpy(ListOfFiles[i], _acBuff); // copy the string
// your code only copies the same
// pointer over and over
}
for (int i = 0; i < 14; i++) {
sprintf(buffer, "%s", ListOfFiles[i]);
printf("%s\n", buffer); // added a \n, so output is readable
}
}
免责声明:
- 为简洁起见,没有任何错误检查
- 分配的内存未明确释放
sizeof(double)
在这里仍然是错误的,但没有任何后果。你应该自己找出原因。
我正在编写一些简单的字符串相关代码(我是这方面的初学者),当我执行这段代码时,我收到一条我不明白的警告。这是代码。
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#define Extension ".txt"
#define LOG_MIN_FILENAME_SIZE sizeof(double) + sizeof(Extension) + 2
char* buffer[LOG_MIN_FILENAME_SIZE];
int timez = 0;
int minutes = 0;
int main()
{
char _acBuff[LOG_MIN_FILENAME_SIZE];
char* ListOfFiles[14];
for(int i=0; i<14; i++){
sprintf(_acBuff, "%d" "%d"Extension, timez, minutes);
ListOfFiles[i]= _acBuff;
}
for(int i=0; i<14; i++){
sprintf(buffer, "%s", ListOfFiles[i]);
printf("%s", buffer);}
}
这是警告:
warning: Format "%s" expects Arguments of type char* but Argument 2 has type "char**"
据我所知,我使用了正确的格式说明符,那么问题到底是什么?
你想要这个:
#include <stdio.h>
#include <stdlib.h> // needed for malloc
#include <string.h> // needed for strcpy
#define Extension ".txt"
#define LOG_MIN_FILENAME_SIZE sizeof(double) + sizeof(Extension) + 2
char buffer[LOG_MIN_FILENAME_SIZE]; // you want an array of char, not an array of
// pointers to char
int timez = 0;
int minutes = 0;
int main()
{
char _acBuff[LOG_MIN_FILENAME_SIZE];
char* ListOfFiles[14];
for (int i = 0; i < 14; i++) {
sprintf(_acBuff, "%d" "%d"Extension, timez, minutes);
ListOfFiles[i] = malloc(strlen(_acBuff) + 1); // allocate memory for the string
strcpy(ListOfFiles[i], _acBuff); // copy the string
// your code only copies the same
// pointer over and over
}
for (int i = 0; i < 14; i++) {
sprintf(buffer, "%s", ListOfFiles[i]);
printf("%s\n", buffer); // added a \n, so output is readable
}
}
免责声明:
- 为简洁起见,没有任何错误检查
- 分配的内存未明确释放
sizeof(double)
在这里仍然是错误的,但没有任何后果。你应该自己找出原因。