Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Opengl Pointer To Array Value Pointed..?

Recommended Posts

I'm playing around wtih openGL and trying to get the hang of c/c++(very frustrating at times). I pass a pointer to an array from an object to my main via a get method. If I print out(cout) the pointer(p) and the value pointed to (*p) from main, *p prints the decimal equivilent of the memory address p, not the value that p should be pointing to. Within the object, everything is peachy, its just when I pass the pointer to to main that this happens. The arrays plist[] and vlist[] in meshloader are the ones im having trouble with.

Any ideas? Thanks in advance. Feel free to tell me how bad my code is in general!


Code:

class MeshLoader{	  public:			MeshLoader(){};			~MeshLoader();			float * loadMesh();			int getvsize();			int getpsize();			float * getvlist();			int * getplist();	  private:			int vsize;			int psize;			float vlist[];			int plist[];};



Code:

#include <iostream>#include <fstream>#include <math.h>#include <stdlib.h>using namespace std;#include "meshloader.h"MeshLoader::~MeshLoader(){}float* MeshLoader::loadMesh(){	int i = 0;	int j = 0;	vsize = 0;	psize = 0;	vlist[100];	plist[100];	char line[255];	char tag[255];	ifstream fin("xml.x");	if(fin.is_open())	{		while(fin.getline(line, 255) != NULL)		{			strcpy(tag, line);			if(strcmp("<model>", strtok(tag, " ")) == 0)			{			}			strcpy(tag, line);			if(strcmp("<vlist>", strtok(tag, " ")) == 0)			{				vsize = atoi(strtok(NULL, " "));				for(i=0;i<vsize;i++)				{					fin.getline(line, 255);					vlist[3*i+0] = (float) atof(strtok(line, " "));					vlist[3*i+1] = (float) atof(strtok(NULL, " "));					vlist[3*i+2] = (float) atof(strtok(NULL, " "));				}			}			strcpy(tag, line);			if(strcmp("<plist>", strtok(tag, " ")) == 0)			{				psize = (int) atoi(strtok(NULL, " "));				for(i=0;i<psize;i++)				{					fin.getline(line, 255);					plist[3*i+0] = (int) atoi(strtok(line, " "));					plist[3*i+1] = (int) atoi(strtok(NULL, " "));					plist[3*i+2] = (int) atoi(strtok(NULL, " "));				}			}		}	}	cout << &plist[0] << ":";	cout << *&plist[0] << ":";	return 0;}int MeshLoader::getvsize(){	return vsize;}int MeshLoader::getpsize(){	return psize;}float * MeshLoader::getvlist(){	return &vlist[0];}int * MeshLoader::getplist(){	cout << &plist[0] << ":";	cout << *&plist[0] << ":";	return &plist[0];}


the relevant part of main:

Code:

#include <cstdlib>#include <iostream>#include <string>#include <sstream>#include <stdio.h>#include <math.h>#include <time.h>using namespace std;#include <GL/glut.h>#include "meshloader.h"MeshLoader myScene;int * p;float * v;int main(int argc, char **argv){	p = myScene.getplist();	v = myScene.getvlist();	cout << p << ":";	cout << * p << ":";				glutInit(&argc, argv);	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);	glutInitWindowPosition(300,100);	glutInitWindowSize(640,320);	glutCreateWindow("glutton");	glutDisplayFunc(render);	glutIdleFunc(render);	glutReshapeFunc(reshape);	glutMouseFunc(processMouse);	glutMotionFunc(processMouseActiveMotion);	glutPassiveMotionFunc(Motion);	glutEntryFunc(processMouseEntry);	glutKeyboardFunc(processNormalKeys);	glutSpecialFunc(processSpecialKeys);	glEnable(GL_DEPTH_TEST);	glutMainLoop();	return 0;}

Edited by BuffaloHELP (see edit history)

Share this post


Link to post
Share on other sites

alot of the time when i start having trouble with C++ i just seek out an alternate route of doing the same thing. When you are using pointers to reference arrays (we'll call ours myPointer), normally *myPointer just returns the first value in the array. On this note, you could try outputting myPointer[0], as that is equivalent to saying *(myPointer + 0).maybe change the declaration from vlist[] and plist[] to *vlist and *plist respectively and then changing their initialization to vlist = new float[100] and plist = new int[100]. That could help.apart from that, i must congratulate you on how neat your code looks. Good work. Nice and Easy to read. :rolleyes:ok good luck mate.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.