Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Opengl Pointer To Array, Value Pointed..?
kvarnerexpress
post Feb 6 2006, 11:36 AM
Post #1


Super Member
*********

Group: Members
Posts: 407
Joined: 13-December 04
Member No.: 2,696



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:
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:

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:

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;
}


This post has been edited by BuffaloHELP: Feb 8 2006, 08:09 AM
Go to the top of the page
 
+Quote Post
switch
post Feb 8 2006, 07:36 AM
Post #2


Premium Member
********

Group: Members
Posts: 178
Joined: 13-October 04
From: NSW, Australia
Member No.: 1,713



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. biggrin.gif

ok good luck mate.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Green Laser Does Not Work(9)
  2. Opengl And Mfc Dialog Based Application(7)
  3. Array Size Limitations(2)
  4. Opengl Matrix Translation Issues(1)
  5. Remove Objects From Array With Unset(2)
  6. What Is Raid?(11)
  7. Struct And Pointer Arithmetic/notation.(1)
  8. Opengl(1)
  9. Hard Drive Array(5)
  10. Converting Characters In A Variable To Individual Values In An Array(2)
  11. Spliting An Array(4)
  12. Opengl(2)
  13. What Is Maid?(2)
  14. How Do I Create An Array Of Objects?(3)
  15. Opengl(3)
  1. Array Problem(4)
  2. More Dynamic ?id=browsing With Php (associative Array)(1)
  3. [php] Walking Through Multidimensional Arrays(2)
  4. Reading Files From Directory To Array, And Using $_get To Get Them(2)
  5. What Is A Fibonachi's Array?(5)
  6. Using Multiple Selection Array In Table To Order Data(1)
  7. Getting An Array Value Of A Dynamic Variable(9)
  8. Remove A Value From A Php Array Based On Its Value(5)
  9. [ruby]lite Multi-dimensional Array(0)
  10. Array Pointers Can Be Backwards(7)


 



- Lo-Fi Version Time is now: 6th October 2008 - 06:26 PM