Aug 8, 2008

Opengl Pointer To Array - Value Pointed..?

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

free web hosting

Opengl Pointer To Array - Value Pointed..?

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

 

 

 


Reply

switch
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.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : opengl pointer array pointed

  1. Opengl And Mfc Dialog Based Application - some comments about using it together (7)
    AS you probably know you can create a simple OpenGL applications using WinAPI functionality only
    (you can create it using Microsoft Visual Studio), but if you are making somethink with real windows
    interface and want to create application with multiple windows/dialogs/buttons with Windows Styles
    etc... you can use MFC (Microsoft Foundation Classes) to do it ... But i did a 2 hours web search to
    find example of using OpenGL windows inside MFC created Dialog window. I found it and want to share
    my results now. My application is a simple OpenGL based tool, which shows Ope...
  2. Array Pointers Can Be Backwards - (Kind of) (4)
  3. Array Problem - (4)
    I'm having trouble with an assignment for my data structures class.


    I have to simulate a
    card player getting a a pile of 52 cards (a full deck).
    ...
  4. Opengl - (2)
    what files do I need to activate OpenGL in my C++? And please give me a simple program to use it.
    thank you....
  5. Opengl - how to... (1)
    How can enable OpenGL in my C++? I mean the Borland C++, not Visual C++. What do I need other than
    glut.h that I've found, and how to use it? Thanks in advance....
  6. Struct And Pointer Arithmetic/notation. - (1)
    My book that I am reading gave us an assignment that I dont fully understand. Quote: QUOTE
    Create a program that includes the following: A structure called ‘planet’ that includes a name,
    a distance from the sun and the diameter of the planet. This I understand. Code: CODE struct
    Planet { string planName; double planDistSun; double planDiam; }; Quote: QUOTE Create
    an array of 3 planet structures. Does my book mean something like this? What type of array
    should I use? Could I use a char array? Code: CODE Planet Mars; Planet Earth; Pla...
  7. Array Size Limitations - (2)
    I'm writing a win32 console app and am about to implement an array of size 10,000,000 . Can
    someone tell me what this would do ? Would my memory be able to handle it or would it cause other
    variables to lose their value ? I may just end up writing the array to a text file and then just
    searching it when I need it but I would rather not if I didn't need to. Can someone tell me what
    the limit is ? kvarnerexpress...



Looking for opengl, pointer, array, pointed

Searching Video's for opengl, pointer, array, pointed
advertisement



Opengl Pointer To Array - Value Pointed..?



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE