I'm self-teaching myself C with a complete reference book. Unfortunately that means that the author assumes that the reader is familiar with fundamental concepts and doesn't bother commenting the code. Here is an unnecessarily obfuscated code excerpt demonstrating "the use of return statements" along with my attempt to understand what is going on. Please list any corrections.

``````int find_ substr( char *s1, char *s2)
{
register int t;
char *p, *p2;
for( t= 0; s1[ t]; t++) {
p = &s1[ t];
p2 = s2;
while(* p2 && *p2==* p) {
p++;
p2++;
}
if(!* p2) return t; /* substring was found */
}
}``````

So pointers s1 and s2 are passed into "find substring." s2 is the substring we are searching for and s1 is the string we are searching through. As the function cycles through each character of p (equivalent to s1) the letters are compared to p2. When a letter match is found, the while loop checks for a complete match. (ie: you might be searching for digging but you only find dig. If that happens the orange statement is false and we're back at the for loop) If there is a complete match, p2 (the string we're searching through) is finite, the pointer arithmetic goes out of bounds and null is returned, bringing you out of the loop and into the for. The pointer p2 is still out of pounds and the if statement evaluates to false, but the ! changes this to true so t is returned. If the substring is not in the string we are searching through, the while and for loops exits and the -1 is returned.

One statement that confuses me in particular is the loop condition in

``for( t= 0; s1[ t]; t++)``

Does this cycle through every element of any array? What happens if one of your elements happens to have a value of 0? will you break out of the loop or does it continue on until you are out of bounds and returns null? Since there is no bounds checking in C, the compiler will not stop you from writing more elements than allocated at the risk of overwriting other memory, is it possible that the memory cells wlil be contiguous and maybe you will continue looping past the 'last' element of the array?

You've mostly got it.

Each C 'string' ends in a character value of 0. That's what your "out of bounds" is really checking, really it's "end of string.

Commented (and formatted properly)
[code]int find_ substr( char s1, char s2)
{
register int t;
char p, p2;
for( t= 0; s1[t]; …

For absolute beginners, i would recommend "C++ in 21 days" and if you want to move ahead from the beginner stuff i would recommend "C++ programming language".

Hope it helped, bye.

## All 9 Replies

You've mostly got it.

Each C 'string' ends in a character value of 0. That's what your "out of bounds" is really checking, really it's "end of string.

Commented (and formatted properly)

``````int find_ substr( char *s1, char *s2)
{
register int t;
char *p, *p2;
for( t= 0; s1[t]; t++)  // Move character by character thru the source string
// start at the first character t=0 and s1[t]
// until the current character s1[t] is 0 (FALSE)
{
p = &s1[t];         // p becomes the address of the current character
p2 = s2;            // p2 becomes the search string

// Following tests to see if the current position in the source string and the seach string
// matches...
while (*p2  &&      // Continue while *p2 is not 0 (TRUE) -- end of string not found
*p2 == *p)   //            and source and search characters are identical
{
p2++;           //    in both strings
}
if (!*p2) return t; // out of loop -- if the current character in the seach string
//    is 0, we have a match.  Return the substript of the start
//    of the sub-string
}
}``````

Are you talking about Herbert Schildt's C - The Complete Reference?

Yes that is the book I am using. I am going for the very thorough approach (ruling out all the "how to learn to program in 10 days") The first couple hundred pages were manageable but I find the example code to be very difficult to understand simply because Schildt uses code and concepts that aren't covered until much later in the book. I would of also liked to have seen some sort of exercises with solutions. There is a lot of material to digest so I'll probabbly end up rereading the entire text anyways. I will undoubately run into many more examples which I won't fully understand but that's why I come here.:cheesy:

That being said, I find the book to be fairly well written, it was just never intended for newbies such as myself. Experienced programmers in other languages, programmers who are moving on from C to C++, or programmers who just need to review all the features of C and C++ would find Schildt's book useful. If any of you are interested I would definately recommend the electronic version over the text though. At over 1000 pages I can only imagine how unwieldly the text must be.... The search function is invaluable.

I guess you didn't read the link which I gave. Get a better book. Don't let me point out the stupid mistakes of the author.

I guess you didn't read the link which I gave. Get a better book. Don't let me point out the stupid mistakes of the author.

Damn.

Do you have any recommendations or should I pick a text from one of the stickies?

I'm actually not sure whether I should start with C, jump into C++ or bother learning the language at all. (mainly because of the massive time commitment)

I know I will be learning C++ for the next four years in university. But I also know you shouldn't blindly accept what school teaches you as Gospel. Countless articles on reddit talk are about how Java sucks, schools that teach it put out mediocre hackers, etc. etc.

I don't know how many programs are still being written in C, but for all I know C could become archaic by the time I graduate.... As useful and powerful as C is, (Lisp too is I'm told, but good luck finding a job requiring Lisp coders) I guess I'm lucky this time in that I only wasted two weeks on a bad book, but I don't want to find out two years into my program that I've run in the wrong direction. Any tips or advice?

For absolute beginners, i would recommend "C++ in 21 days" and if you want to move ahead from the beginner stuff i would recommend "C++ programming language".

Hope it helped, bye.

I'm actually not sure whether I should start with C, jump into C++ or bother learning the language at all. (mainly because of the massive time commitment)

I know I will be learning C++ for the next four years in university. But I also know you shouldn't blindly accept what school teaches you as Gospel. Countless articles on reddit talk are about how Java sucks, schools that teach it put out mediocre hackers, etc. etc.

If you're considering learning C purely as a grounding for C++, then please save yourself the wasted effort and skip 'C'. the two languages sometimes look similar at first glance (C++ inherited alot of the C syntax), but they are fundamentally completely different languages - even the "Hello World" programs you learn to write on day 1 for each language are completely different. (You've probably already written a "Hello World" in C, so here's a quick example in C++ to contrast)

``````#include <iostream>
using namespace std;

int main()
{
cout << "Hello, World!";
}``````

I'd like to backup Grunt's reccomendations for Bruce Eckel's Thinking in C++, and Accelerated C++ by Koenig & Moo. Both excellent beginner's books. Also, C++ Primer by Lajoe, Lippman & Moo comes highly reccomended for beginners.

Francis Glassborow also has a book written for people with no prior programming or computer science knowledge, called You Can Do It. that's also an excellent book, but the compiler & libraries included with the book only work on Windows. (So learning from the book on any other OS would be tricky)

Thank you for all the recommendations. I've started on Bruce Eckel's "Thinking in C" and skimmed through "Thinking in C++"

Both are available on www.mindview.net and can be downloaded free of charge, though a hardcopy of the latter can be purchased.

"Thinking in C" is a visual-audio presentation that explores the basic features of C in preparation of learning Java and/or C++. I'm still working through the chapters and I'll share my impressions but at the moment I have nothing but good things to say.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.