Trying to understand pointers for function paramaters

J

John Bode

Richard Hengeveld said:
Hi all,

I'm trying to understand how pointers for function parameters work. As I
understand it, if you got a function like:

void f(int *i)
{
*i = 0;
}

int main()
{
int a;
f(&a);
return 0;
}

It does what you want (namely altering the value of a).
I find this illogical. As far as I can understand, the address of "a" is
passed, and "*i" is set with this address not "i", as it should be in my
understanding.
What am I missing?

I think you're getting hung up with declarator syntax as much as
anything else, and that you're looking at the formal parameter
declaration as an actual dereference operation, which is why you're
confused.

C declaration statements follow a "declaration mimics use" paradigm.
When you dereference the pointer to get the value it's pointing to,
you use the expression "*i". The idea is that the declaration of "i"
(a pointer) should closely mirror how it's actually used in the code;
therefore, the declaration looks like this:

int *i

The int-ness of i is specified in the type specifier "int"; the
pointer-ness of i is specified in the declarator "*i". This statement
introduces the symbol "i" as having type pointer-to-int (although the
declaration reads as pointer-to-type-int).

When you pass your value (&a, type pointer-to-int) to this function,
it is written to the formal parameter "i" (type pointer-to-int), *not*
to the expression "*i" (type int).

Hope that helps.
 
S

Stephen Sprunk

Richard Hengeveld said:
Thanks for replying.
Yes, that is exactly what is confusing me.
I understand you set a pointer (if you're not passing to functions) by:

int a, *p;
p = &a;

and not:

p* = &a

This is a syntax error; I assume you meant:

*p = &a;

This is a type mismatch; you're trying to assign an address (a) to an
integer (*p) without a cast. Now, if you did:

p = &a;

This is correct. Then you can do:

*p = 1;

And then you will find that a==1. This is no different than doing:

*(&a) = 1;

S
 
C

CBFalconer

Christopher said:
Perhaps there could be a section of the FAQ dedicated to
identifying posters to plonk?

Too dynamic. The best I can do is publish my own c.l.c plonk
list. Trollsdale isn't on it because he requires watching else
his alteration tricks will go unnoticed. The FAQ plonks (Answers,
FAQ list) are because I have a copy and don't need any more, and
they are big.

c:\netscape\users\cbf\news\host-netnews.att.net>grep condition=
comp.lang.c.dat
condition=" OR (subject,contains,comp.lang.c Answers)"
condition=" OR (subject,contains,My china ex. girlfriend)"
condition=" OR (from,contains,Kenny McCormack)"
condition=" OR (subject,contains,comp.lang.c FAQ list)"
condition=" OR (from,contains,RoSsIaCrIiLoIA)"
condition=" OR (from,contains,lxrocks)"
condition=" OR (from,contains,Generic Usenet Account)"
condition=" OR (from,contains,SM Ryan)"
condition=" OR (from,contains,Skybuck Flying)"
condition=" OR (subject,contains,C++ wins over C)"
condition=" OR (subject,contains,What is an object)"
 
R

Richard Hengeveld

Thanks all for helping me out and being patiant with me.
To be clear, I was not trying to attack c logics or concepts. I was just
trying to learn c. Thanks again.
Richard
 
R

Richard Hengeveld

Thanks all for helping me out and being patiant with me.
To be clear, I was not trying to attack C logics or concepts. Just trying to
learn C. Thanks again
 
M

Mabden

CBFalconer said:
And just when I thought Trollsdale was showing signs of
reformation. He seems to be a dedicated recidivist. Can we apply
the 'three strikes' law somehow?
Shut up! It was the same function with a variable changed.

Where's your input? What is your content?

Nothing!

**** off!
 
K

Keith Thompson

Mabden said:
Shut up! It was the same function with a variable changed.

Where's your input? What is your content?

Nothing!

**** off!

Mabden, whoever you are, I am sick and tired of your insults. You've
sometimes shown signs of being worth talking to, but if I had a
killfile, you would have been in it several times over by now.
Tisdale deliberately forged a quotation, making it appear that the
previous poster had written something he didn't. He changed the
formatting of the OP's code (the original was better). He changed a
variable name; the new name was clearer, but he didn't bother to say
that. This is unacceptable behavior, and I don't choose to sit
quietly by and let other readers be lied to.

If you don't like our replies, feel free to ignore them. Killfile us
if that makes you happy.

As for your obscene language, it doesn't particularly bother me,
especially if it's used creatively (though I try to avoid it in public
forums); you're just not very good at it.
 
