[KLUG Programming] substr in C?

Adam Williams programming@kalamazoolinux.org
21 Jul 2003 14:59:57 -0400


> > Is there a C equivalent to substr in perl or PHP? Something that might work 
> > like this: 
> > oldstring="Tony missed the clue bus.";
> > /* start at position 0, give me the next 4 chars as my string */
> > newstring=substr(oldstring, 0, 4); 
> > newstring would then contain the value "Tony". 
> ere's an example that gives you the word "missed" from your string:
> ------------------------------------------------------------------
> #include <stdio.h>
> main(argc, argv)
> int argc;
> char **argv;
> {
> char *oldstring="Tony missed the clue bus.", newstring[7];
>   strncpy(newstring, oldstring+5, 6);

I remember by C instructor telling me that pointer arithmatic was bad
(like oldstring+5).  But you imply that it is OK?  Was my C instructor
on crack, or why did he make that statement?