1,105,625 Community Members

When to use & and not

Member Avatar
mattboy64
Newbie Poster
20 posts since Apr 2012
Reputation Points: 13 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

These days im using xcode and
its a bit of annoyingly good amounts of changes from borland which im used to
But i keep getting error for this

printf ("\nThe amount of First Class cars Stocks remaining are: %d", &stock1);

(while stock1 is declared: int stock1)

and it says format types specifies type "int" but argument has type "int*"

i dont understand what this means..
is a pointer being used at int?

the more puzzling thing for me is that when i take out the "&" for some parts of the code:

fprintf (Stocks,"\nThe amount of First Class Stocks remaining are: %d\n", stock1);

it shows no errors
although this method doesnt work in the earlier code

I'm sorry if this is too obvious a problem
but i've never gone out of using the & sign

but please i need any help whatsoever

Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
0
 

scanf() requires the address of the variable it will change. printf() does not.

& is the address of operator.

Member Avatar
Tumlee
Posting Whiz in Training
203 posts since Oct 2011
Reputation Points: 42 [?]
Q&As Helped to Solve: 41 [?]
Skill Endorsements: 4 [?]
 
2
 

Clarifying what Adak said, here's what you should know:

The & symbol (address-of operator) will return the address of a variable (where it is stored in memory). If you have declared an integer called foo, then &foo would be its address.

When you're dealing withprintf(), you only really need to know the values of these variables. You don't need to know where they're being stored, because you only really care whether foo is a 3, a 12, or whatever number. You will never use & in printf() unless you plan on printing out raw memory addresses, which you will almost never do.

Then scanf() function actually needs to modify your data. In order for it to do this, having a value isn't enough, it actually needs to know where your variable is stored in memory so scanf() can modify it. This is why scanf() requires an & sign for most types.

The only exception to this rule is strings. A string esssentially is a memory address that points to the first character in a group of characters, so neither printf() nor scanf() take the & symbol in this case.

Member Avatar
deceptikon
Eternally Awesome
4,693 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

You will never use & in printf() unless you plan on printing out raw memory addresses, which you will almost never do.

Never say never. ;) printf() supports a %n specifier that assigns the number of characters written up to that point:

#include <stdio.h>

int main(void)
{
    int n = 0;

#ifdef _MSC_VER
    // Visual Studio disables %n by default and only provides a runtime way to enable it...
    _set_printf_count_output(1);
#endif

    printf("Printing 22 characters%n\n", &n);
    printf("Printed %d characters\n", n);

    return 0;
}

However, that specifier is viewed as a security risk, and some compilers (notable is Visual Studio, as in the example) will disable it.

A string esssentially is a memory address that points to the first character in a group of characters

Noting, of course, that this is a conversion that happens implicitly when arrays are used in value context and it's important to recognize that strings in general aren't inherently pointers.

Member Avatar
Tumlee
Posting Whiz in Training
203 posts since Oct 2011
Reputation Points: 42 [?]
Q&As Helped to Solve: 41 [?]
Skill Endorsements: 4 [?]
 
0
 

Never say never. ;) printf() supports a %n specifier that assigns the number of characters written up to that point:

Like I said, you will never use them (as in mattboy64). ;)

Member Avatar
mattboy64
Newbie Poster
20 posts since Apr 2012
Reputation Points: 13 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you so much!
I practically understand everything
except what deceptikon posted :(
but i guess like what Tumlee said,

Like I said, you will never use them (as in mattboy64). ;)

Member Avatar
deceptikon
Eternally Awesome
4,693 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

except what deceptikon posted :(

Sadly, that happens a lot. Today at work I was getting frustrated because a client just didn't seem to grok my explanations about how macro translation in some of our software's text areas worked... :(

Like I said, you will never use them (as in mattboy64). ;)

Yeah...want to know how many times I was told I'd never use something when I was a beginner that I use often now? ;)

Member Avatar
mattboy64
Newbie Poster
20 posts since Apr 2012
Reputation Points: 13 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hahahah
you are right though
since I am planning to get deep in coding
I should stop underestimating :)

Member Avatar
Tumlee
Posting Whiz in Training
203 posts since Oct 2011
Reputation Points: 42 [?]
Q&As Helped to Solve: 41 [?]
Skill Endorsements: 4 [?]
 
0
 

Yeah...want to know how many times I was told I'd never use something when I was a beginner that I use often now? ;)

Well then, hopefully by the time he uses it, he'll have forgotten that I told him he'll never use it! :D

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: