[KLUG Programming] substr in C?

Bruce Smith programming@kalamazoolinux.org
21 Jul 2003 15:07:24 -0400


> >   strncpy(newstring, oldstring+5, 6);
> 
> I remember by C instructor telling me that pointer arithmatic was bad
> (like oldstring+5).  

I never had this problem, because I never took a C programming class.
I'm self-C-taught, and have a fair amount of experience using C.

> But you imply that it is OK?  

I've never had a problem doing it.

> Was my C instructor on crack, or why did he make that statement?

I don't know your instructor (or his drug habits), and I don't know what
he was thinking.  You should have ask "why?" when he stated that!  :-)

I can see where you could get yourself into trouble if you add a number
larger than the length of the string, but there are a lot of ways to get
yourself in trouble in C.  That seems like an easy mistake to prevent.

What did your instructor offer as an alternative method to what Tony is
trying to do?

--------------------------------------------
Bruce Smith                bruce@armintl.com
System Administrator / Network Administrator
Armstrong International, Inc.
Three Rivers, Michigan  49093  USA
http://www.armstrong-intl.com/
--------------------------------------------