Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C doubt

Reply
Thread Tools

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?
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
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.
 
Reply With Quote
 
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.
 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
doubt Ajith Nair ASP .Net 6 03-29-2005 01:25 PM
doubt about namespace google manasa sreenivas via .NET 247 ASP .Net 1 05-15-2004 03:05 PM
Doubt to go .Net technology Igbal ASP .Net 1 11-17-2003 06:56 AM



Advertisments