Pointer question

E

Erik

Hi,
to swap the integer variables a,b,
in Kerninghan-Ritchie I find the function example

Swap(&a,&b);

unsing a function

void swap (int *px, int *py)
{
int temp;
temp:= *px;
*px = *py;
*py = temp
}

Now here I have a problem understanding that.
The function gets two !Pointers! an arguments.

So as far as I see *px=&a will happen, but *px is an integer
variable and &a is a pointer. But I think px=&a would be
correct.

????
Thanks in advance
Erik
 
C

Christian Bau

Hi,
to swap the integer variables a,b,
in Kerninghan-Ritchie I find the function example

Swap(&a,&b);

unsing a function

void swap (int *px, int *py)
{
int temp;
temp:= *px;
*px = *py;
*py = temp
}

You should really try hard to get your spelling correct. The code that
you wrote has no chance of ever compiling, and even if it did, what does
the function "swap" have to do with the function "Swap" that you are
trying to call?

I am sure that Mr. Ritchie has never written a book together with anyone
by the name of Kerninghan. I know the word "sing" but I have no idea
what you mean by "unsing". If you think I am too pedantic, then
programming is not for you.
 
R

Ramiro Rodriguez

So as far as I see *px=&a will happen, but *px is an integer
variable and &a is a pointer. But I think px=&a would be
correct.

????
Thanks in advance
Erik

In the function declaration px is declared as a variable of type int*. *px
is not the variable that gets set when the function is called it is px
which gets set to &a.

-RR
 
R

Rowland

Christian Bau said:
You should really try hard to get your spelling correct. The code that
you wrote has no chance of ever compiling, and even if it did, what does
the function "swap" have to do with the function "Swap" that you are
trying to call?

That's a fair point as code should be correct when it's posted.
I am sure that Mr. Ritchie has never written a book together with anyone
by the name of Kerninghan. I know the word "sing" but I have no idea
what you mean by "unsing". If you think I am too pedantic, then
programming is not for you.

I don't think this is fair though, as the chap appears to be German, and
probably
has a certain amount of difficulty with English, it not being his native
language.
Criticism of one typographical error and one misspelling, which do not
affect comprehension of
his message, is unwarranted. If it was unreadable garbage I would agree with
you,
but it's not.

Rowland.
 
C

Christian Bau

"Rowland said:
I don't think this is fair though, as the chap appears to be German, and
probably
has a certain amount of difficulty with English, it not being his native
language.
Criticism of one typographical error and one misspelling, which do not
affect comprehension of
his message, is unwarranted. If it was unreadable garbage I would agree with
you,
but it's not.

Spelling "Kernighan" correctly has nothing to do with your native
language. And I am one hundred percent sure that there is not one German
who knows the word "using" and would believe that it is spelt as
"unsing".
 
R

Rowland

Christian Bau said:
Spelling "Kernighan" correctly has nothing to do with your native
language. And I am one hundred percent sure that there is not one German
who knows the word "using" and would believe that it is spelt as
"unsing".

I do agree that there is not one German. Perhaps two, or even three ("If you
think I am too pedantic, then programming is not for you." (sorry, couldn't
resist!)).

You're right in that I should have said two typos. I still feel it was a
slight over-reaction to two minor mistakes, and unhelpful to the discussion
though. I don't mean to flame, and I agree that it's a very subjective line
as to when somebodies lack of grammar/punctuation/spelling/etc. becomes a
problem. All IMHO, naturally ;-)

Warm regards,

Rowland.
 
S

SM Ryan

(e-mail address removed) (Erik) wrote:
# Hi,
# to swap the integer variables a,b,
# in Kerninghan-Ritchie I find the function example
#
# Swap(&a,&b);
#
# unsing a function
#
# void swap (int *px, int *py)
# {
# int temp;
# temp:= *px;
# *px = *py;
# *py = temp
# }
#
# Now here I have a problem understanding that.
# The function gets two !Pointers! an arguments.
#
# So as far as I see *px=&a will happen, but *px is an integer
# variable and &a is a pointer. But I think px=&a would be
# correct.

If you're referring to the explanation of function call by inlining
the function text, what you've really got is

Swap(&a,&b);
---------------------------
{
int *px = &a;
int *py = &b;
{
int temp;
temp = *px;
*px = *py;
*py = temp
}
}

So that implied parameter assignment is equivalent to the initialiser
in a declaration, not to the assignment expression in a statement.
In a declaration, *px does not mean dereference px, but rather it
declares that px can be dereferenced in subsequent statements.
 
M

Martin Ambuhl

Erik said:
void swap (int *px, int *py)
{
int temp;
temp:= *px;
*px = *py;
*py = temp
}

Now here I have a problem understanding that.
The function gets two !Pointers! an arguments.

So as far as I see *px=&a will happen, but *px is an integer
variable and &a is a pointer. But I think px=&a would be
correct.

It is an error to think that the assignment "*px=&a will happen". 'px'
is a pointer to int, and it is px (not *px) set to &a. What you think
"would be correct" is in fact what happens.
 
M

Martin Ambuhl

Christian said:
You should really try hard to get your spelling correct.

While case is important, so the mistake of 'Swap' for 'swap' matters,
the typo of 'unsing' and misspelling 'Kerningham' hardly matter.
However, the misspelling of ':=' for the assignment operator is not only
important, but suggests a background in a language from Switzerland that
does _not_ distinguish between 'Swap' and 'swap'.
 

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

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top