"." vs "->" operators

B

Ben

(I am using gcc 3.2 on RH 8)

int name (struct str *name)

I call the above function like this:
struct str buf;
int conf = name(&buf);
int j;

for (j=0; buf.address[j]; j++) {
printf("%c", buf.address[j]);
}

If I do buf->address[j] instead of buf.address[j], it gives me error..
Why's that? aren't they the same thing?

Bit confused!
Ben
 
M

Mark Henning

Ben said:
If I do buf->address[j] instead of buf.address[j], it gives me error..
Why's that? aren't they the same thing?

No. buf->address[j] is equivelant to (*buf).address[j]
 
M

Mike Wahler

Ben said:
(I am using gcc 3.2 on RH 8)

int name (struct str *name)

I call the above function like this:
struct str buf;
int conf = name(&buf);
int j;

for (j=0; buf.address[j]; j++) {
printf("%c", buf.address[j]);
}

If I do buf->address[j] instead of buf.address[j], it gives me error..

It should.
Why's that? aren't they the same thing?

Not at all. Why would you think so?

'.' is used to select a member from a struct
object 'directly' , using the struct object's name.

'->' is used to select a member from a struct object
'indirectly', using a pointer to that struct object.

struct s
{
int member;
};

struct s obj; /* struct */
struct s *p = &obj; /* pointer to struct */

obj.member; /* (1) direct access */
p->member; /* (2) indirect access, via a pointer */
(*p).member; /* (3) same as (2) */


(2) is simply 'shorthand' notation for (3).

Inside your function 'name()', you can access the struct
members with '->' (since the parameter is a pointer to
a struct). e.g. name->address. Alternatively,
(*name).address

BTW your function and its parameter have the same identifier.
Don't Do That. :)


-Mike
 
E

Eric Sosman

Ben said:
(I am using gcc 3.2 on RH 8)

int name (struct str *name)

I call the above function like this:
struct str buf;
int conf = name(&buf);
int j;

for (j=0; buf.address[j]; j++) {
printf("%c", buf.address[j]);
}

If I do buf->address[j] instead of buf.address[j], it gives me error..
Why's that? aren't they the same thing?

Both notations work with structs (or unions) and
designate the element named on the right-hand side.
The difference is in what's on the left-hand side: for
`.' the l.h.s. is an actual struct, while for `->' it's
a pointer to a struct.

struct str buf; /* a struct object */
struct str *ptr = &buf; /* a pointer to it */

buf.thing = 42; /* assign to a struct element */
ptr->thing = 42; /* assign via a pointer */

buf->thing = 42; /* error: l.h.s. not a pointer */
ptr.thing = 42; /* error: l.h.s. not a struct */

Actually, the `->' operator could be done away with, at
some cost in verbosity:

(*ptr).thing = 42;

Since `ptr' points to a struct, `*ptr' is the struct
itself, and we can apply the `.' operator to it. (We
need the parentheses because `*ptr.thing' would be the
interpreted as `*(ptr.thing)', which is not what we want.)
 
M

Mark A. Odell

(e-mail address removed) (Ben) wrote in

(I am using gcc 3.2 on RH 8)

int name (struct str *name)

I call the above function like this:
struct str buf;
int conf = name(&buf);
int j;

for (j=0; buf.address[j]; j++) {
printf("%c", buf.address[j]);
}

If I do buf->address[j] instead of buf.address[j], it gives me error..
Why's that? aren't they the same thing?

Sure it does. -> is for pointers to struct/unions whereas . is for
struct/unions. E.g.

struct str Str;
struct str *pStr = &str;

if (Str.address[0] != pStr->address[0])
{
printf("The world has broken\n");
}
 
M

Martin Ambuhl

Ben said:
(I am using gcc 3.2 on RH 8)

int name (struct str *name)

I call the above function like this:
struct str buf;
int conf = name(&buf);
int j;

for (j=0; buf.address[j]; j++) {
printf("%c", buf.address[j]);
}

If I do buf->address[j] instead of buf.address[j], it gives me error..

Of course it does. 'buf' is a struct, not a pointer to a struct.
Why's that? aren't they the same thing?

No. A pointer to a thing is not the thing.
Avoiding the evil name 'str':
struct s b1, *b2;
b2 = &b1;

These refer to the same thing:
b1.address[j]
b2->address[j]
(*b2).address[j]
(&b1)->address[j]
(*(&b1)).address[j]
 
T

Tim Hagan

Tim Hagan

[snip]
Avoiding the evil name 'str':

Huh? There is nothing wrong with 'str'. It is a valid identifier.

Regarding function names, WG14/N869 states:

7.26.10 General utilities <stdlib.h>

[#1] Function names that begin with str and a lowercase
letter (possibly followed by any combination of digits,
letters, and underscore) may be added to the declarations in
the <stdlib.h> header.

7.26.11 String handling <string.h>

[#1] Function names that begin with str, mem, or wcs and a
lowercase letter (possibly followed by any combination of
digits, letters, and underscore) may be added to the
declarations in the <string.h> header.

So 'str' is even a valid function name.
 
D

Dan Pop

In said:
Avoiding the evil name 'str':

str is never, in any context, an evil name. It is an evil *prefix* for
user identifiers, but not an evil user identifier.

Dan
 
D

Dan Pop

In said:
(I am using gcc 3.2 on RH 8)

int name (struct str *name)

I call the above function like this:
struct str buf;
int conf = name(&buf);
int j;

for (j=0; buf.address[j]; j++) {
printf("%c", buf.address[j]);
}

If I do buf->address[j] instead of buf.address[j], it gives me error..
Why's that? aren't they the same thing?

The simplest answer is:

.. is the simple selection operator, expecting an operand with structure
type at its left. -> is the indirection and selection operator, expecting
an operand with pointer to structure type at its left.

ptr -> field is actually a (handy) shortcut for (*ptr).field.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top