[KLUG Programming] substr in C?

Tony Gettig programming@kalamazoolinux.org
Tue, 22 Jul 2003 11:51:51 -0400


Quoting Bruce Smith <bruce@armintl.com>:

> Are you compiling & running this on a Intel Linux box?

I've tried it on Red Hat 8 and W2K, both using gcc, with the same results.

> How are your strings (and other variables shown) declared?

Sorry about that. I should have included that before. 

Declared globally (at the top of the file):
typedef struct 
{
   char username[7];	/* username */
   char firstname[20];	/* First name */
   char middlename[15];	/* Middle name */
   char lastname[25];	/* Last Name */
   char yog[5];		/* Year of Graduation */
   char location[25];	/* Location: school building name */
   char homeroom[5];	/* Homeroom assignment */
   char pw[6];		/* Initial password */
} student_record;
#define STRINGSIZE 256;

Declared in the function:
char *strTemp[255];
student_record student;

I plan to be at KLUG tonight. I could show you the entire work in progress 
then if you like. Thanks again!

Tony


> 
> > I'm encountering something odd with this method, but I have an idea what is
> 
> > causing it. Using the example strncpy(newstring, oldstring+5, 6) again, the
> 
> > oldstring+5 equates to 5 times 4. That is, it actually starts reading at 
> > position 20 of my fixed width line of data. 
> > 
> > I just did a little test and verified that for each 1 added to the string 
> > pointer, it actually moves 4. In fact, my string is declared as
> strTemp[255] 
> > and the sizeof(strTemp) is 1020...4 times the declaration. Yet, the sizeof
> > (char) is 1. Is each character in a string pointer 4 bytes? I tried
> something 
> > like oldstring+5-15 (so I can presumably start at position 5), but I can 
> > assure you, the result is not what I expect. :)
> > 
> > Aha! As I compose this email and research the mystery, I find in the GNUC
> docs 
> > that the sizeof a pointer is 4 bytes, at least in their example in the
> String 
> > Length explanation.
> > 
> > Here's an actual code snippet:
> > 
> > while ((fgets(strTemp, STRINGSIZE, hDatafile)) != NULL)
> > {
> > 	strncpy(student.username, strTemp+1, 7);
> > 	student.username[7]='\0';
> > 	strncpy(student.lastname, strTemp+7, 24);
> > 	student.lastname[strlen(student.lastname+1)]='\0';
> > 	printf("%s\n%s\n", student.username, student.lastname);
> > }
> > 
> > I tried strTemp+1/4, but that's the same as strTemp+0. Likewise,
> strTemp+7/4 
> > is the same as strTemp+1. Any ideas or pointers (pun intended) in the right
> 
> > direction are appreciated. 
> 
> --------------------------------------------
> Bruce Smith                bruce@armintl.com
> System Administrator / Network Administrator
> Armstrong International, Inc.
> Three Rivers, Michigan  49093  USA
> http://www.armstrong-intl.com/
> --------------------------------------------
> 
> _______________________________________________
> Programming mailing list
> Programming@kalamazoolinux.org
> http://www.kalamazoolinux.org/mailman/listinfo/programming
> 


-- 
Tony Gettig
Voiceovers, PGP key, and more at
http://gettig.net