M

Mabden

Keith Thompson said:
Mabden, whoever you are, I am sick and tired of your insults. You've
sometimes shown signs of being worth talking to, but if I had a
killfile, you would have been in it several times over by now.

Well, stop trolling Tisdale unless you post actual content.
Tisdale ....

Get over it.
If you don't like our replies, feel free to ignore them. Killfile us
if that makes you happy.
Ditto.

As for your obscene language, it doesn't particularly bother me,
especially if it's used creatively (though I try to avoid it in public
forums); you're just not very good at it.

Well, I use strong language with my friends (yes, I have some...) and
sometimes forget how some posters don't in their Real Life (tm).

I just have so many newsgroups to get through and a limited timeslice.
To have to read a long post about how this function was changed from "i"
to "p" and then compare them, and then find no difference, and then
realize, "Oh, it's just KT on his period again" gets really old.

Can't you just killfile Tisdale and be done with it, Nanny?
 
J

John Bode

Mabden said:
Shut up! It was the same function with a variable changed.

Even so, Tisdale should have marked his changes. It raises red flags
with me because I've dealt with posters in other fora who
*significantly* edit quoted material for patently dishonest purposes
(i.e., change a withering criticism into wholehearted support).

If you edit quoted material *FOR ANY REASON*, even if it's just to
change a stinking variable name, you should mark those edits
*somehow*.

When someone comes along to read the thread after the original article
has fallen off the server, what they'll be reading is Tisdale's
unmarked edit of the original, and if that reader has problems with
Tisdale's style or corrections, they'll erroneously attribute those
problems to the OP. That doesn't help the OP much.
Where's your input? What is your content?

Nothing!

**** off!

Where's *your* input? What is *your* content?
 
M

Mabden

John Bode said:
"Mabden" <mabden@sbc_global.net> wrote in message

Even so, Tisdale should have marked his changes. It raises red flags
with me because I've dealt with posters in other fora who
*significantly* edit quoted material for patently dishonest purposes
(i.e., change a withering criticism into wholehearted support).

If you edit quoted material *FOR ANY REASON*, even if it's just to
change a stinking variable name, you should mark those edits
*somehow*.

When someone comes along to read the thread after the original article
has fallen off the server, what they'll be reading is Tisdale's
unmarked edit of the original, and if that reader has problems with
Tisdale's style or corrections, they'll erroneously attribute those
problems to the OP. That doesn't help the OP much.


Where's *your* input? What is *your* content?

Too subtle for you? I can be that way, as I wouldn't wish to offend.

I was saying that a no content post wastes my time, and that I'm a Big
Boy and can distinguish a good post from a bad post all on my own. I
don't need someone following along to say, "Hey, that last post you read
might not be fully accurate! I don't want you to believe every word of
that last post! Hey, something on the internet may be misrepresenting
itself as fact, when in fact it is fiction! There's this one guy, and
you know, for some reason he changes everything he touches, and DON'T
LISTEN TO HIM!!!!! PLEEEEEZZZZEEEE!!!! --- There, I feel good about
myself because I've warned the world!"

That is what I object to.
 
D

Default User

Mabden wrote:

Well, stop trolling Tisdale unless you post actual content.


I thought Mabden was starting to get a clue, but has gone berserk over
the weekend.


*plonk*




Brian Rodenborn
 
M

Mabden

Default User said:
Mabden wrote:




I thought Mabden was starting to get a clue, but has gone berserk over
the weekend.

OK, I do tend to do that. My bad.


Why not just plonk Tisdale? Then you won't complain about minutia (sp?)
and I won't feel it necessary to respond what a bunch a whinging
bastards you all are...
 
F

Flash Gordon

On Tue, 05 Oct 2004 08:47:23 GMT

Why not just plonk Tisdale? Then you won't complain about minutia
(sp?) and I won't feel it necessary to respond what a bunch a whinging
bastards you all are...

Tisdale doesn't get plonked by a lot of people because if no one pointed
out his errors people who did not know any better might believe him and
have problems as a result.

So people who are gratuitously insulting but either provide no advice or
correct advice get plonked, people who provide incorrect advice
generally don't get plonked even if they are gratuitously rude.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top