Pinguino Forum
Strings in C - help - Printable Version

+- Pinguino Forum (
+-- Forum: Software (
+--- Forum: Syntax & Programs (
+--- Thread: Strings in C - help (/showthread.php?tid=3503)

Pages: 1 2 3 4

RE: Strings in C - help - pingotg - 12-05-2012

You need to be sure a null is there. I pointed out that specifically strncpy is defined NOT to add a null in the very situation you are using it.

The idea is that the "n" (64 here) is the max number of chars to copy, stopping at a null if one is found. Your code is such that no null will be in those 64, so strncpy finishes at 64 bytes.

I really urge you to read the doc for strncpy or indeed any function you use. I think people are expected to read a data sheet for a chip and similarly they should read the doc for a language and its library functions when they use them.

I think the issue here is that you're using a function in a particular way without knowing how it behaves and a language which you don't really know well enough. Most str functions are defined to add a null and their doc says when/if they will do so - and importantly some (such as strncpy) are expected to be used in a specific way but you're not doing so.

I didn't see strncpy detailed in your Arduino stuff but in any case this isn't Arduino.


RE: Strings in C - help - KiloOne - 12-05-2012

I am a person who reads datasheets and documentation more than anyone I know.

I did look at the documentation for strcpy() prior to ever using it, many times.

Here is what I got from a C++ online reference (I know we don't compile in C++ but I understood that basic functions were the same):
strcpy<cstring>char * strcpy ( char * destination, const char * source );Copy string
Copies the C string pointed by source into the array pointed by destination, including the terminating null character.

To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.

I took this to mean that it added the null, not just if you happened to include the last character of source in the copied characters.

I must be getting a null from somewhere because it has not hiccuped once yet.

But actually I did have to research elsewhere to find out that strcpy() can be used to copy partial strings. K&R did not really add to this.

Do you have a good online reference that could be helpful?


RE: Strings in C - help - KiloOne - 13-05-2012

(11-05-2012, 08:53 PM)moreno Wrote: Hi

I think this easy routine can solve the problem

CDCPrintAnyLength (char * Str)

int Len = strlen(Str);
int Ctn = 0;
char Buf;
   if ( (Ctn + 64) < Len)
     Buf = Str[Ctn+64];
     Str[Ctn+64] = 0;
     Str[Ctn+64] = Buf;
   Ctn += 64;
while (Ctn < Len);

I have not tested it but I think can work without problems.

Bye Bye, Moreno

I have tested this and it works for me.Big Grin

Gone with my crazy way of doing it.

Thanks Moreno!


RE: Strings in C - help - pingotg - 13-05-2012

1. C++ is a different language so ignore it (the name is misleading and IMO stupid)
2. you're using strncpy not strcpy so read the right one Smile

It's up to you, use it right or not. I posted the fix.


RE: Strings in C - help - bjoernp - 15-05-2012

Dale, what John wants to bring forward here is more than just a optional good practice. You should not take that too lightly.

Strings in C are arrays of characters with a terminating zero (null-character). Without the null, it's not a string but just an array of characters (which can be perfectly fine, if you want just that).

What can be a little confusing here is the fact, that this difference really is only in the semantics (as you already said before) and little more. The C-compiler won't care, it treats arrays of char (or pointers to char) and strings just alike (except for initialization, where it usually adds the trailing zero), but all standard functions dealing with c-strings require these semantics. So you really should care about that zero just to make your life a little easier. Smile
This means, that a pointer-to-char can actually mean several types. As with any pointer, it will be a pointer to a single char element and this element can either stand alone or the pointer can mark the location into an array of them (starting index after initialization). Adding to that, a char[] or char* can be (or point to) a plain array or them or a string following the null-terminated string semantics.
That said, it's still possible to treat a non terminated string like a normal one, using only the *n* variants of the string functions (which are generally safer and preferable anyway). This seems to be the road you were going here. There may be occasions, where this is necessary (hitting size constraints in flash i.e.), but usually this is not the way to go.

RE: Strings in C - help - KiloOne - 17-05-2012

Thanks all,

From now on I will treat my strings with respect and make sure that everyone but the leader has someone beside them, even if it is nothing! Smile