Author |
Message |
Karel van Eerse #1 / 13
|
 printing a string contained in a structure
Good evening everybody, I've come across a problem while reading chapter 6 of K&R which I haven't been able to solve so far. The problem that I have pertains to printing a string on the screen. The string is accessed by means of a pointer, which is declared in a structure. The code looks like this (the information contained in the structure is purely fictional): #include <stdio.h> int main (void) { struct address { int number; char *street; char *city; char *state; char *country; }; struct address mine = {1754, "Fifth Avenue", "Tampa", "Florida", "USA"}; struct address *p; p = &mine; printf ("The object 'street' points to is %s.\n", *p -> street); /* the rest of the structure members are printed here ...*/ return 0; Quote: }
Now every time I run this program (it compiles without warnings/ errors), I get a "Divide Error, Abnormal Program Termination", after which the program halts. K&R page 132 gives me the "*p -> street" (could have figured that one out myself) and page 133 shows me how to "fill" the structure, so that part seems OK. Can someone point out the error to me please? The fact that the code is so easy and yet unable to work drives me nuts! Cordially, Casper van Eersel
|
Tue, 02 Oct 2001 03:00:00 GMT |
|
 |
Ben Pfaf #2 / 13
|
 printing a string contained in a structure
struct address { int number; char *street; char *city; char *state; char *country; }; struct address mine = {1754, "Fifth Avenue", "Tampa", "Florida", "USA"}; struct address *p; p = &mine; printf ("The object 'street' points to is %s.\n", *p -> street); Remove the *. -- (supporter of the campaign for grumpiness where grumpiness is due in c.l.c) Please: do not email me copies of your posts to comp.lang.c do not ask me C questions via email; post them instead
|
Tue, 02 Oct 2001 03:00:00 GMT |
|
 |
Paul Lutu #3 / 13
|
 printing a string contained in a structure
<< printf ("The object 'street' points to is %s.\n", *p -> street); >> This must be: printf ("The object 'street' points to is %s.\n", p->street); Applying '*' to p, and applying '->' to p, both have the same effect. Doing both in this case produces undefined behavior. -- Paul Lutus www.arachnoid.com
<snip>
|
Tue, 02 Oct 2001 03:00:00 GMT |
|
 |
Stefan Hetz #4 / 13
|
 printing a string contained in a structure
Quote:
> struct address *p; > p = &mine; > printf ("The object 'street' points to is %s.\n", *p -> street); > /* the rest of the structure members are printed here ...*/ > return 0; > }
If p is the pointer to a structure, *p is a structure. To access a member of a structure use struct.member, to access a member of a structure via a pointer to this structure use ptrtostruct->member. In this case use either p->street or (*p).street (the brackets are necessary because the . operator has a higher precedence than the * operator). It probably looks nicer to use p->street. Stefan
|
Tue, 02 Oct 2001 03:00:00 GMT |
|
 |
Jmma #5 / 13
|
 printing a string contained in a structure
printf ("string is %s\n",&p->street[0]) ; will work.
|
Wed, 03 Oct 2001 03:00:00 GMT |
|
 |
Will Ro #6 / 13
|
 printing a string contained in a structure
: Good evening everybody, : I've come across a problem while reading chapter 6 of K&R which I haven't : been able to solve so far. The problem that I have pertains to printing a : string on the screen. The string is accessed by means of a pointer, which : is declared in a structure. The code looks like this (the information : contained in the structure is purely fictional): : #include <stdio.h> : int main (void) : { : struct address : { : int number; : char *street; : char *city; : char *state; : char *country; : }; : struct address mine = {1754, "Fifth Avenue", "Tampa", "Florida", "USA"}; : struct address *p; : p = &mine; : printf ("The object 'street' points to is %s.\n", *p -> street); : /* the rest of the structure members are printed here ...*/ : return 0; : } : Now every time I run this program (it compiles without warnings/ errors), I : get : a "Divide Error, Abnormal Program Termination", after which the program : halts. K&R page 132 gives me the "*p -> street" (could have figured that : one out myself) and page 133 shows me how to "fill" the structure, so that : part seems OK. I don't know where K&R gives you *p->street; it accesses whatever p->street points to, in your case the letter 'F'. If you replace %s by %c in the printf() string the code will work. However, that's not what you want to do. If p is a pointer to struct, *p is a struct, and you access elements of a structure with '.', hence mine.street or (*p).street. The parentheses are necessary because '.' (and '->') have high precedence. However, there's a shorthand; instead of saying (*p).element you can just say p->element, which is thought to be clearer and simpler to read. Will
|
Wed, 03 Oct 2001 03:00:00 GMT |
|
 |
