Click here to Skip to main content
15,886,037 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,
will someone please explain to me how to dynamically allocate and array of structures
for example this structure
C++
typedef struct
{
   float angle;   //current servo angle
   unsigned int16 width_count;

   float min_angle;  //min achievable angle
   float max_angle;  //max achievable angle

   int16 min_uswidth;
   int16 max_uswidth;
}servo_motor;

servo_motor servo[4];


thanks in advance,
z3ngew
Posted

It's malloc, please see:
http://www.cplusplus.com/forum/articles/416/[^].

Please don't forget free.

Why do you thing your allocation should be dynamic? You don't have variable number of structures; in your code, it's the immediate constant 4, which is, by the way, always bad. You should have explicit const. In this case, you can simply declare the array, and it will be allocated on stack and not removed until you leave for a upper-level stack frame.

—SA
 
Share this answer
 
v2
Comments
z3ngew 12-Aug-13 16:32pm    
I am sorry, i asked the wrong question, it should be How to pass array of struct or or an item of array struct in a function.

Thanks in advance
Sergey Alexandrovich Kryukov 12-Aug-13 16:34pm    
You pass a pointer to the first element of array, what's the problem? This is the same as to pass servo, which is if the type servo_motor[].
After all, grab any C textbook and read about it all at once, you will avoid wasting so much time on those questions...
—SA
Maciej Los 12-Aug-13 17:00pm    
+5!
Sergey Alexandrovich Kryukov 12-Aug-13 17:02pm    
Thank you, Maciej.
—SA
pasztorpisti 12-Aug-13 23:33pm    
+5. Recently a collegue found a hardcoded constant in one of my routines I've written in haste. It was 42. Of course when he asked me about some more info I told him that "Its the meaning of life!". :-) Finally we figured out that it was the length of the textual representation of a 160bit sha1 hash + the "\r\n" newline but it took some time regardless of the short size of code that contained it.
Here: 7.2 Passing Arrays to Functions[^] you'll find an example
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Aug-13 17:04pm    
That certainly would answer the question, my 5, but... where did you find such an ugly manual?! Code samples are formatted as bitmap images, with all the re-sample distortions. People apparently are not capable of simple HTML formatting, how can they teach programming?
—SA
Maciej Los 12-Aug-13 17:09pm    
Thank you, Sergey ;)
You know, where i found it ;)
Sergey Alexandrovich Kryukov 12-Aug-13 17:22pm    
Well, come to think about, I do, still weird... :-)
—SA
Maciej Los 12-Aug-13 17:24pm    
;)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Aug-13 17:24pm    
But then, C++ or C? Passing be reference is the one of the distinct C++, not C features. But this is not a problem. In C, you can pass pointers by value. I already answered your question...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900