Apr 25, 2009

What are C-Style Strings (C-Strings)?

C-Style strings form a definitive part in majority of interview coding questions so I thought I should learn myself and add it to the page. I tried to ignore the C-Style string area but finally I am yielding to it ;) To start with, a C-Style string ('string' henceforth) is actually an array of characters terminated by null character ('\0') to mark the end of string.


For eg. "Interview" is actually an array of characters represented by:

[I] [n] [t] [e] [r] [v] [i] [e] [w] [\0]


The length of this string is therefore actually '10' so if we happen to copy this string in a character array we will need to allocate space for 10 characters. However, the library function 'strlen' returns the length of string excluding the terminating null character, thus in this case 'strlen' will return '9'.


A string is usually passed around and manipulated as a 'pointer to character (char*)' or an 'array of characters'; which are actually the same as name of array means a constant pointer pointing to the first element of that array.


Now let's write a function to print a simple string:

void PrintStr(char* str)
{
std::cout<<"\n String Passed: " <<>

}


int main()

{

char* chPtr = “Interview”;

PrintStr(chPtr);
}

This function correctly prints value of 'str'. But there is a problem in 'PrintStr' function, consider the 'main()' below:

int main()

{

char chArr[3] = {'A', 'B', 'C'};

PrintStr(chArr);
}


This compiles fine and executes well but have a look at the output:



So we just learnt a lesson that before using a C-Style string pointer it must be made sure that it is properly null terminated. A way to do this is to change the function signature to include length of string pointed to by the pointer so we may use only the memory said to be valid by length.

Why am I starting-up with this blog

So here comes my first post for this blog. After a bright winter, we eventually have a very pleasant warm Spring day today here in upstate NY, however, I couldn't keep myself from writing my first post.

I put-up my personal webpage (doesn't exist anymore) last summer so I could start a nice discussion hovering around Object Oriented Programming technologies. That couldn't go well as I lacked making efforts to regularly keep posting new articles. Then I started working full-time and one day, really don't know how but, landed at a nice blog that talked about personal financing tips. I liked the idea so much that I still keep up with tips to save money and how to always keep multiplying it. It didn't take much time for me to then start reading numerous personal finance advicing blogs and I became all indulged in the money matters ;)

After graduating from school I didn't get much chance to discuss technology issues as I used to get back then. So again the same very idea of discussion boards, blogs, personal webpages, ..... started lurking around me. For the past few days I was thinking should I really blog about what I always wanted to talk about, and guess what, that's what blogging is all about :)

So here I am, being curious, planning to do and communicate a lot through this great channel. When I think of where I want to see Let's Be Curious in one year I could think of regularly posted technical tit-bits concerning basics of OOP, specifically C++, no high-funda logics and conceptual nightmares but simple basics which I like myself to be introduced to again and again and again....

I will also try to continuoulsy post articles related to personal money management, savings, frugality, budgeting tips from time to time. Also recent graduates (begineer's) job postings for starters because I struggled a lot finding this job and now I am watching my friends do even more. And finally if I could keep upto it, interesting deals available :)

So here with this first post, I plan to start a wonderful journey of sharing and learning ... hope to see you back here again ... please leave me a comment what you think of the blog and things discussed here ;)