float ar[310]; for(int x = 0; x <= 308; x += 4){ angle = 2.0*(float)PI*x/100; radius = 0.5f * (2+sin(8 * angle)); ar[x] = radius * cos(3*angle), radius * sin(8*angle), radius * sin(3*angle); } glBegin(GL_LINE_LOOP); for(x = 0; x <= 308; x += 4) glVertex3fv(ar[x]); glEnd(); I'm just trying to get glVertex to read from the array and draw a circle. Again I'm sorry for the newbieness of my question, but its frustrating and Im just hoping someone can help. I get an error message saying it cant convert from "float" to "const float *". At the "ar[x]=" line I want to put the x,y,z values into the array by each other. I think it is doing it ok with the commas... Any help is appreciated.
🔒 Locked
Accessing an array
Advertisement
Original Post
Hello, I'm sorry for the newbieness of this question... I'm having trouble using an array. I want to store values in it then access the values later this is the code I have:
use the following:
for (x = 0; x < 310; x+= 3)
{
angle = 2.0*(float)PI*x/100;
radius = 0.5f * (2+sin(8 * angle));
ar[x] = radius * cos(3*angle);
ar[x+1] = radius * sin(8*angle);
ar[x+2] = radius * sin(3*angle);
}
Each element must be put into the location of the array one at a time
for (x = 0; x < 310; x+= 3)
{
angle = 2.0*(float)PI*x/100;
radius = 0.5f * (2+sin(8 * angle));
ar[x] = radius * cos(3*angle);
ar[x+1] = radius * sin(8*angle);
ar[x+2] = radius * sin(3*angle);
}
Each element must be put into the location of the array one at a time
I forgot to say you probibly also have to allocate memory from the heap for such a large array e.g.
int* pAr = new int[310];
then use the array
for (x = 0; x < 310; x+= 3)
{
angle = 2.0*(float)PI*x/100;
radius = 0.5f * (2+sin(8 * angle));
*(pAr+x) = radius * cos(3*angle);
*(pAr+x+1) = radius * sin(8*angle);
*(pAr+x+2) = radius * sin(3*angle);
}
then before the program terminates use
delete pAr
int* pAr = new int[310];
then use the array
for (x = 0; x < 310; x+= 3)
{
angle = 2.0*(float)PI*x/100;
radius = 0.5f * (2+sin(8 * angle));
*(pAr+x) = radius * cos(3*angle);
*(pAr+x+1) = radius * sin(8*angle);
*(pAr+x+2) = radius * sin(3*angle);
}
then before the program terminates use
delete pAr
NOTE: x is incremented by 3 in the for loop not by 1 ->
not size*3!!!! This will allocate way more memory than needed
not size*3!!!! This will allocate way more memory than needed
Quote:
Original post by CryonicGhost
I forgot to say you probibly also have to allocate memory from the heap for such a large array e.g.
int* pAr = new int[310];
then use the array
for (x = 0; x < 310; x+= 3)
{
angle = 2.0*(float)PI*x/100;
radius = 0.5f * (2+sin(8 * angle));
*(pAr+x) = radius * cos(3*angle);
*(pAr+x+1) = radius * sin(8*angle);
*(pAr+x+2) = radius * sin(3*angle);
}
then before the program terminates use
delete pAr
even when you allocat from the heap you don't need to access your array with pointer arithmatic. you can use normal code:
int *ar = new int[310];for ( x=0; x<310; x +=3 ){ //do stuff; ar[x] = something; ar[x+1] = somethingElse; ar[x+2] = somethingElseEntirely;}-me
Sweet dude - didnt know that about pointers so we even :)
Try something like this. It should draw a circle with radius 1 on the XY plane. I'm not quite sure how your values work in your example, so I'm going to start from scratch.
Anyway, here's my version:
Your code has two errors. The first one is this line:
Here, you assume that commas allow you to assign values to consecutive indices in the array. When used like this, commas are more like the equivalents of semicolons. So, ar[x] is getting the value radius*cos(3*angle), and the other two comma-seperated statements are just creating a float value which is then ignored.
Next is this line:
glVertex3fv expects a pointer to a float, and you are passing it a float value. ar[x] returns the value stored at index x, not a pointer to it. To do that you'd have to pass either &ar[x] or ar+x.
I suggest you find a copy of The C Programming Language by Kernighan and Ritchie, and read it cover to cover. Normally I would suggest some online tutorial, but I have yet to see one which sufficiently covers some of the little details, especially the bit about how commas work.
Hope that helps.
[edit] 6 other replies in the time it took me to type that... a new record! [/edit]
Anyway, here's my version:
// some variablesfloat ar[90][3]; // 90 points of x,y,zfloat angle, radius;int i;// calculate the arrayfor(int i=0; i<90; i++){ angle = i*PI/45; radius = 1; ar[0] = radius * sin(angle); // x ar[1] = radius * cos(angle); // y ar[2] = 0; // z}// draw the circle from values in the arrayglBegin(GL_LINE_LOOP); for(i=0; i<90; i++) glVertex3fv(ar);glEnd();Your code has two errors. The first one is this line:
ar[x] = radius * cos(3*angle), radius * sin(8*angle), radius * sin(3*angle);Here, you assume that commas allow you to assign values to consecutive indices in the array. When used like this, commas are more like the equivalents of semicolons. So, ar[x] is getting the value radius*cos(3*angle), and the other two comma-seperated statements are just creating a float value which is then ignored.
Next is this line:
glVertex3fv(ar[x]);glVertex3fv expects a pointer to a float, and you are passing it a float value. ar[x] returns the value stored at index x, not a pointer to it. To do that you'd have to pass either &ar[x] or ar+x.
I suggest you find a copy of The C Programming Language by Kernighan and Ritchie, and read it cover to cover. Normally I would suggest some online tutorial, but I have yet to see one which sufficiently covers some of the little details, especially the bit about how commas work.
Hope that helps.
[edit] 6 other replies in the time it took me to type that... a new record! [/edit]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement