Reputation: 1532
I have a shared structure, and inside it a request 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];
};
Then I prepare a request;
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;
And the compiler warns me that I'm doing an incompatible implicit declaration in the strcpy function. I guess that's a problem with the pointers, but isn't what I wrote above supposed to be true?
Upvotes: 0
Views: 944
Reputation: 10529
Halo, you might be missing to include string.h as some of the other posters have said. Also, the string copying you are doing on that field will cause a buffer overflow if dir_path_name is larger than d_name.
I'd go out of a limb and suggest you do the following:
Using your code sample, it would go as follows:
/* dir_path_name should be null-terminated for this to work */
size_t path_len = strlen(dir_path_name);
char * dest = sdata_ptr->request_queue[index].d_name;
size_t n = strlen(dest);
if( path_len >= n )
{
... err out, no workie
}
memcpy(dest, dir_path_name, n );
if( path_len < n )
{
memset((dest+n),0,n-path_len);
}
Hope it helps.
Upvotes: 1
Reputation: 77400
"implicit declaration" warnings usually mean you didn't include the proper header, which, for strcpy
, is string.h.
Instead of strcpy
, you should use strlcpy
, which lets you limit the number of characters copied, preventing buffer overflow.
Upvotes: 3
Reputation: 273456
It doesn't appear to be a problem with the nested struct, at least without seeing more code (i.e. what is dir_path_name
).
This might be a long shot, but did you include string.h
for the compiler to see the declaration of strcpy
?
Upvotes: 2