ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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,
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.
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.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
Quote:
Originally Posted by Completely Clueless
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
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.
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.
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).
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.