I am fairly new to C and have been having a lot of difficulty trying to copy the values of an array of pointers to strings. I have created a struct that contains such a pointer array (part of an implementation of a doubly linked list)
typedef struct Node
{
int value;
char *args[41];
....
} Node;
When I want to add a node, I have been using the following method
void addNew(char *args[], Node *current)
{
// first node is passed in, so loop until end of list is reached
while ((*current).next != NULL
current = (*current).next;
// create new node that is linked with the last node
(*current).next = (Node *)malloc(sizeof(Node));
((*current).next)).prev = current;
current = (*current).next;
// assign value to new node
(*current).value = some-new-value;
// allocate space for new argument array
(*current).args[41] = (char*)malloc(41 * sizeof(char*));
int i=0;
// loop through the arg array and copy each of the passed-in args into the node
for (i=0; i<41; i++)
strcpy((*current).args[i], args[i]);
(*current).next = NULL;
}
I think the root of my problem is in how I am allocating space for the pointers in the new node, but I haven't been able to figure out what I was doing wrong. As it stands, I get a Segmentation Fault (core dumped) as soon as the strcpy line is reached.
Any idea what Im doing wrong?