Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > (Potentially) simple question about C syntax

(Potentially) simple question about C syntax
Thread Tools
Professional Poster
Join Date: Oct 1999
Location: Always within bluetooth range
Status: Offline
Reply With Quote
Mar 31, 2003, 10:21 AM
 
<disclaimer>
OK, let me say first that I know next to nothing about the C language but I'm writing a simple little OpenGL program and C is the language I have to use (and I mean pure C and glut.framework ... no cooca, appkit, Obj -C etc.. This is actually the 4th of a little series I have to write and they've all been written on my mac and run seamlessly on windows .. which is what I need) .
</disclaimer>

So here's the issue: I'm setting up a bunch of arrays for points of various polygons in OpenGL. Code looks like
GLfloat column[][2] = { {3.25, 1}, {3.75,1}, {3.75,13}, {3.25,13}};


easy enough so far. But, I need to set one such GLfloat array to the return value of a function ... something like
GLfloat piece[][2] = setpiece();


.. but I have no idea what the syntax should look like. I've tried a couple of things and get "invalid initializer" (amongst other things when I screw around with it). So how do I initialize and set piece[][2] to the return value of a function ? It is unknown when I call the function how many elements will be in the 2D array so the function needs to size and return a 2D array of GLfloats as when I was setting GLfloat column[][2] = { {3.25, 1}, {3.75,1}, {3.75,13}, {3.25,13}};

Any help or redirects to a similar code sample would be helpful ... I know this must be a piece of cake for any reasonably experienced C coder and I can't seem to find a similar code sample.

Muchos Gracias
     
Junior Member
Join Date: Oct 2001
Status: Offline
Reply With Quote
Mar 31, 2003, 10:54 AM
 
Instead of using a 2D array I would create a struct for the point and create an array of your structure.
Code:
typedef struct point{ double x; double y; }; struct point* pointArray = (point*)malloc(sizeof(point) * numberofelements); pointArray[index] = setPoint(); struct point* setPoint() { struct point* pointy; return pointy; }

Your setPoint function will then return a pointer to your struct type.
I hope this helps.

type R
I be that insane n***a from the psycho ward.
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Mar 31, 2003, 01:38 PM
 
Code:
typedef struct { GLFloat x, GLFloat y, } MyGLPoint; MyGLPoint* pieces = setpiece(); // do stuff free(pieces);
and then, in setpiece
Code:
MyGLPoint* setpiece () { // find out how many MyGLPoints you're going to require MyGLPoint *returnedPieces = malloc ( requiredPointCount * sizeof(MyGLPoint) ); returnedPieces[0].x = 1; returnedPieces[0].y = 2.5; // etc.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Krusty  (op)
Professional Poster
Join Date: Oct 1999
Location: Always within bluetooth range
Status: Offline
Reply With Quote
Mar 31, 2003, 04:22 PM
 
Hey, thanks you guys (gals?). I'll work with bot h suggestions later tonight and see how it goes.

I *LOVE* these boards
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:18 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2