我有一个共享结构,里面有一个请求结构:
struct shared_data {
pthread_mutex_t th_mutex_queue;
struct request_queue {
int min;
int max;
char d_name[DIR_SIZE];
pid_t pid;
int t_index;
} request_queue[BUFSIZE];
int count;
int data_buffer_allocation[BUFSIZE];
int data_buffers[BUFSIZE][100];
};
然后我准备一个请求;
struct shared_data *sdata_ptr;
...
...
sdata_ptr->request_queue[index].pid = pid;
strcpy(sdata_ptr->request_queue[index].d_name, dir_path_name);
sdata_ptr->request_queue[index].min = min;
sdata_ptr->request_queue[index].max = max;
并且编译器警告我,我在 strcpy 函数中进行了不兼容的隐式声明。我想这是指针的问题,但我上面写的不应该是真的吗?
最佳答案
“隐式声明”警告通常意味着您没有包含正确的 header ,对于 strcpy
,它是 string.h。
您应该使用 strlcpy
而不是 strcpy
,它可以限制复制的字符数,防止缓冲区溢出。
关于C嵌套结构指针问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2571109/