[KLUG Programming] substr in C?
Bruce Smith
programming@kalamazoolinux.org
21 Jul 2003 14:22:58 -0400
> > > Rock on! That is great! Thanks for the clue!
> >
> > If I was SCO, I'd sue you for using my code in your program. ;-)
>
> I don't know that I will use it exactly as it was written, but I definitely
> grasped the concept from your example and can now hopefully write something
> that doesn't generate a fatal error. :)
Cool!
> And besides, you're too nice of a dude to impersonate a SCO!
I've got Tony fooled! ;->
> Actually, you'll be able to see how I accomplish this when I post the code for
> peer review upon completion. (No NDA required. :) It's the same program I was
> talking about in the recent thread about sorting, uniq'ing, etc. There are
> other school admin types on the klug list, so maybe this will be of use to
> them (probably with modifications) when it is done.
Great!
BTW, please don't attach large programs to messages sent to KLUG lists.
(like your finished program) Post URL's to download your code instead.
Small code snippets (like you've been doing) is OK on this list.
> My long term goal is to get better at C, which I've not used seriously since
> the class I took a few years ago. A majority of the open source projects seem
> to be coded in C, and I hope I can be helpful someday. The beauty of this
> current learning project is I can work on it and learn at work!
You're doing the right thing.
The best way "to get better at C" is to write C programs. (IMO)
> Thanks again for enlightening me on that. I wondered a couple of hours ago
> whether strncpy would get the job done, but I didn't realize you could say
> oldstring+5 and start there. Thanks again!
Think of strings (in C) as just a pointer to a location in memory, where
the characters exist. Since it's a pointer (just a number), you can add
to it. (Subtraction is not recommended since you don't know what's in
the memory prior to your string :)
That's also the reason "if (string1 == string2)" doesn't work, since
you're comparing the pointers (numbers), not the actual characters.
To compare strings, use strcmp (or strncmp) to compare byte by byte.
FWIW ... :-)
--------------------------------------------
Bruce Smith bruce@armintl.com
System Administrator / Network Administrator
Armstrong International, Inc.
Three Rivers, Michigan 49093 USA
http://www.armstrong-intl.com/
--------------------------------------------