Jump to content
  • Advertisement
Sign in to follow this  
Bear777

(solved) small pointer problem (c++)

This topic is 3774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am a little inexperienced regarding pointers, so I need a little help with this one. Here is my code: struct Point{ float x,y,z; }; int main() { Point vert[11]; vert[0].x = 0; } The program crashes when i do that x=0, I hope it's clear what I'm trying to do, I hope I can get a quick answer. Michel [Edited by - Bear777 on June 16, 2008 11:51:36 AM]

Share this post


Link to post
Share on other sites
Advertisement
There is nothing wrong with that code, did you omit anything?

... It would be best if you just copy and pasted your actual code.

[edit]

I should also add that that snippet has nothing to do with pointers.

Share this post


Link to post
Share on other sites
Quote:
Original post by fpsgamer
I should also add that that snippet has nothing to do with pointers.

Well, in the expression
vert[0].x = 0;

the subexpression vert decays into a pointer, and vert[0] is transformed to *(vert + 0) under the hood ;-)

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Well, in the expression
vert[0].x = 0;

the subexpression vert decays into a pointer, and vert[0] is transformed to *(vert + 0) under the hood ;-)


Are you sure about that?

Share this post


Link to post
Share on other sites
Quote:
Original post by nobodynews
Are you sure about that?

Yes.

An lvalue [...] of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.
(The exceptions are when the array is the operand of a sizeof or & operator, or is a literal string initializer for a character array.)
In an expression of the form a, the array reference "a" decays into a pointer, following the rule above, and is then subscripted just as would be a pointer variable in the expression p

Share this post


Link to post
Share on other sites
Anyway, the code was right, but for some reason if i declare that array outside a function it works. So thanks, it's solved.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bear777
if i declare that array outside a function it works.

That doesn't sound like a real solution.

Share this post


Link to post
Share on other sites
I was more looking at the statement "vert[0] is transformed to *(vert + 0) under the hood". Your link also states that when you have:

char a[] = "hello";
char *p = "world";

"It is important to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler sees the expression a[3], it emits code to start at the location "a," move three past it, and fetch the character there. When it sees the expression p[3], it emits code to start at the location "p," fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In the example above, both a[3] and p[3] happen to be the character 'l', but the compiler gets there differently."

That said, I'm sure when you do *(vert+0) a compiler will still generate the same assembly as if you did vert[0].

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by nobodynews
Are you sure about that?

Yes.

An lvalue [...] of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.
(The exceptions are when the array is the operand of a sizeof or & operator, or is a literal string initializer for a character array.)
In an expression of the form a, the array reference "a" decays into a pointer, following the rule above, and is then subscripted just as would be a pointer variable in the expression p


Point vert[11];
vert[0].x = 0;

equivalent to:

Point vert[11];
(*(vert + 0)).x = 0;

... so whats your point?

The OP has clearly omitted the thing that is the real problem.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!