ambuli
asked on
copying strings to a structure field
Hi Experts,
I have the following in the .h file
typedef struct
{
char *name;
char *value;
} my_table_t;
void functionOne(my_table_t *table);
And I am doing something like below to copy some data. I want to know if this valid or should I use strcpy to copy the values.
my_table_t table;
table.name = "nameOne";
table.value = "valueOne";
functionOne(&table);
I have the following in the .h file
typedef struct
{
char *name;
char *value;
} my_table_t;
void functionOne(my_table_t *table);
And I am doing something like below to copy some data. I want to know if this valid or should I use strcpy to copy the values.
my_table_t table;
table.name = "nameOne";
table.value = "valueOne";
functionOne(&table);
ASKER
functionOne is basically used for getting the xml message representing this C structure. So, I am using libXML2 to get the xml messages.
So, it is used something like below.
string_field = xmlNewChild(new_tuple, NULL, "StringField", NULL);
xmlNewChild(string_field, NULL, "Name", "nameOne");
xmlNewChild(string_field, NULL, "Value", table->name);
What I am trying to debug is that I am getting the following error, possibly from libXml2. So, I am trying to see if my copying is incorrect.
xmlEscapeEntities : char out of range
So, it is used something like below.
string_field = xmlNewChild(new_tuple, NULL, "StringField", NULL);
xmlNewChild(string_field, NULL, "Name", "nameOne");
xmlNewChild(string_field, NULL, "Value", table->name);
What I am trying to debug is that I am getting the following error, possibly from libXml2. So, I am trying to see if my copying is incorrect.
xmlEscapeEntities : char out of range
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It appears that
when I do this,
table->name contains some trailing bytes( garbage characters...)
A bit more details.
I am getting the value from SPI_getvalue( ) from postgres database, which returns char *.
So,
table->name = SPI_getvalue();
functionOne(&table);
...
In functionOne( )
xmlNewChild(string_field, NULL, "Value", table->name);
when I do this,
table->name contains some trailing bytes( garbage characters...)
A bit more details.
I am getting the value from SPI_getvalue( ) from postgres database, which returns char *.
So,
table->name = SPI_getvalue();
functionOne(&table);
...
In functionOne( )
xmlNewChild(string_field, NULL, "Value", table->name);
It would probably be best of you just posted the code here. Unfortunately, what the code codes and what you describe may be orthogonal. With code to view we can try to diagnose the issue rather than conjecture based upon the symptoms you describe.
-Rx.
-Rx.
s/what the code codes/what the code does
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> You need to allocate memory for these variables, just assigning pointer values does not guarantee the validity of the data
"The result is returned in memory allocated using palloc. (You can use pfree to release the memory when you don't need it anymore.) "
http://www.postgresql.org/docs/8.2/static/spi-spi-getvalue.html
"The result is returned in memory allocated using palloc. (You can use pfree to release the memory when you don't need it anymore.) "
http://www.postgresql.org/docs/8.2/static/spi-spi-getvalue.html
ASKER
Sorry, I was tied up with something else and couldn't comment on this. The code is pretty much identical to what I described. It is a larger program, and I wasn't able to simplify that to post here. Anyway, thanks for all your inputs.
In particular, what might happen if you tried to write to *(table.name) or *(table.value) would be undefined.