C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming
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.

Learn More!


Download to Enter
| Contest Rules

Tutorials | Forums

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 9th, 2012, 04:10 PM
counciler counciler is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 3 counciler User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.)

Reply With Quote
  #2  
Old February 9th, 2012, 05:25 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,164 ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level)ptr2void User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 3 Days 17 h 39 m 23 sec
Reputation Power: 1686
& is the address-of operator. It's use in this case is to receive the size of the file into the variable. Assuming it's an unsigned int:
c Code:
Original - c Code
  1.  
  2. unsigned int fileSize;
  3. result = AudioFileReadBytes(fileID, false, 0, &fileSize, outData);

then if that function worked, the variable fileSize will likely contain the size of the data read into outData
c Code:
Original - c Code
  1.  
  2. int fileSize;
  3. result = AudioFileReadBytes(fileID, false, 0, &fileSize, outData);
  4. printf("The size of the file is %u\n", fileSize);


-> is the struct dereference by pointer operator. If you have a struct:
c Code:
Original - c Code
  1. struct myStruct
  2. {
  3.     int val;
  4. }


and you create one via a pointer

c Code:
Original - c Code
  1. struct myStruct *p = malloc(sizeof(*p));


then to access the val member you use this operator

c Code:
Original - c Code
  1. p->val = 42;
__________________
I ♥ ManiacDan

This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!

Reply With Quote
  #3  
Old February 9th, 2012, 10:25 PM
jameer jameer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 128 jameer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.
__________________
ASP.NET Hosting
SQL Server Hosting

Reply With Quote
  #4  
Old February 23rd, 2012, 06:48 PM
counciler counciler is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 3 counciler User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old February 23rd, 2012, 08:44 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Click here for more information.
 
Join Date: Jan 2003
Location: USA
Posts: 5,415 dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level) 
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.

Reply With Quote
  #6  
Old February 23rd, 2012, 09:14 PM
counciler counciler is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2012
Posts: 3 counciler User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old February 23rd, 2012, 10:47 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Click here for more information.
 
Join Date: Jan 2003
Location: USA
Posts: 5,415 dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 11st Grade (Above 100000 Reputation Level) 
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.

Here are a couple other quotes from Wirth's Wikipedia article (http://en.wikipedia.org/wiki/Niklaus_Wirth):
Quote:
Wirth's law: "Software is getting slower more rapidly than hardware becomes faster."

"Reliable and transparent programs are usually not in the interest of the designer."
—Niklaus Wirth

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > & and ->


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap