C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-14-2009, 09:07 AM   #1
Registered User
 
Join Date: Aug 2006
Posts: 61
Struct types and dynamic arrays

Hi there,

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   Reply With Quote
Old 03-14-2009, 10:18 AM   #2
subminimalist
 
MK27's Avatar
 
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   Reply With Quote
Old 03-14-2009, 10:28 AM   #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   Reply With Quote
Old 03-14-2009, 10:39 AM   #4
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by simone.marras View Post
thank you very much for helping: I really appreciate it
Happy to be helpful. b/t/w, I forgot to use an f here:
Code:
	ray[i].z=(float)(x-i)*0.5f;
which doesn't matter much, but if you are using a lot of float vectors in eg, graphics stuff where optimization is a priority, without the "f" decimal numbers will get converted from a double (the default for decimals, I guess), which is one extra little thing for the processor to do. Using f and (float) ensures 0.5 is stored and accessed as a float, and the answer is stored and accessed as a float (without (float) it will be stored in the processor registers as a double and then converted to a float when placed into main memory).

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   Reply With Quote
Old 03-14-2009, 10:50 AM   #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   Reply With Quote
Old 03-14-2009, 10:55 AM   #6
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by simone.marras View Post
One more question, do you do C++ as well?
Me? No.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 03-14-2009, 10:56 AM   #7
Registered User
 
Join Date: Aug 2006
Posts: 61
Thank you, Ill manage
S.
simone.marras is offline   Reply With Quote
Reply

Tags
allocation, struct, structs

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22