tad@killer.UUCP (Tad Marko) (10/10/87)
I've never bothered to get a complete grasp over structures before, and
now it is hurting me. Could someone please tell me the proper way to
accomplish the following ugliness?
-----------------
struct X {
char *s;
int a, b;
};
main()
{
struct X x;
struct X *sp;
char max[25];
sp = &x;
sp.s = max;
strcpy(x.s, "Some Stuff");
fun(&x);
}
fun(stuff)
struct X *stuff;
{
stuff->*(++s) = 'X';
printf("%s\n", stuff.s); /* I want this to print "Xme Stuff" */
}
===============================================================================
Tad Marko at UNIX Connection BBS AT&T 3B2, Dallas, Texas
North Texas State University ACM C/UNIX SIG Chairman
Snail: UUCP: ihnp4!killer!tad
2829 Sagebrush Drive or ihnp4!convex!ntvax!tad
Flower Mound, Texas 75028-2721 BitNet: IG45@NTSUVAX
214-539-0080 214-539-7776 TEXNET: NTVAXB::IG45
"Hi there!" -- Peter Gabriel InterN: IG45%NTVAXB.DECNET@UTADNX.CC.UTEXAS.EDUedw@ius1.cs.cmu.edu (Eddie Wyatt) (10/11/87)
In article <1778@killer.UUCP>, tad@killer.UUCP (Tad Marko) writes: > I've never bothered to get a complete grasp over structures before, and > now it is hurting me. Could someone please tell me the proper way to > accomplish the following ugliness? > > ----------------- Corrected program to follow: struct X { char *s; int a, b; }; main() { struct X x; struct X *sp; char max[25]; sp = &x; /* sp.s = max; */ sp->s = max; strcpy(x.s, "Some Stuff"); fun(&x); } fun(stuff) struct X *stuff; { /* stuff->*(++s) = 'X'; */ *(++stuff->s) = 'X'; printf("%s\n", stuff->s); /* I want this to print "Xme Stuff" */ } -- Eddie Wyatt e-mail: edw@ius1.cs.cmu.edu
ron@topaz.rutgers.edu (Ron Natalie) (10/11/87)
The line
stuff->*(++s) = 'X';
Should read
*++stuff->s = 'X';
The only thing that can follow "->" (or ".") for that matter are
element names. "stuff->s" is the character pointer in the structure
pointed to by stuff. "->" binds tighter than everything else in the
expression.
-Ronrwhite@nusdhub.UUCP (Robert C. White Jr.) (10/13/87)
In article <1778@killer.UUCP>, tad@killer.UUCP (Tad Marko) writes: The only problem I saw in you posted program is that when you have a pointer to a structure you were not de-refrenceing the pointer before reaching for the value. You need something like (*structname).fieldname to do this. The shorthand for this is the -> operator, therefore where sp and stuff were defined as "struct X *sp, *stuff" you need to change the lines as follows: > sp.s = max; sp->s = max; > printf("%s\n", stuff.s); /* I want this to print "Xme Stuff" */ printf("%s\n", stuff->s); /* I want this to print "Xme Stuff" */ Disclaimer: o-god-please-don't-let-me-be-wrong.... Robert.
leather@osiris.cso.uiuc.edu (10/13/87)
> /* Written 11:39 pm Oct 9, 1987 by tad@killer.UUCP > /* ---------- "Need help with structure pointers" ---------- */ > I've never bothered to get a complete grasp over structures before, and > now it is hurting me. Could someone please tell me the proper way to > accomplish the following ugliness? > > pgrogram deleted for ugliness ... |-) > A very important distinction to remember is the difference between: - the structure member operator "." and - the structure pointer operator "->" The member operator works with the name of a structure: x.s = 'X'; The pointer operator works with the pointer to a structure: sp->s = 'X'; You must also be very careful of how you de-reference your pointers (ie. how you access the data for which you have the pointer). *++(stuff->s) = 'X'; Personally, I try not to mix the two operations; but, to each his own. "The C Programming Language" by Brian W. Kernighan & Dennis M. Ritchie treats the subject in depth. Get a copy and read it (Chapter 6). UNTIL THEN - TRY THIS: (I only made it work; making it aesthetically pleasing is a matter of taste &/or programming style, which I choose not to dig into here) struct X {char *s; int a, b; }; main() {struct X x; struct X *sp; char max[25]; sp = &x; sp->s = max; strcpy(x.s, "Some Stuff"); fun(&x); } fun(stuff) struct X *stuff; { *++(stuff->s) = 'X'; printf("%s\n", stuff->s); /* I want this to print "Xme Stuff" */ } Good Luck - Dave L.