C嵌套结构指针问题

标签 c pointers structure

我有一个共享结构,里面有一个请求结构:

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/

相关文章:

c - 用于存储字符串的哈希表实现

c - C中多管道和命令执行的实现

c - if语句检查里面是否有一个char指针是做什么的

c++ - 在结构 C++ 之间传递数组

javascript - 从数组中删除随机字符串,JavaScript

c++ - 取消引用结构以获取第一个成员的值

c++ - 如何使用C/C++读取目录的文件索引?

c - 框模糊边缘遇到问题

c - 处理堆栈变量的指针示例

c - 指向结构指针的指针