Joe Wrigh #7 / 13
|
 printing a string contained in a structure
Quote:
> printf ("string is %s\n",&p->street[0]) ; > will work.
No it won't. The char * is mine.street or p->street. --
"Everything should be made as simple as possible, but not simpler." --- Albert Einstein ---
|
Wed, 03 Oct 2001 03:00:00 GMT |
|
 |
Lawrence Kir #8 / 13
|
 printing a string contained in a structure
... Quote: >#include <stdio.h> >int main (void) >{ > struct address > { > int number; > char *street; > char *city; > char *state; > char *country; > }; > struct address mine = {1754, "Fifth Avenue", "Tampa", "Florida", "USA"}; > struct address *p; > p = &mine; > printf ("The object 'street' points to is %s.\n", *p -> street); > /* the rest of the structure members are printed here ...*/ > return 0; >} >Now every time I run this program (it compiles without warnings/ errors), I >get >a "Divide Error, Abnormal Program Termination", after which the program >halts. K&R page 132 gives me the "*p -> street" (could have figured that >one out myself) and page 133 shows me how to "fill" the structure, so that >part seems OK.
K&R used *p->str to access a single character in the string i.e. to produce a value of type char. However here you're using the %s conversion specifier which requires a pointer to (the first character of) a string i.e. a value of type char *. You need to pass the value of p->street (i.e. the pointer value) not the character it points at so make that printf("The object 'street' points to is %s.\n", p->street); -- -----------------------------------------
-----------------------------------------
|
Wed, 03 Oct 2001 03:00:00 GMT |
|
 |
Lawrence Kir #9 / 13
|
 printing a string contained in a structure
Quote: ><< printf ("The object 'street' points to is %s.\n", *p -> street); >> >This must be: >printf ("The object 'street' points to is %s.\n", p->street); >Applying '*' to p, and applying '->' to p, both have the same effect.
*p evaluates to the structure that p points to, p->street evaluates to the street member of that structure so they are different. Quote: >Doing >both in this case produces undefined behavior.
To be clear *p->street is equivalent to *(p->street) which is fine as long as p and p->street are valid pointers to appropriate objects. The undefined behaviour is caused by passing a char value to printf when a char * value is required. -- -----------------------------------------
-----------------------------------------
|
Wed, 03 Oct 2001 03:00:00 GMT |
|
 |
Michael Rubenste #10 / 13
|
 printing a string contained in a structure
On Sat, 17 Apr 1999 08:30:16 -0400, Joe Wright Quote:
>> printf ("string is %s\n",&p->street[0]) ; >> will work. >No it won't. The char * is mine.street or p->street.
And how is p->street different from &p->street[0]? -- Michael M Rubenstein
|
Wed, 03 Oct 2001 03:00:00 GMT |
|
 |
Will Ro #11 / 13
|
 printing a string contained in a structure
: > : > printf ("string is %s\n",&p->street[0]) ; : > will work. : No it won't. The char * is mine.street or p->street. True, but &p->street[0], tho' weird, will work. I had to double check the precedences to be sure of it, though. Will
|
Wed, 03 Oct 2001 03:00:00 GMT |
|
 |
H2OLUV6 #12 / 13
|
 printing a string contained in a structure
use p->street for printf, instead of *p->street and you'll be fine!! Note: *p->street is a pointer to pointer, which in your case, is simply a syntax error. Your truly, Lori><HTML><PRE>Subject: printing a string contained in a structure
|
Fri, 05 Oct 2001 03:00:00 GMT |
|
|