Velocity Reviews > C doubt

# C doubt

sangeeta chowdhary
Guest
Posts: n/a

 07-01-2010
int arr[]={0,1,2,3,4};
int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
int **p=p;

now,

ptr++;
*ptr++;
*++ptr

are same,means we are incrementing ptr everytime.

but ++*ptr is not incrementiing ptr . Why?

Peter Nilsson
Guest
Posts: n/a

 07-01-2010
sangeeta chowdhary <(E-Mail Removed)> wrote:
> ptr++;
> *ptr++;
> *++ptr
>
> are same,means we are incrementing ptr everytime.
>
> but ++*ptr is not incrementiing ptr . Why?

A better question is why do you think it increments ptr?

Do you think (*ptr)++ should increment ptr as well?

If not, why not?

--
Peter

Ike Naar
Guest
Posts: n/a

 07-01-2010
In article <(E-Mail Removed)>,
sangeeta chowdhary <(E-Mail Removed)> wrote:
>int arr[]={0,1,2,3,4};
>int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
>int **p=p;

This is an error; you probably meant ``int **ptr = p;''.

>now,
>
>ptr++;
>*ptr++;
>*++ptr
>
>are same,means we are incrementing ptr everytime.

There are subtle differences.
The first expression yields ptr, and, as a side effect, increments ptr.
The second expression yields *ptr, and, as a side effect, increments ptr.
The third expression yields *(ptr+1), and a a side effect, increments ptr.

So, all three statements increment ptr as a side effect, but each
statement produces a different value; it is only because you ignore
the produced values, that the three statements are effectively the same.

>but ++*ptr is not incrementiing ptr . Why?

++*ptr means ++(*ptr), that is, it increments *ptr,
the value that ptr points at, not ptr itself.

sangeeta chowdhary
Guest
Posts: n/a

 07-01-2010
On Jul 1, 12:07*pm, (E-Mail Removed) (Ike Naar) wrote:
> In article <(E-Mail Removed)>,
> sangeeta chowdhary *<(E-Mail Removed)> wrote:
>
> >int arr[]={0,1,2,3,4};
> >int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
> >int **p=p;

>
> This is an error; you probably meant ``int **ptr = p;''.
>
> >now,

>
> >ptr++;
> >*ptr++;
> >*++ptr

>
> >are same,means we are incrementing ptr everytime.

>
> There are subtle differences.
> The first expression yields ptr, and, as a side effect, increments ptr.
> The second expression yields *ptr, and, as a side effect, increments ptr.
> The third expression yields *(ptr+1), and a a side effect, increments ptr..
>
> So, all three statements increment ptr as a side effect, but each
> statement produces a different value; it is only because you ignore
> the produced values, that the three statements are effectively the same.
>
> >but ++*ptr is not incrementiing ptr . Why?

>
> ++*ptr means ++(*ptr), that is, it increments *ptr,
> the value that ptr points at, not ptr itself.

Thanks a lot.

sangeeta chowdhary
Guest
Posts: n/a

 07-01-2010
On Jul 1, 11:59*am, Peter Nilsson <(E-Mail Removed)> wrote:
> sangeeta chowdhary <(E-Mail Removed)> wrote:
> > ptr++;
> > *ptr++;
> > *++ptr

>
> > are same,means we are incrementing ptr everytime.

>
> > but ++*ptr is not incrementiing ptr . Why?

>
> A better question is why do you think it increments ptr?
>
> Do you think (*ptr)++ should increment ptr as well?
>
> If not, why not?
>
> --
> Peter

Thanks a lot.

Vincenzo Mercuri
Guest
Posts: n/a

 07-01-2010
sangeeta chowdhary wrote:
> int arr[]={0,1,2,3,4};
> int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
> int **p=p;
>
> now,
>
> ptr++;
> *ptr++;
> *++ptr
>
> are same,means we are incrementing ptr everytime.
>
> but ++*ptr is not incrementiing ptr . Why?

*************************************
* A Frequently Unasked Memo *
*************************************

ptr, s, t being pointers (really?)

------------------------------------
1) ++*ptr == ++(*ptr)
------------------------------------

So, ++*s = ++*t; means:

*s = *s + 1; *t = *t + 1;
*t = *t + 1; or *s = *s + 1;
*s = *t; *s = *t;

