Discuss & and -> in the C Programming forum on Dev Shed. & and -> C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
Posts: 3
Time spent in forums: 57 m 37 sec
Reputation Power: 0
& and ->
I'm curious what the above symbols are for (& and ->.)
I know that & can be used as a bitwise operator. But I seen it in a context that didn't use it as an operator. I saw the following code used in an OpenAL context. My best guess is it's used as a cast to turn the fileSize into bitwise data.
Code:
result = AudioFileReadBytes(fileID, false, 0, &fileSize, outData);
As for the ->, this one confuses me the most. I can't remember the exact code that was written for it, and don't really feel like searching for it unless no one knows what I'm talking about. But I remember it was used to reference an array within a #define directive (Like fooVariable -> fooArray[3] or something of that nature.)
Posts: 128
Time spent in forums: 19 h 25 m 42 sec
Reputation Power: 3
As we all know there can be only one return value from a function. Now by using & (address-of) operator with the function argument you can directly return value through that argument.
Since this operator passes the address of the argument the operation done on that argument may be reflected in the main function also.
Posts: 3
Time spent in forums: 57 m 37 sec
Reputation Power: 0
Alright, after looking more into c it starts to make a little more sense. However, I'm still confused at the usage for using it for data types. I noticed a pattern, I haven't seen it used for variables that are already defined as being pointers, and variables like unsigned int don't require pointers to be defined, so you need the & for them. However, I've seen variables that aren't pointers be passed directly into a function without the address-of operator.
My best guess is it depends on how the function was set up to require arguments. If you set up the function to require the address of a specific variable then you'll have to pass the address of it, aka a pointer. Which would make me wonder why set it up to require the pointer of something like an unsigned int instead of just requiring an unsigned int as the argument?
Let me know if I'm off the mark here. I appreciate the help.
Posts: 5,415
Time spent in forums: 2 Months 2 Days 14 h 4 m 4 sec
Reputation Power: 1738
Quote:
"Whereas Europeans generally pronounce his name the right way ('Nick-louse Veert'), Americans invariably mangle it into 'Nickel's Worth.' This is to say that Europeans call him by name, but Americans call him by value." — Introduction by Adriaan van Wijngaarden at the IFIP Congress (1965).
Swiss computer scientist Niklaus Wirth invented the Pascal programming language, patterning it after Algol. A feature of both languages was built-in support for "call by name" (AKA "call by reference") and "call by value" with Pascal defaulting to "call by value" and requiring you to use a keyword, VAR to make it "call by name" and Algol doing the same thing only in reverse. This is a fundamental programming concept.
In "call by value", the argument is evaluated and that value is stored in a local variable for use by the function; that local variable is of course the parameter it is passed in through. All the changes you make to that local variable are lost when you exit the function; the variable that you pass in by value remains unaffected.
In "call by name" (AKA "call by reference", which is probably the more preferred term), the address of the argument is passed to the function and all references to that parameter within the function use that address to access the variable that was passed in. As a result, all changes you make to that parameter also modifies the variable that was passed in, such that after you exit the function the changes you made remain in effect. Of course, whereas you can pass an expression by value (eg, (x+1)), you must pass a variable when you call by name.
OK, C only supports "call by value" and does not support "call by name", so you need to implement it yourself. You implement "call by name" by explicitly declaring functions to take pointers for arguments and by explicitly passing the addresses of the variables with the & address operator. Of course, if the variable is an address, then you do not need to use the address operator on it; be aware that array names can be used as pointers in most, but not in all cases.
For example, let's consider the classic swap function. First, I will screw it up by not using pointers:
Code:
void swap_not(int a, int b)
{
int temp = b;
b = a;
a = temp; // they've been swapped, but only within the function
}
int main()
{
int x = 3, y = 4;
swap_not(x, y); // calling by value; the wrong way
// x ==3, y == 4; the swap did not affect x and y
return 0;
}
Calling by value, the changes within the function did not make their way out to the calling function. That is not what we want.
Now to implement call-by-reference:
Code:
void swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp; // they've been swapped in the calling function
}
int main()
{
int x = 3, y = 4;
swap(&x, &y); // calling by name/reference; the right way!
// x == 4, y == 3; the swap worked!
return 0;
}
That's the way it's done in C. Now, in C++, AKA "a better C", you are able to declare parameters to be references, which does the same thing as pointers do only without the explicit notation -- kind of a Pascal-ization if you would.
Posts: 3
Time spent in forums: 57 m 37 sec
Reputation Power: 0
Superb explanation. Makes a lot more sense now. I appreciate the help! I especially like the joke and brief history lesson. I wonder if people actually call him Nickel's Worth or if that's just something someone invented as a joke. Programming jokes are hilarious to me, like how there's only 10 people in this world, the ones that can read binary and the ones that can't.
Posts: 5,415
Time spent in forums: 2 Months 2 Days 14 h 4 m 4 sec
Reputation Power: 1738
Niklaus Wirth was German Swiss. In German, Wirth is pronounced similiarly to unschooled English "Veert" (I started out as a foreign language major, primarily in German, so I have trouble with common English "phonetic" spellings). Though the only other place I've seen that joke was in a late-70's comedy piece, "Real Programmers Don't Write Pascal". And I already knew how to pronounce his name and don't recall hearing anyone else; outside of a single mention of his name in an introductory lecture, I can't think of anywhere else it would have come up. Similarly, C programmers may or may not be familiar with the term, "K&R", which applies to a particular indenting style, and even fewer would know that that stands for Kernighan and Ritchie, Ritchie having been the inventor of C, Kernighan of the "Hello, World" program, and both of them the authors of the first book on C.