473,774 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ 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 1441
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*********@ya hoo.com> wrote in message
news:d9******** *************** ***@posting.goo gle.com...
(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*********@yah oo.com (Ben) wrote in
news:d9******** *************** ***@posting.goo gle.com:
(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)).addres s[j]

Nov 14 '05 #6
Tim Hagan
"Martin Ambuhl" <ma*****@earthl ink.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*********@yah oo.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*****@earthl ink.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
2463
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
4388
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 = 1 >>> i
70
8911
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 expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
2
3509
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: http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#1 Copying a file: WRONG WAY: #include <fstream> std::ifstream IN ("input_file"); std::ofstream OUT ("output_file");
81
7351
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 be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
6
1662
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 operators '^', '&', and '|' have lower precedance than '==', '!=', '>=", etc. I discovered this when the statement "if (array1 ^ array2 == 0xff)" failed to do what I expected. To me, it doesn't make any sense to give the bitwise operators lower...
12
13916
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 does not support it. Could someone helps me please? The example table: T1 (col1 varchar(7) not null,
43
2751
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 & \ !(v)->vcpu_info->evtchn_upcall_mask) whereas evtchn_upcall_pending is of type unsigned char (and also evtchn_upcall_mask is of type unsigned char).
25
2583
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 constants in Python language. There are some workarounds available, for example the const-module. To me, this looks quite cumbersome and very unintuitive. For the interpreter, in the efficiency-wise, I just cannot tell.
4
1389
by: Leonid | last post by:
Hello all :) could anyone give a guide, ebook etc.. about the operators "?" and ":" thanks in advance Leonid
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10040
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8939
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4012
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 we have to send another system
2
3611
muto222
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.