LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-24-2009, 09:17 AM   #1
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 779

Rep: Reputation: 67
Code fragment explanation please!


Hi all,

I still sometimes come across source code I don't understand. Here's one example:

Code:
if( rsrc->outfile )
In this snippet, what is '->' all about?? If it's Boolean it's a new one on me! Any ideas what this symbol combination means?

Thanks!
 
Old 10-24-2009, 09:31 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I could have said "Google", but Google does not like that phrase by itself.

Found after some Googling (assuming you meant in the C language):

Quote:
In fact, an expression involving the member-selection operator (->) is a shorthand version of an expression using the period (.) if the expression before the period consists of the indirection operator (*) applied to a pointer value. Therefore,

expression -> identifier

is equivalent to

(*expression) . identifier

when expression is a pointer value.
 
Old 10-24-2009, 09:36 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
rsrc is a pointer to a struct. One of the elements in this struct is outfile and that's the way to access it.

Code:
struct abc {
int a;
char outfile[22];
}

int main (int argc, char *argv[])
{
struct abc myvar;    // variable of type struct abc
struct abc *myptr;   // pointer to struct abc

    // a way to set some values
    myvar.a = 4;
    strcpy (myvar.outfile,"TheOutfile");

    // let myptr point to myvar
    myptr=&myvar;
    // and print
    printf("%d >> %s\n",myptr->a, myptr->outfile);

    return 0;
}
Where it is used more often is in functions. You don't want to pass the actual data as it can be very big. Therefore you pass the pointer to the data.

Code:
struct abc {
int a;
char outfile[22];
}

void print_abc (struct abc *myptr)
{
    printf("%d >> %s\n",myptr->a, myptr->outfile);
}


int main (int argc, char *argv[])
{
struct abc myvar;    // variable of type struct abc

    // a way to set some values
    myvar.a = 4;
    strcpy (myvar.outfile,"TheOutfile");

    print_abc(&myvar);

    return 0;
}
Note:
* coded from the head, so might contain some mistakes

Last edited by Wim Sturkenboom; 10-24-2009 at 09:40 AM.
 
Old 10-24-2009, 10:56 AM   #4
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 779

Original Poster
Rep: Reputation: 67
Thanks, Wim! I was wondering what kind of variable rsrc could be, to be honest; couldn't find it declared in any of the source or header files linking to the source. That's not to say it's not defined somewhere, just that I hadn't found it.
Pixellany, I tried Googling as well and found the same problem. Have since found out '->' is a 'postfix' operator (whatever that is). Some kind of bitwise variant, I guess.
 
Old 10-24-2009, 12:30 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,227

Rep: Reputation: 170Reputation: 170
Quote:
Originally Posted by Completely Clueless View Post
Thanks, Wim! I was wondering what kind of variable rsrc could be, to be honest; couldn't find it declared in any of the source or header files linking to the source. That's not to say it's not defined somewhere, just that I hadn't found it.
Pixellany, I tried Googling as well and found the same problem. Have since found out '->' is a 'postfix' operator (whatever that is). Some kind of bitwise variant, I guess.
i dont thin -> is a postfix operator
it accesses a member of a pointer to a structure
 
Old 10-24-2009, 12:51 PM   #6
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Rep: Reputation: 33
Why don't you pick up a good book on C and start learning the language? It's a very basic concept.
 
Old 10-24-2009, 01:01 PM   #7
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,079

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Quote:
Originally Posted by Completely Clueless View Post
Have since found out '->' is a 'postfix' operator (whatever that is).
It is not a postfix operator.

A postfix operator has one operand and the operator comes after the operand.
A prefix operator has one operand and the operator comes before the operand.
An infix operator has two operands and the operator comes between the operands.

-> has two operands (a pointer and a field name) and the -> operator comes between the two operands.

Quote:
Some kind of bitwise variant.
I think you are confused over the fact that C and C++ support implicit casts to true/false when values that are not boolean are used in places such as if conditions.

From the original snip of code there is no reason to suspect that rsrc->outfile is a boolean value. rsrc is a pointer to some kind of structure and outfile is the name of some field of that kind of structure. The type of rsrc->outfile is the type of whatever is that named field in that kind of structure.
 
Old 10-24-2009, 01:14 PM   #8
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 779

Original Poster
Rep: Reputation: 67
Wink

Quote:
Originally Posted by johnsfine View Post
It is not a postfix operator.
Okay fine. Well this online tutorial must be wrong then:

http://www.tutorialspoint.com/ansi_c...ator_types.htm

It's stated to be a postfix operator in the top entry of the last table on this page (under operator precedence).

Thought I'd better just exculpate myself....
 
Old 10-24-2009, 01:15 PM   #9
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 779

Original Poster
Rep: Reputation: 67
Wink

Quote:
Originally Posted by gregorian View Post
Why don't you pick up a good book on C and start learning the language? It's a very basic concept.
Bit pointless these days what with all these net tutorials. Nevertheless, I am on the lookout for a concise C pocket reference - to be the subject of a future post.
 
Old 10-24-2009, 01:16 PM   #10
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 779

Original Poster
Rep: Reputation: 67
Smile

