Pointer problem

R

Rishu

hello everybody can anybody tell me about the following programme.

int *ptr;
int a;
ptr=&a;

then what is the diffrence between ptr++ & (*ptr)++
 
S

Salvatore Di Fazio

hello everybody can anybody tell me about the following programme.

int *ptr;
int a;
ptr=&a;

then what is the diffrence between ptr++ & (*ptr)++

(*ptr)++ == add one to the value pointed
ptr++ == next element of the pointer. Ex:

int a[2] = {1, 2};
int *p = a;
(*p)++;
printf("c[0] = %i", a[0]); // 2
(*p)--;
printf("c[0] = %i", a[0]); // 1

p++;
printf("c[0] = %i", *p); // 2
 
R

Richard Heathfield

Rishu said:
hello everybody can anybody tell me about the following programme.

int *ptr;
int a;
ptr=&a;

then what is the diffrence between ptr++ & (*ptr)++

When you do ptr++ you are effectively pretending that a is an array of 1
int, and moves ptr on to point one past the end of this array. That's
legal as long as you don't deref ptr or move it any further along.

(*ptr)++, on the other hand, invokes undefined behaviour by attempting
to modify an indeterminate value - i.e. the value of the object pointed
to by ptr - i.e. a.
 
C

Chris Dollin

Rishu said:
hello everybody can anybody tell me about the following programme.

int *ptr;
int a;
ptr=&a;

This isn't a program; the assignment statement isn't inside a
function. So fixing it in the obvious way:

int main(void)
{
int *ptr;
int a;
ptr = &a;
// then ptr++ and (*ptr)++
return 0;
}
then what is the diffrence between ptr++ & (*ptr)++

One increments `ptr`. The other increments whatever `ptr` points
to, in this case `a`, getting undefined behaviour because `a`
is uninitialised.

[If `a` had been declared outside `main`, then it would be
(implicitly) initialised to zero.]

[I choose to write `(*ptr)++` as `*ptr += 1` because I think
it's easier to read and because the result of the increment
isn't used elsehere, but that's just my religion.]

[I'd also start the body of main as

int a;
int *ptr = &a;

because, why defer the initialisation of `ptr`? And probably
I'd initialise `a`, but in this case I don't know what to.]

--
"I just wonder when we're going to have to sit down and re-evaluate /Sahara/
our decision-making paradigm."

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
 
R

Richard Heathfield

Chris Dollin said:
[I choose to write `(*ptr)++` as `*ptr += 1` because I think
it's easier to read and because the result of the increment
isn't used elsehere, but that's just my religion.]

The result of the increment is neither here nor there, of course, since
*ptr += 1 also yields an ignored value.

My own preference is ++*ptr, because it removes not only the parentheses
but also the need for them (and, indeed, the maintainer's page-turning
curiosity about whether they are necessary).
 
C

Chris Dollin

Richard said:
Chris Dollin said:
[I choose to write `(*ptr)++` as `*ptr += 1` because I think
it's easier to read and because the result of the increment
isn't used elsehere, but that's just my religion.]

The result of the increment is neither here nor there, of course, since
*ptr += 1 also yields an ignored value.

Yes.

/Stylistically/, I prefer the result of an assignment usually to be
ignored, and the result of a ++ (or --) not to be ignored. It's a hint,
not a universal truth, as witness my willingness to use assignments
in conditions and argument expressions when the alternative is clumsy.

You don't have to write code the same way as I do. That way, if
you do, I know I have gained another Follower In The One True Way.
 
R

Richard Heathfield

Chris Dollin said:
Richard said:
Chris Dollin said:
[I choose to write `(*ptr)++` as `*ptr += 1` because I think
it's easier to read and because the result of the increment
isn't used elsehere, but that's just my religion.]

The result of the increment is neither here nor there, of course,
since *ptr += 1 also yields an ignored value.

Yes.

/Stylistically/, I prefer the result of an assignment usually to be
ignored, and the result of a ++ (or --) not to be ignored.

Schism in the Church!

Seriously, yes, this is more of a denominational difference than a
fundamental disagreement over doctrine. We all have our own liturgies
to bear.

You don't have to write code the same way as I do. That way, if
you do, I know I have gained another Follower In The One True Way.

No, *I* have. :)
 
S

Sonam

hello everybody can anybody tell me about the following programme.

int *ptr;
int a;
ptr=&a;

then what is the diffrence between ptr++ & (*ptr)++
ptr++ would make pointer to point to next int (i.e after 4 bytes) in
the memory
(*ptr)++ would mean a++ i.e the value of content pointed to by the
pointer would be incremented.
 
B

Barry Schwarz

ptr++ would make pointer to point to next int (i.e after 4 bytes) in
the memory

Saying it points to the next int implies it can be dereferenced to
yield the value of that int. That is incorrect. It points one byte
beyond the end of a and is valid only as long as it is not
dereferenced.
(*ptr)++ would mean a++ i.e the value of content pointed to by the
pointer would be incremented.


Remove del for email
 

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,776
Messages
2,569,603
Members
45,198
Latest member
JaimieWan8

Latest Threads

Top