![]() |
| | #1 |
| Registered User Join Date: Aug 2006
Posts: 61
| Struct types and dynamic arrays I am using a struct whose elements may be arrays. I would like though, for these arrays to be dynamically allocatable when I declare them in the main code; how is it supposed to be done? I wrap my code here; it's very simple, however, please let me know if there are more information that you need Thank you in advance, Best S.M. Code: typedef struct Mesh{
float x; //x-coord of Vertex
float y; //y-coord of Vertex
float z; //z-coord of Vertex
} typeMesh
main
{
typeMesh COORDS [SIZE TO BE ALLOCATED] //This is where I'd like to allocate this dynamically.
/* What I would like to obtain is the COORDS array to be of SIZE_n that will be obtained as the output of another function that will be called only later in the code.
;
|
| simone.marras is offline | |
| | #2 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Code: #include <stdio.h>
#include <stdlib.h>
typedef struct {
float x; //x-coord of Vertex
float y; //y-coord of Vertex
float z; //z-coord of Vertex
} Mesh;
int later_function () {
int size;
printf("How big? ");
scanf("%d",&size);
return size;
}
int main() {
int x,i;
Mesh *ray;
x=later_function();
ray=malloc(sizeof(Mesh)*x);
for (i=0;i<x;i++) {
ray[i].x=(float)i;
ray[i].y=(float)x-i;
ray[i].z=(float)(x-i)*0.5;
printf("Vertex %f %f %f\n",ray[i].x,ray[i].y,ray[i].z);
}
free(ray);
return 0;
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #3 |
| Registered User Join Date: Aug 2006
Posts: 61
| Hello MK, thank you very much for helping: I really appreciate it All the best S.M. |
| simone.marras is offline | |
| | #4 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Happy to be helpful. b/t/w, I forgot to use an f here: Code: ray[i].z=(float)(x-i)*0.5f; Or So I Have Been Led To Believe.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #5 |
| Registered User Join Date: Aug 2006
Posts: 61
| Thank you very much for the hint; I do programming for math applications and Im barely learning on this; it surely helps learning by these suggestions One more question, do you do C++ as well? I am trying to do the same thing in my C++ version of the code, but not sure about how to deal with it if there is a CLASS in the between Best S. Last edited by simone.marras; 03-14-2009 at 10:52 AM. |
| simone.marras is offline | |
| | #6 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
|
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #7 |
| Registered User Join Date: Aug 2006
Posts: 61
| Thank you, Ill manage S. |
| simone.marras is offline | |
![]() |
| Tags |
| allocation, struct, structs |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dynamic array of structures containing yet another dynamic array of structures | innqubus | C Programming | 2 | 07-11-2008 07:39 AM |
| Binary Search Trees Part III | Prelude | A Brief History of Cprogramming.com | 16 | 10-02-2004 03:00 PM |
| dynamic ptr types | subdene | C++ Programming | 4 | 07-25-2004 08:54 AM |
| Request for comments | Prelude | A Brief History of Cprogramming.com | 15 | 01-02-2004 10:33 AM |
| operator overloading and dynamic memory program | jlmac2001 | C++ Programming | 3 | 04-06-2003 11:51 PM |