------------------------------------
2) *++ptr == *(++ptr)
------------------------------------

So, *++s = *++t; means:

s = s + 1; t = t + 1;
t = t + 1; or s = s + 1;
*s = *t; *s = *t;

------------------------------------
3) *ptr++ == *(ptr++)
------------------------------------

So, *s++ = *t++; means:

*s = *t; *s = *t;
s = s + 1; or t = t + 1;
t = t + 1; s = s + 1;

------------------------------------
4) (*ptr)++ [no other way to get it]
------------------------------------

So, (*s)++ = (*t)++; means:

*s = *t; *s = *t;
*s = *s + 1; or *t = *t + 1;
*t = *t + 1; *s = *s + 1;

--
Vincenzo Mercuri

Vincenzo Mercuri
Guest
Posts: n/a

 07-01-2010
Il 02/07/2010 3.58, pete ha scritto:
> Vincenzo Mercuri wrote:
>>
>> sangeeta chowdhary wrote:
>>> int arr[]={0,1,2,3,4};
>>> int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
>>> int **p=p;
>>>
>>> now,
>>>
>>> ptr++;
>>> *ptr++;
>>> *++ptr
>>>
>>> are same,means we are incrementing ptr everytime.
>>>
>>> but ++*ptr is not incrementiing ptr . Why?

>>
>> *************************************
>> * A Frequently Unasked Memo *
>> *************************************
>>
>> ptr, s, t being pointers (really?)
>>
>> ------------------------------------
>> 1) ++*ptr == ++(*ptr)
>> ------------------------------------
>>
>> So, ++*s = ++*t; means:
>>
>> *s = *s + 1; *t = *t + 1;
>> *t = *t + 1; or *s = *s + 1;
>> *s = *t; *s = *t;
>>
>> ------------------------------------
>> 2) *++ptr == *(++ptr)
>> ------------------------------------
>>
>> So, *++s = *++t; means:
>>
>> s = s + 1; t = t + 1;
>> t = t + 1; or s = s + 1;
>> *s = *t; *s = *t;
>>
>> ------------------------------------
>> 3) *ptr++ == *(ptr++)
>> ------------------------------------
>>
>> So, *s++ = *t++; means:
>>
>> *s = *t; *s = *t;
>> s = s + 1; or t = t + 1;
>> t = t + 1; s = s + 1;
>>
>> ------------------------------------
>> 4) (*ptr)++ [no other way to get it]
>> ------------------------------------
>>
>> So, (*s)++ = (*t)++; means:
>>
>> *s = *t; *s = *t;
>> *s = *s + 1; or *t = *t + 1;
>> *t = *t + 1; *s = *s + 1;

>
> Your meanings associated with these following statements:
> ++*s = ++*t;
> (*s)++ = (*t)++;
> suggest that you think that those statements can compile.
>

Wow! Indeed I thought!
I have been programming since November and I've never
noticed they were not lvalues! (I've never used them)
THANKS! (Shame on Me!)

--
Vincenzo Mercuri

Vincenzo Mercuri
Guest
Posts: n/a

 07-01-2010
Il 02/07/2010 3.58, pete ha scritto:
> Vincenzo Mercuri wrote:
>>
>> sangeeta chowdhary wrote:
>>> int arr[]={0,1,2,3,4};
>>> int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
>>> int **p=p;
>>>
>>> now,
>>>
>>> ptr++;
>>> *ptr++;
>>> *++ptr
>>>
>>> are same,means we are incrementing ptr everytime.
>>>
>>> but ++*ptr is not incrementiing ptr . Why?

>>
>> *************************************
>> * A Frequently Unasked Memo *
>> *************************************
>>
>> ptr, s, t being pointers (really?)
>>
>> ------------------------------------
>> 1) ++*ptr == ++(*ptr)
>> ------------------------------------
>>
>> So, ++*s = ++*t; means:
>>
>> *s = *s + 1; *t = *t + 1;
>> *t = *t + 1; or *s = *s + 1;
>> *s = *t; *s = *t;
>>
>> ------------------------------------
>> 2) *++ptr == *(++ptr)
>> ------------------------------------
>>
>> So, *++s = *++t; means:
>>
>> s = s + 1; t = t + 1;
>> t = t + 1; or s = s + 1;
>> *s = *t; *s = *t;
>>
>> ------------------------------------
>> 3) *ptr++ == *(ptr++)
>> ------------------------------------
>>
>> So, *s++ = *t++; means:
>>
>> *s = *t; *s = *t;
>> s = s + 1; or t = t + 1;
>> t = t + 1; s = s + 1;
>>
>> ------------------------------------
>> 4) (*ptr)++ [no other way to get it]
>> ------------------------------------
>>
>> So, (*s)++ = (*t)++; means:
>>
>> *s = *t; *s = *t;
>> *s = *s + 1; or *t = *t + 1;
>> *t = *t + 1; *s = *s + 1;

