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);


ambuliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
It can be valid, depending on what you want to do with functionOne
In particular, what might happen if you tried to write to *(table.name) or *(table.value) would be undefined.
0
ambuliAuthor Commented:
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
0
ozoCommented:
If you can pass "nameOne" to it, it should be equally valid to pass
table->name = "nameOne" to it.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

ambuliAuthor Commented:
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);
0
evilrixSenior Software Engineer (Avast)Commented:
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.
0
evilrixSenior Software Engineer (Avast)Commented:
s/what the code codes/what the code does
0
evilrixSenior Software Engineer (Avast)Commented:
Incidentally, the documentation for SPO_getvalue documents two failure conditions. Do you check for both of these before attempting to pass the result to your XML function?

"Column value, or NULL if the column is null, colnumber is out of range (SPI_result is set to SPI_ERROR_NOATTRIBUTE), or no no output function available (SPI_result is set to SPI_ERROR_NOOUTFUNC). "


// Error checking, something like...
table->name = SPI_getvalue(...);
if((SPI_ERROR_NOATTRIBUTE != SPI_result) && (SPI_ERROR_NOOUTFUNC != SPI_result))
{
	functionOne(&table);;
}
else
{
	// Handle error
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
You need to allocate memory for these variables, just assigning pointer values does not guarantee the validity of the data - these pointers may not necessarily point to valid memory locations later. A

#include <string.h>

table->name = strdup(SPI_getvalue());

or

table->name = _strdup(SPI_getvalue());

might help, don't forget to 'free()' the memory later.

(see also http://www.globalyzer.com/gi/help/gihelp/unsafeMethod/_strdup.htm)
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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
0
ambuliAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.