-2

I am trying to give a value of char array field of a struct to an an element of another struct array.

struct Node {
   char template_id[6];
};
struct Node1
{
   char *template_id;
}
void main()
{ 
   Node1 *temp;
   temp_counter=0;
   temp=malloc(5*sizeof(Node1));
   temp[temp_counter].template_id=cur_node->template_id; //getting seq error here
} 

Tried the followings:

strcpy(temp[temp_counter].template_id,cur_node->template_id);
strncpy(temp[temp_counter].template_id,cur_node->template_id,6);

Still seq error. cur_node is initialized in different place and it is ok. Tried the following:

temp[temp_counter].template_id="hello"; // It works though
neilco
  • 7,964
  • 2
  • 36
  • 41
tryu hjkl
  • 145
  • 2
  • 2
  • 9
  • 2
    What's the value for cur_node, and where is it defined. – Preet Sangha Nov 04 '13 at 23:42
  • 2
    You have a comment: `//getting seq error here` We can't see what error you get, but if we did, we might figure out what you did wrong. Don't forget to tell us what you're trying to accomplish, what _actually_ happens, and what **YOU** expect to happen. – nos Nov 05 '13 at 00:02
  • Agreed, you need to show the error you're getting, and provide code to reproduce it. You say that "`cur_node` is initialized in different place and it is ok", but if you knew where the problem was, you wouldn't be asking here, so you should provide it. Voting to close until you provide enough information to answer your question. And `main()` returns an `int`. – Crowman Nov 05 '13 at 01:19

2 Answers2

0

Try type casting the allocated memory to the Node type. And verify if the memory is allocated using a NULL check.

temp=(Node1*) malloc(5*sizeof(Node1))
if (temp==NULL) exit (1)
Saravana Kumar
  • 816
  • 1
  • 12
  • 28
  • 3
    [Don't](http://stackoverflow.com/a/605858/249237) cast the result of `malloc` – Kninnug Nov 04 '13 at 23:55
  • Don't listen to Kninnug, he doesn't know what he's talking about. Type casting void pointers is a good practice. – Ralph Ritoch Nov 05 '13 at 00:15
  • Type casting is bad practice in C it seems. This [link](http://stackoverflow.com/a/7545394/207057) explanation is convincing. But if you wanna your code work with C++ you need type casting. – Saravana Kumar Nov 05 '13 at 00:35
  • 1
    Typecasting is a terrible idea. If it doesn't work, forcing it to go wrong with a typecast isn't going to solve anything. – Crowman Nov 05 '13 at 00:51
0

I guess that cur_node variable is not well defined. You should post its definition. Note, that when you use strcpy or strncpy you must ensure that the destination pointer points to a proper memory area which is capable of containing the string. For example, you can allocate this memory area by calling malloc.

However, if the cur_node is well defined, your code works in both cases.

#include <stdio.h>
#define SIZE_TEMPLATE 6 
struct Node {
char template_id[SIZE_TEMPLAtE];
};
struct Node1
{
char *template_id;
}; 

void main()
{ struct Node1 *temp;
  struct Node cur_node;
  int temp_counter=0;
  memset(&cur_node, 0, SIZE_TEMPLATE); 
  strncpy(cur_node.template_id, "HI", 2); 
  temp=malloc(5*sizeof( struct Node1));
  temp[temp_counter].template_id=cur_node.template_id;
  puts(temp[temp_counter].template_id);
  temp[temp_counter].template_id= malloc(SIZE_TEMPLATE* sizeof(char));
  strcpy(temp[temp_counter].template_id,cur_node.template_id);
  puts(temp[temp_counter].template_id);
} 

OutPut :

HI
HI
Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
  • Thank you.cur_node is instance of struct Node. template_id is char array with 6 elements and a field of Node.Actually in my code, Node and Node 1 have many other fields which are all double and I do not have any problem for them. To save some space and not confuse you, I just omitted them. – tryu hjkl Nov 05 '13 at 01:51
  • have you tried this solution? it should work – Giuseppe Pes Nov 05 '13 at 07:11
  • I tried the second version of your solution.And it works brilliant. Thank you, man.I am a new to C, and char*, char[], const char* are giving me a headache. – tryu hjkl Nov 05 '13 at 17:08