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; = "nameOne";
table.value = "valueOne";


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.

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


table->name = SPI_getvalue();


In functionOne( )

xmlNewChild(string_field, NULL, "Value", table->name);
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.

evilrixSenior Software Engineer (Avast)Commented:
s/what the code codes/what the code does
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(...);
	// Handle error

Open in new window


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


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

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

(see also
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.) "
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.
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

From novice to tech pro — start learning today.