NO

Author Topic: "Finding Null terminator(s) in a string" C Language. Source compiles in PellesC  (Read 4114 times)

EdPellesC99

  • Guest
Code: [Select]
#include <stdio.h>

///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///

/* This program is about "Finding Null terminator(s) in a string.". */

///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///\\\///

/* Compile as Simple Console application, in PellesC.*/

// // ~ I will declare a somewhat strange string, in order to prove things to myself, since as a learner in C,  I have both a hard time finding information, and then  understanding it completely.......when I do.

//********************************************************************************************************
// I have been trying to understand Safe String function use, and why it is necessary, and for starters why Null terminators are involved and how a bad null terminator situation leads to buffer over runs.
//********************************************************************************************************


// // ~ Sometimes I think beginners do know some things, they are experts at knowing what is confusing !  And given enough spare time (once they understand it) they might be able to better explain a concept to someone still confused !

// // ~ Pelles C, with it's solid and non-bloated IDE, has helped me enormously start to learn C.
/*
I am thankful to Pelle for his fine work on the PellesC IDE !

It installs without problems, it runs perfectly, it loads an instance QUICKLY for me (on my slower computer) compared to say
CodeBlocks (which is best (for me) in C++).

I want to try to give a little back, (when time permits) in the form of a "Complete Program that Compiles in PellesC", one someone is guaranteed to be able to copy, and compile.   ..... and maybe understand something ....that they too, have had a hard time collecting the info on, and understanding it.

I myself find the information at MSDN enough to make me want to pull my hair out sometimes (Ok, almost all the time). Why in the world they cannot, add a program e.g. that will compile, I do not know (except they would HAVE to make it for Visual C++ Studio. which is Soo unnecessarily complex for a beginner -- it is ridiculous ..... and the latest versions may not even load up on a computer that is older(and maybe has problems- like mine.... ! ) Pelles C is great for the learner, and great after that !
*/

// // ~ Start •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •

void seeMyArray(char *myArray);
void LookForNull( char *str, int size );

// // ~ Modifying my last name: Grossheim a bit, for my string declaration.

char strsrc[20]={'G', 'r', '\0', 's', '0', 'h', 'e', 'i', '\0'};

// Nine characters, filling cells strsrc[0] thru strsrc[8].......note I have a Null in strsrc[2], and strsrc[8], and a zero in array subscript strsrc[4], .........in many places I read strsrc[9] thru strsrc[19] are filled in with zeros (but not what I found for myself).

char msg1[] = "My array \"strsrc\" is declared like this:\nchar strsrc[20]={'G','r','\\0','s','0','h','e','i','\\0'};\n\nI declare it as an array of 20 cells. There are 9 characters,\nchar #3, is a Null Terminator, char #5 is literal Zero.\n\nCells filled will be strsrc[0] thru strsrc[8], and by default the balance\nof the string is filled out with zeros(cells 13 thru 19).\n\nBy default the quoted string is Null Capped, so cell 19 must be a\nNull Terminator.\n";
char msg2[] = "Note: I cannot prove to myself the Null terminator is there.\n\nWhere I expect a Null terminator to be, I print a value of '0'.\nAnd I get this value for each format specifier, however I get a blank\nfor the %c specifier.\n\nI notice the Zero is identified as ascii 48, as it should be, so there is\nsomething going on in the null positions besides zero.\n\n=============================================================================\n\nNow I will use my LookForNull function to see where the Null is.\n\nHit Rtn to Continue.";
char msg3[] = "    (See Above) Well, I found out a few things:\n\nA Zero is formatted out as %c is Ascii 48, whereas a Null is different:\nformated as %c it is a blank ! NOT a capital letter O, and not a Zero\n(a strange bird) !\n\n(I have read: \"the Null is '0' in ascii, and 000 in hexadecimal\".)\n\nExtra array cells are filled out with Nulls and NOT zeros as I have read somewhere.\n\nYou can find yourself with more than one Null in a String, \n\nLast, the final Null is no different than other Nulls in my string.\n============Finished==========================================================\nHit Rtn to Close";

int main(void)
{
seeMyArray(strsrc);
getchar();
LookForNull( strsrc, 20 );

printf("\n%s", msg3);

getchar();
return 0;
}
// // ~ What do I want to do?
// // ~ I want to loop through my Array and print the contents of each cell to the console right thru to last cell.
// // ~ Why? I want to be able to verify .... where the null terminator is, and whether there are more than one.

// // ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •
void seeMyArray(char *myArray)
{
printf("%s\nHit Rtn to Continue.\n===========================================================================", msg1);
getchar();

int i;
for ( i = 0; i < 20; i++ )
{
// // ~ Looking at each cell as a character.
printf("value at strsrc[%d] as a character is:  %c\n ", i, strsrc[i]);

// // ~ Looking at each cell in ascii.
printf("value at strsrc[%d] is in ascii:  %d\n ", i, strsrc[i]);

// // ~ Looking at each cell in hexadecimal.
printf("value at strsrc[%d] is in hexadecimal:  %x\n ", i, strsrc[i]);

// // ~ Looking at each cell in Octal.
printf("value at strsrc[%d] is, in Octal:  %o\n\n\n", i, strsrc[i]);
}
printf("%s\n", msg2);

}
// // ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •


// // ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •
void LookForNull( char *str, int size )
{
int i;
for (  i = 0; i < size; ++i )
{
if ( str[i] == '\0' )
{
printf( "Null Terminator is cell No. %d of this array.\n", i );
}
if ( i == size )
{
printf("You have reached the last cell. strsrc[%d].\n", i );
}
}
}
// // ~ •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •  •

// Hope someone finds this useful.