Speaking of being careful, you might want to review your use of strncpy. You could have some other goal in mind, but your usage seems very strange to me. Usually the safer strncpy (compared to strcpy) is used to prevent overflowing the destination buffer. In your case that is not happening because you're limiting the copy by the size of the source not the destination. strncpy requires the second parameter be null terminated and it will copy that null terminated string UP TO the count number of characters. It will pad the first parameter with nulls up to count characters (which may be what you're after- since that is the only difference between your use and using strcpy). BUT if strlen(string_data_x) is >= STRING_SIZE you will create a hard to find buffer overflow problem.
If you are just after preventing buffer overflow then the count parameter should be STRING_SIZE. BUT AGAIN, then the destination string will not be null terminated if you equal the size. If the destination has a null in the last character then using a COUNT of STRING_SIZE -1, gets you maximum safety and a null terminated destination string. [soapbox]Just another reason why I prefer writing code in C++, the C std lib is fraught with peril.[/soapbox]
@rsg sorry to dupe some of your info I didn't refresh before I hit submit.
Bit of help making struct global.
Re: Bit of help making struct global.
I hear ya tod, thanks for pointing it out. There are a couple things that are why it all seems strange though.
#1) the example i have is nothing like what i am actually doing, i was a quick post to keep it as basic as possible to get the real point accost.
#2) the actual read data has some crap at the back end of the string that i dont need. The strncpy is being used to strip off the end part of that string that i dont care about.
Sorry if my example cause more of a confusion. I have not used structs allot, mainly because it seems that there are so many ways to use them, along with so many people saying use it this way, and not that way. Yet you see people using both. The many C/C++ books i have dont really cover its useage in its entirety. so structs have remained a bit of a shady thing for me.
I like clear example of useage of things, such as if, while and when, not to many different ways you can write them
But a struct, jeez talk about a headache.
You got things like
struct first{
bla...bla...bla...
};
then...
struct first{
bla...bla...bla...
}last;
then...
struct first{
bla...bla...bla...
}last[MAX] = {bla...bla...bla...};
then
typedef struct{
bla...bla...bla...
}first;
there are more as well. after all this a guy is like WTH!!
#1) the example i have is nothing like what i am actually doing, i was a quick post to keep it as basic as possible to get the real point accost.
#2) the actual read data has some crap at the back end of the string that i dont need. The strncpy is being used to strip off the end part of that string that i dont care about.
Sorry if my example cause more of a confusion. I have not used structs allot, mainly because it seems that there are so many ways to use them, along with so many people saying use it this way, and not that way. Yet you see people using both. The many C/C++ books i have dont really cover its useage in its entirety. so structs have remained a bit of a shady thing for me.
I like clear example of useage of things, such as if, while and when, not to many different ways you can write them

But a struct, jeez talk about a headache.
You got things like
struct first{
bla...bla...bla...
};
then...
struct first{
bla...bla...bla...
}last;
then...
struct first{
bla...bla...bla...
}last[MAX] = {bla...bla...bla...};
then
typedef struct{
bla...bla...bla...
}first;
there are more as well. after all this a guy is like WTH!!
Re: Bit of help making struct global.
I think struct confusion may be more a result of poorly written books, than the language itself. A good book should talk about when and why to leave out the tag or structure_variable. It should also talk about arrays of structs, passing structs, pointers to structs, and if you're lucky why you might want to typedef a complicated struct.
Structs can really help to clean up your code. I don't write much C anymore but if I did and I had a function taking more than two parameters I would always look to see if I couldn't clean things up by encapsulating the parameters into a single struct. They are almost always related anyway.
FINALLY (and you may already know this but it's a mistake I see a lot) in case you ever need a struct's size (for something like memcpy() ) be sure to use sizeof(). Don't add up the length of the constituent parts. The compiler is allowed to (and often does) insert padding bytes.
Structs can really help to clean up your code. I don't write much C anymore but if I did and I had a function taking more than two parameters I would always look to see if I couldn't clean things up by encapsulating the parameters into a single struct. They are almost always related anyway.
FINALLY (and you may already know this but it's a mistake I see a lot) in case you ever need a struct's size (for something like memcpy() ) be sure to use sizeof(). Don't add up the length of the constituent parts. The compiler is allowed to (and often does) insert padding bytes.
Re: Bit of help making struct global.
I have seen that and it does clean things up.I don't write much C anymore but if I did and I had a function taking more than two parameters I would always look to see if I couldn't clean things up by encapsulating the parameters into a single struct.
I will have to force myself to take the time to learn it better. I am currently moving from C to C# in .netmf. I hope the class there explains it all better.
Re: Bit of help making struct global.
In C# structs are almost the same thing as classes. I seldom use them in C# as I usually want reference behavior.
Re: Bit of help making struct global.
No worries, tod - you were only amplifying my points, and fixing an oversight on my part! And I certainly agree with your points in this part:
Also this:I think struct confusion may be more a result of poorly written books, than the language itself. A good book should talk about when and why to leave out the tag or structure_variable. It should also talk about arrays of structs, passing structs, pointers to structs, and if you're lucky why you might want to typedef a complicated struct.
But on this:Structs can really help to clean up your code.
I would only add that this last is a guideline - there are plenty examples of when two should be three, four, or more, as well as when one is enough to warrant a struct; note that I am not disagreeing with you! And of course, if you are passing structs as parameters, you may well want to be passing pointers to structs, unless you need pass-by-value semantics; otherwise, you will suffer copy overhead... Of course, I may be preaching to the choir...I don't write much C anymore but if I did and I had a function taking more than two parameters I would always look to see if I couldn't clean things up by encapsulating the parameters into a single struct. They are almost always related anyway.