*i am trying to copy the value of

ar2[10] //in main. has the value 1,2,3,4,5

in

ar[10]  //(in struct node). empty array

so to copy the value iam not sure if i could use a strcpy for ex,

strcpy(head->ar, ar2);

//here is my code
struct node
{
int ar[10]; //empty array
struct node *next;
};
struct node *head;

void insert_list1(int[]);

int main()
{
  int ar2[10]; //let just say ar2 has the value 1,2,3,4,5
  insert_list1(ar2);
}


void insert_list1(int ar2[])
{
  struct node *cur_node = head;
  struct node *new_node;


  if(cur_node == NULL)
    {
      head = (struct node *) malloc(sizeof(struct node));
      if(head == NULL)
    {
      printf("Node allocation failed.\n"); fflush(stdout);
      exit(1);
    }
      strcpy(head->ar, ar2);                     //---NEED HELP WITH THIS LINE--------------
      head->next = NULL;
    }
  else
    {
      while(cur_node != NULL)
    {
      cur_node = cur_node->next;
    }
      new_node = (struct node *)malloc(sizeof(struct node));
      if(new_node == NULL)
    {
      printf("Node allocation failed.\n"); fflush(stdout);
      exit(1);
    }
      strcpy(new_node->ar, ar2);
      new_node->next = NULL;
      cur_node->next = new_node;
    }
}

Recommended Answers

All 8 Replies

you can not use strspy() as it is used to copy strings not integers or arrays!!

I c. so if i repace strcpy with a for loop for ex,

for(i = 0; i < 10; i++)
{
  new_node->ar[i] =ar2[i];
}

will that copy data from ar2 to ar?

yea!! that'll work fine... this is the only way you can copy an array..!!

oh on line 41, i am getting a segmentation fault(core dumped). do you know if i am conneting the nodes right? to me it looks fine, may be iam missing some thing.

sorry!! i don't know much about it! I'm a starter in C.. But i guess, after copying, cur_node and new_node become same isn't it???

@Vish0203

after copying, cur_node and new_node become same isn't it???

not the current node but it's next node, and not really "same", its only used to allocate a memory for the next node

@hwoarang69

@line 29

while(cur_node != NULL)

make that

while(cur_node->next != NULL)

otherwise you're iterating to an empty/non existing node and assigning a node to it's next node instead of itself which isn't good,instead you should check if the next node is empty then allocate memory to the next node like what iv'e stated

oh right forgot to put ->next. thanks alot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.