Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Themonkster

pointers arrrghhhhh

This topic is 5938 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey guys got a quick question. started to play with C and have come up with this problem. I have made this function heres the prototype void getNumber(int*); heres the definition void getNumber(int * a) { printf("ENTER GUESS: "); scanf("%i",&a); } heres how I use it getNumber(&guess); but what this is doing is giving guess the value of the address of a not the the value ,I think. if I remove the & I get errors c:\my documents\c projects\game\main.c(23) : warning C4047: 'function' : 'int *' differs in levels of indirection from 'int ' c:\my documents\c projects\game\main.c(23) : warning C4024: 'getNumber' : different types for formal and actual parameter 1 c:\my documents\c projects\game\main.c(23) : warning C4700: local variable 'guess' used without having been initialized this method works fine without the & for strings but not for ints. I know simple stuff but it's late. cheers [edited by - themonkster on September 12, 2002 7:38:14 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
And he would want scanf("%i",a) right? Because it''s the address of the value that he wants to be scnned into and scanf expects the address

Share this post


Link to post
Share on other sites
Passing ''&a'' says ''the address of a''. Since ''a'' is a pointer to an int ''int*'', you''re asking for the address of a variable which contains the address of a, when actually you just need the address of a. So pass ''a'' instead of ''&a'' to scanf.

I don''t know what you''ve declared ''guess'' as either, but there may be an error there too.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
Bah, don't do it like that in the first place, just return a value from your function, like so:


    
int getNumber();

int main()
{
int a;
a = getNumber();
return a;
}

int getNumber()
{
int returnValue;
printf("ENTER GUESS: ");
scanf("%i", &returnValue);
return returnValue;
}


EDIT: oops...
EDIT: ack, thanks scaught, I hate making stupid mistakes like that without a compiler to prod me along it was a quick example that I was trying to fit in before anyone else replied, but I failed in two regards =(

[edited by - Neosmyle on September 12, 2002 8:14:28 PM]

Share this post


Link to post
Share on other sites
No, don't do it Neosmyle's way. He's scanf()ing into uninitialized memory.

EDIT: Nevermind, he fixed it.



[edited by - scaught on September 12, 2002 8:18:00 PM]

Share this post


Link to post
Share on other sites

int getnumber (void)
{
int i;
scanf ("%d", &i);
return i;
}


Or if you REALLY want to pass a pointer to the function...


void getnumber (int * i)
{
scanf ("%d", i);
}

int main (void)
{
int i;
getnumber (&i);
return 0;
}



Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
As an addendum to the value-pointer discussion, I would like to say this:

Every variable has 2 values: the data, and the address of the data. Think about it: if the program just stores a value somewhere in memory, where will the program ever find it? To find it again, the program stores the address of the variable on the stack or the heap.

Keeping that in mind, when you declare a variable as a pointer, you are storing a variable which has an address, and the data of the variable points to another memory location. Let''s see some examples:

int a = 5;

That stores an address in memory (ie something like 0x03117135) and at that location in memory is stored the value 5.

int a = 5;
int *b = a;

This is dangerous. Declaring a pointer stores an address in memory, and at that location, is the address of another variable. Doing the above is saying that an integer value is stored in the memory location 5, or 0x00000005, and there is probably just garbage there, which is very bad. In order to store 5 into the pointer, you must use this address-of operator (&).

int a = 5;
int *b = &a;

Now, the address of a is some location in memory, and its data is 5. The address of b is some location in memory, and its data is the address of a. Therefore changing a, then accessing b will result in the changed value of a. Now look:

int a = 5;
int *b = &a;
int c = b;

The code is trying to assign 5 to c, but it is really assigning some garbage value to c, the location in memory of a. In order to use b as an int and not a memory location, you must use the dereference operator (*).

int a = 5;
int *b = &a;
int c = *b;

Now 5 will be stored in c, and changing a or b will not affect c, and changing c will not affect a or b.

Hope that helped to clear up any misunderstandings about pointers/references/values. If something else needs to be explained, let me know.

Share this post


Link to post
Share on other sites
quote:
Original post by Miserable
You probably want scanf("%i",*a);

& gives the address of the pointer. * gives you the value that the pointer is pointing to.


that did the job.

cheers for the demo neosmyle made real sense.

[edited by - themonkster on September 12, 2002 9:21:56 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Neosmyle
Every variable has 2 values: the data, and the address of the data. Think about it: if the program just stores a value somewhere in memory, where will the program ever find it? To find it again, the program stores the address of the variable on the stack or the heap.
I could be wrong, but I think that isn''t true. The address of a variable is never stored on the stack/heap, but only as hard coded offsets relative to the stack pointer, in the case of a variable pushed on the stack. Addresses of variables allocated from the heap allways needs to be stored in a pointer, allocated either on the stack (in which case its address is a hard coded offset) or on the heap (in which case we start over).

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!