Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > 1 more doubt !

Reply
Thread Tools

1 more doubt !

 
 
maadhuu
Guest
Posts: n/a
 
      09-14-2005
firstly, i am thankful to all those who answered the 1st set of doubts. And
i am not yet enlightened to that extent , coz ' i keep getting doubts.

is the following defined in the language ??
int main()
{
int a = 1;
int *p = &a;
p++;
printf("%d",*p);
return 0;
}

thanking you,
ranjan.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      09-14-2005
In article <(E-Mail Removed) outprogramming.com>,
maadhuu <(E-Mail Removed)> wrote:
>is the following defined in the language ??


>int main()


You should use int main(void) to be consistant with the C standard.

>{
> int a = 1;


Valid.

> int *p = &a;


Valid.

> p++;


Valid.

> printf("%d",*p);


Not valid. It is legal for an object pointer to point one location
past the end of the object, but it is not legal to dereference the
pointer so formed.

> return 0;
>}

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      09-14-2005
maadhuu wrote:
> firstly, i am thankful to all those who answered the 1st set of doubts. And
> i am not yet enlightened to that extent , coz ' i keep getting doubts.
>
> is the following defined in the language ??
> int main()


this should be "int main (void)"

> {
> int a = 1;
> int *p = &a;


So far so good.

> p++;


What you are doing here is advancing the pointer to point to the space
right after the location of a. This is well-defined as a special case,
p+=2 would be undefined behavior.

> printf("%d",*p);


You forgot to "#include <stdio.h>".
Dereferencing p here is undefined behavior.

> return 0;
> }


Robert Gamble

 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      09-14-2005
Robert Gamble wrote:
> maadhuu wrote:
> > firstly, i am thankful to all those who answered the 1st set of doubts. And
> > i am not yet enlightened to that extent , coz ' i keep getting doubts.
> >
> > is the following defined in the language ??
> > int main()

>
> this should be "int main (void)"
>
> > {
> > int a = 1;
> > int *p = &a;

>
> So far so good.
>
> > p++;

>
> What you are doing here is advancing the pointer to point to the space
> right after the location of a. This is well-defined as a special case,
> p+=2 would be undefined behavior.


Why is it well defined?
Section 6.5.6 of the C99 standard (Additive operators) says

"Moreover, if the expression P points to the last element of an
array object, the expression (P)+1 points one past the last
element of the array object"

I note that a is not an array object, so p doesn't point to one...
Am I confused here?

>
> > printf("%d",*p);

>
> You forgot to "#include <stdio.h>".
> Dereferencing p here is undefined behavior.
>


If it weren't for the deferencing problem, probably wants to be
printf("%d\n", *p);
Without the newline, you may not see anything.

-David

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      09-14-2005
In article <(E-Mail Removed) .com>,
David Resnick <(E-Mail Removed)> wrote:
>Robert Gamble wrote:
>> > p++;


>> What you are doing here is advancing the pointer to point to the space
>> right after the location of a. This is well-defined as a special case,
>> p+=2 would be undefined behavior.


>Why is it well defined?
>Section 6.5.6 of the C99 standard (Additive operators) says


>"Moreover, if the expression P points to the last element of an
>array object, the expression (P)+1 points one past the last
>element of the array object"


>I note that a is not an array object, so p doesn't point to one...
>Am I confused here?


C89 3.3.6

For the purposes of these operators, a pointer to a nonarray object
behaves the same as a pointer to the first element of an array of
length one with a typoe of the object as its element type.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
 
Reply With Quote
 
maadhuu
Guest
Posts: n/a
 
      09-14-2005
thanx,
but printing just p, is valid right ???
and also one more doubt .
is a[i] ++i ; valid ??? and one more doubt .

int i = 10;
int a = ++i + ++i;
is this also valid ??? because i++ + i++ is invalid ,but there, the side
effects are present ,and here there shouldn't be any side effects .


 
Reply With Quote
 
maadhuu
Guest
Posts: n/a
 
      09-14-2005
also, i had the same doubt :

Moreover, if the expression P points to the last element of an
array object, the expression (P)+1 points one past the last
element of the array object .

as was quoted by Mr David from
Section 6.5.6 of the C99 standard (Additive operators)

so, does that p++ apply only to arrays or to single objects too ?
ranjan.

 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-14-2005
"maadhuu" <(E-Mail Removed)> wrote:

Please preserve some context when posting replies.

>thanx,
>but printing just p, is valid right ???
>and also one more doubt .


Question. You have questions, not doubts.

>is a[i] ++i ; valid ??? and one more doubt .


ITYM: a[i] = ++i;

>int i = 10;
>int a = ++i + ++i;
>is this also valid ??? because i++ + i++ is invalid ,but there, the side
>effects are present ,and here there shouldn't be any side effects .


++i has the obvious side effect of incrementing i. Above assignments
invoke undefined behaviour for the very same reasons as the examples
you posted in the other thread. And would you, please, read the FAQ,
as I already suggested?

Best regards
--
Irrwahn Grausewitz ((E-Mail Removed))
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc
 
Reply With Quote
 
David Resnick
Guest
Posts: n/a
 
      09-14-2005

Walter Roberson wrote:
> In article <(E-Mail Removed) .com>,
> David Resnick <(E-Mail Removed)> wrote:
> >Robert Gamble wrote:
> >> > p++;

>
> >> What you are doing here is advancing the pointer to point to the space
> >> right after the location of a. This is well-defined as a special case,
> >> p+=2 would be undefined behavior.

>
> >Why is it well defined?
> >Section 6.5.6 of the C99 standard (Additive operators) says

>
> >"Moreover, if the expression P points to the last element of an
> >array object, the expression (P)+1 points one past the last
> >element of the array object"

>
> >I note that a is not an array object, so p doesn't point to one...
> >Am I confused here?

>
> C89 3.3.6
>
> For the purposes of these operators, a pointer to a nonarray object
> behaves the same as a pointer to the first element of an array of
> length one with a typoe of the object as its element type.
> --


I see now, thanks.

-David

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-14-2005
"maadhuu" <(E-Mail Removed)> writes:
> firstly, i am thankful to all those who answered the 1st set of doubts. And
> i am not yet enlightened to that extent , coz ' i keep getting doubts.
>
> is the following defined in the language ??
> int main()
> {
> int a = 1;
> int *p = &a;
> p++;
> printf("%d",*p);
> return 0;
> }


No.

First, calling printf() without a prototype invokes undefined behavior
(though it's relatively unlikely to cause any visible problems). Add
"#include <stdio.h>" to the top of your program.

The declaration "int main()" is valid, but "int main(void)" is better
and more explicit.

It's implementation-defined whether you need a newline at the end of
your output. Change "%d" to "%d\n".

Finally (and this is your real problem), after "p++;", p points to a
nonexistent object just after a. You're allowed to create a pointer
just past the end of a real object, but any attempt to dereference
such a pointer invokes undefined behavior.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
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
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
New Releases: More No Doubt, Jonny Quest & Sealab: Updated complete downloadable R1 DVD DB & info lists Doug MacLean DVD Video 2 03-06-2004 07:46 PM



Advertisments