Pelles C forum

C language => Beginner questions => Topic started by: EdPellesC99 on April 12, 2010, 05:18:23 PM

Title: Null terminator.
Post by: EdPellesC99 on April 12, 2010, 05:18:23 PM
 
  Hi,

  I am trying to study the SafeString functions, and use them. .... Using #include <strsafe.h>, and strsafe.lib.

  Here and there I read the functions deprecated are not guaranteed to be null terminated. I have read you must verify it.

  I have tried to understand pointers, and arrays etc. My background is scripting. C++ info is everywhere, but there is not
the concern for buffer over-run possibilities.

  So I am trying to understand things from the bottom up (and I do mean bottom!) So I must ask what will no doubt seem like a dumb question.

  The null terminator is always refered to as '\0'.

   Code clip (used in console program),
char strsrc[20]="this is a test";
strsrc[sizeof(strsrc)-1] = '\0'; /* ensure null terminated */


   With defaults, after "test", I have '0', '0', '0', etc until the 20th array cell is filled.

   One question is  (concerned with copying and concantenation operations): Do I have to worry about having more than one Null terminator?

   I have looked in various books, and looked on the net. I am unsure.

   The Null terminator is represented as '\0', and it is the final '0': Is that '0' any different than a previous '0' in the array?

  I have also read that quoted strings are by definition null terminated, but I have never read  .....if it is guaranteed.

  Only C has the high level of concern for buffer overrun (by function operations that do not guarantee it), so I am trying to really understand how I can write Safe string operations.

  Thanks in advance,  ...........any book or link suggestions to help my brain would also be appreciated.

  ... Ed

 


Title: Re: Null terminator.
Post by: JohnF on April 13, 2010, 11:09:47 AM
char strsrc[20]="this is a test";

strsrc[] is guaranteed to be NULL terminated so there is no need to use the next line.

strsrc[sizeof(strsrc)-1] = '\0'; /* ensure null terminated */


One question is (concerned with copying and concantenation operations): Do I have to worry about having more than one Null terminator?


No, a function that concats strings looks after things for you. Do a test and see!

Null terminator is represented as '\0', and it is final '0': Is that '0' any different than a previous '0' in array?


If you have a C string the first NULL terminates the string, any characters after this terminator are technically not part of string.

I have also read that quoted strings are by definition null terminated, but I have never read .....if it is guaranteed.


Guaranteed.

EDIT: You might find the C-FAQ useful.

http://c-faq.com/ (http://c-faq.com/)

John
Title: Re: Null terminator.
Post by: EdPellesC99 on April 20, 2010, 04:56:18 PM
   Thanks for the reply John. (I did not have my reply notification set.)

I got on the forum to post this topic:

http://forum.pellesc.de/index.php?topic=3160.0 (http://forum.pellesc.de/index.php?topic=3160.0)  and I saw your response.

 If you look at it and have any comments, please leave a reply there. I could have helped myself some by reading your response.  :-)  

 Thanks for the link, I had found it, but I will explore it some more!

 Best Regards, Ed