(solved) small pointer problem (c++)
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]
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.
... 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.
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 ;-)
Quote:Original post by DevFred
Well, in the expressionvert[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?
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
Anyway, the code was right, but for some reason if i declare that array outside a function it works. So thanks, it's solved.
Quote:Original post by Bear777
if i declare that array outside a function it works.
That doesn't sound like a real solution.
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].
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].
Quote:Original post by DevFredQuote: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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement