Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

Pointer problem


hello everybody can anybody tell me about the following programme.

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

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

On 24 Mag, 13:38, Rishu <abhineet_@yahoo.co.in> wrote:

> 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

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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

Rishu wrote:
> 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

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).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

Richard Heathfield wrote:
> 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.

--
"Based on their behaviour so far -- I have no idea."                   /Sahara/

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

Chris Dollin said:

> Richard Heathfield wrote:

>> 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.

<snip>

> 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. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

On May 24, 4:38 pm, Rishu <abhineet_@yahoo.co.in> wrote:
> 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.
Sonam said:

<snip>

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

Well, after sizeof(int) bytes, anyway.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.

On 25 May 2007 05:09:22 -0700, Sonam <kumarso@gmail.com> wrote:

>On May 24, 4:38 pm, Rishu <abhineet_@yahoo.co.in> wrote:
>> 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

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
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc