473,472 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

"." vs "->" operators

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
Nov 14 '05 #1
8 1420
Ben wrote:
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]
Nov 14 '05 #2
"Ben" <cr*********@yahoo.com> wrote in message
news:d9**************************@posting.google.c om...
(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
Nov 14 '05 #3
Ben wrote:
(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.)

--
Er*********@sun.com

Nov 14 '05 #4
cr*********@yahoo.com (Ben) wrote in
news:d9**************************@posting.google.c om:
(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");
}

--
- Mark ->
--
Nov 14 '05 #5
Ben wrote:
(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]

Nov 14 '05 #6
Tim Hagan
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:2l************@uni-berlin.de...

[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.

--
Tim Hagan
Nov 14 '05 #7
In <d9**************************@posting.google.com > cr*********@yahoo.com (Ben) writes:
(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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
In <2l************@uni-berlin.de> Martin Ambuhl <ma*****@earthlink.net> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Fabrice Régnier | last post by:
Hi ;) Everything is in the title. the prob is "boo" is displayed ! Can you believe it ? Thanx for anyone who can help me. fabrice.
7
by: Petr Prikryl | last post by:
Hi, Summary: In my opinion, the C-like prefix increment and decrement operators (++i and --i) should be marked as "syntax error". Current situation: try... (Python 2.4 (#60, ...)) >>> i =...
70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
2
by: Steven T. Hatton | last post by:
I'm still not completely sure what's going on with C++ I/O regarding the extractors and inserters. The following document seems a bit inconsistent:...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
6
by: Rennie deGraaf | last post by:
In the last few days, I have discovered two "interesting" behaviours of the C language, both of which are apparently correct. Could someone please explain the reasoning behind them? 1. The...
12
by: Emi Lu | last post by:
Hello all, I have a question about "date" & "timestamp" types in PostgreSQL. I want to setup the default value '0000-00-00' and "0000-00-00 00:00:00" for them. However, it seems that PostgreSQL...
43
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
25
by: tsaar2003 | last post by:
Hi Pythonians, To begin with I'd like to apologize that I am not very experienced Python programmer so please forgive me if the following text does not make any sense. I have been missing...
4
by: Leonid | last post by:
Hello all :) could anyone give a guide, ebook etc.. about the operators "?" and ":" thanks in advance Leonid
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.