Struct types and dynamic arrays

This is a discussion on Struct types and dynamic arrays within the C Programming forums, part of the General Programming Boards category; Hi there, I am using a struct whose elements may be arrays. I would like though, for these arrays to ...

  1. #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.
    
    
    ;

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #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.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #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.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by simone.marras View Post
    One more question, do you do C++ as well?
    Me? No.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Thank you, Ill manage
    S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. dynamic ptr types
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 07-25-2004, 08:54 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

Tags for this Thread


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