>
> Your meanings associated with these following statements:
> ++*s = ++*t;
> (*s)++ = (*t)++;
> suggest that you think that those statements can compile.
>

Just a question (for my personal benefit, in order to understand)
why they are not lvalues?

--
Vincenzo Mercuri

Vincenzo Mercuri
Guest
Posts: n/a

 07-01-2010
Il 02/07/2010 3.00, Vincenzo Mercuri ha scritto:
> Il 02/07/2010 3.58, pete ha scritto:
>> Vincenzo Mercuri wrote:
>>>
>>> sangeeta chowdhary wrote:
>>>> int arr[]={0,1,2,3,4};
>>>> int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
>>>> int **p=p;
>>>>
>>>> now,
>>>>
>>>> ptr++;
>>>> *ptr++;
>>>> *++ptr
>>>>
>>>> are same,means we are incrementing ptr everytime.
>>>>
>>>> but ++*ptr is not incrementiing ptr . Why?
>>>
>>> *************************************
>>> * A Frequently Unasked Memo *
>>> *************************************
>>>
>>> ptr, s, t being pointers (really?)
>>>
>>> ------------------------------------
>>> 1) ++*ptr == ++(*ptr)
>>> ------------------------------------
>>>
>>> So, ++*s = ++*t; means:
>>>
>>> *s = *s + 1; *t = *t + 1;
>>> *t = *t + 1; or *s = *s + 1;
>>> *s = *t; *s = *t;
>>>
>>> ------------------------------------
>>> 2) *++ptr == *(++ptr)
>>> ------------------------------------
>>>
>>> So, *++s = *++t; means:
>>>
>>> s = s + 1; t = t + 1;
>>> t = t + 1; or s = s + 1;
>>> *s = *t; *s = *t;
>>>
>>> ------------------------------------
>>> 3) *ptr++ == *(ptr++)
>>> ------------------------------------
>>>
>>> So, *s++ = *t++; means:
>>>
>>> *s = *t; *s = *t;
>>> s = s + 1; or t = t + 1;
>>> t = t + 1; s = s + 1;
>>>
>>> ------------------------------------
>>> 4) (*ptr)++ [no other way to get it]
>>> ------------------------------------
>>>
>>> So, (*s)++ = (*t)++; means:
>>>
>>> *s = *t; *s = *t;
>>> *s = *s + 1; or *t = *t + 1;
>>> *t = *t + 1; *s = *s + 1;

>>
>> Your meanings associated with these following statements:
>> ++*s = ++*t;
>> (*s)++ = (*t)++;
>> suggest that you think that those statements can compile.
>>

>
> Just a question (for my personal benefit, in order to understand)
> why they are not lvalues?
>

modifiable lvalues (as compiler complains)

--
Vincenzo Mercuri

Vincenzo Mercuri
Guest
Posts: n/a

 07-01-2010
Il 02/07/2010 5.41, Ian Collins ha scritto:
> On 07/ 2/10 10:30 AM, Vincenzo Mercuri wrote:
>> Il 02/07/2010 3.58, pete ha scritto:
>>>
>>> Your meanings associated with these following statements:
>>> ++*s = ++*t;
>>> (*s)++ = (*t)++;
>>> suggest that you think that those statements can compile.
>>>

>>
>> Just a question (for my personal benefit, in order to understand)
>> why they are not lvalues?

>
> (*s)++ yields a value, not an object. For example if *s is 4, how can
> something be assigned to 5?
>

I am being a little confused.

doesn't *s++ yield a value?
*s first, and then increase s by 1.

by value, don't you mean a number (in this case)?

thanks anyway.
you made me want to open
my books again!

--
Vincenzo Mercuri