Quote:
Originally Posted by smeezekitty View Post
i dont thin -> is a postfix operator
it accesses a member of a pointer to a structure
That seems to be the consensus. BTW, heck of a start you've made on this group in just one month!
 
Old 10-24-2009, 03:02 PM   #11
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,079

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Quote:
Originally Posted by Completely Clueless View Post
Well this online tutorial must be wrong then:
In my opinion, it is wrong. But the question is slightly tricky. There is a fundamental difference between the right hand "operand" of the ordinary infix operators and the right hand operand of -> or .

An ordinary operand can be replaced by an expression in () that evaluates to the correct kind of thing.

But the right hand operand of -> or . must directly be the right kind of thing (a field name in the correct structure). There is no support for it to be an expression evaluating to the right thing.

(In C++ there are alternate versions ->* and .* which are ordinary infix operators in the sense that the right hand operand could be an expression evaluating to the right kind of thing.

So, if you took a stricter view of the meaning of "operand" then -> or . could not be called infix operators.

Even with that view, you couldn't call -> or . postfix operators. Instead, in your original example of rsrc->outfile you would call ->outfile a postfix operator.

It is clearer to call -> or . infix operators that happen to have the same operator precedence as the true postfix operators (the other items listed on that line).
 
Old 10-25-2009, 11:45 AM   #12
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Rep: Reputation: 33
Quote:
Originally Posted by Completely Clueless View Post
Bit pointless these days what with all these net tutorials. Nevertheless, I am on the lookout for a concise C pocket reference - to be the subject of a future post.
Try "The C Programming Language" by Kernighan and Ritchie. While it won't fit in your pocket, it's certainly concise.
 
Old 10-25-2009, 12:48 PM   #13
easuter
Member
 
Registered: Dec 2005
Location: Portugal
Distribution: Slackware64 13.0, Slackware64 13.1
Posts: 538

Rep: Reputation: 62
Quote:
Originally Posted by gregorian View Post
Try "The C Programming Language" by Kernighan and Ritchie. While it won't fit in your pocket, it's certainly concise.
O'Reilly's "C Pocket Reference" will , but yeah that book is a must have.

Quote:
Originally Posted by Completely Clueless
If it's Boolean it's a new one on me!
Types like chars, integers and pointers can be evaluated as boolean: zero is false and non-zero is true.

Eg: a NULL pointer is "false" (if the NULL defines to zero as it does on my system), a non-NULL pointer is "true".
 
Old 10-26-2009, 06:23 PM   #14
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 779

Original Poster
Rep: Reputation: 67
Thumbs up

Quote:
Originally Posted by gregorian View Post
Try "The C Programming Language" by Kernighan and Ritchie. While it won't fit in your pocket, it's certainly concise.
I already have it! _Great_ book on C and one of the very few I couldn't bear to throw away when I moved house. (Sorry Herb!)
 
Old 10-30-2009, 10:15 AM   #15
Completely Clueless
Member
 
Registered: Mar 2008
Location: Marbella, Spain
Distribution: Many and various...
Posts: 779

Original Poster
Rep: Reputation: 67
Lightbulb

Quote:
Originally Posted by johnsfine View Post
In my opinion, it is wrong.
You were right. It is indeed a 'structure access operator' as you and some others here asserted. I finally found what follows from some obscure site and post it here for clarification:

Quote:
Selection (structure-access) operators ('.' and '->')

The C language supports two selection operators:

. (direct member selector)
-> (indirect, or pointer, member selector)

You use the selection operators '.' and '->' to access structure and union members. Suppose that the object s is of struct type S and sptr is a pointer to s. Then, if m is a member identifier of type M declared in S, these expressions:

s.m
sptr->m

are of type M, and both represent the member object m in s.

The expression

sptr->m

is a convenient synonym for (*sptr).m.

The direct member selector ('.') uses the following syntax:

expresssion . identifier

The expr must be of type union or structure. The identifier must be the name of a member of that structure or union type.

The indirect member operator ('->') uses the following syntax:

expr -> identifier

The expr must be of type pointer to structure or pointer to union. The identifier must be the name of a member of that structure or union type.

The expression with selection operators designates a member of a structure or union object. The value of the selection expression is the value of the selected member; it will be an lvalue if and only if the expr is an lvalue. For example,

struct mystruct
{
int i;
char str[21];
long d;
} s, *sptr=&s;

...

s.i = 3; // assign to the 'i' member of mystruct 's'
sptr->d = 12345678; // assign to the 'd' member of mystruct 's'

The expression 's.m' is an lvalue, provided that 's' is an lvalue and 'm' is not an array type. The expression 'sptr->m' is an lvalue unless 'm' is an array type.

If structure B contains a field whose type is structure A, the members of A can be accessed by two applications of the member selectors.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does EXT2/# Fragment? sirclown82 General 6 03-13-2007 08:51 PM
awk code explanation pete1234 Programming 2 10-14-2006 07:05 PM
Code explanation ... ? dbee Linux - Software 1 12-17-2005 01:06 AM
fat fs code explanation ramya272 Programming 0 03-06-2004 09:55 AM
Pascal Code Explanation Gerardoj Programming 1 09-25-2003 01:15 AM


All times are GMT -5. The time now is 04:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration