| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
how to modify the size of array in struct??
hi,
I get a question. in my program, I have: struct gogogo { char here[100]; }; but, the the size of "here" will be changed each time when I use it. How can I difine this kind of array?? ( the struct is required in this program ) |
|
#2
|
||||
|
||||
|
Use dynamic memory. Change "char here[100]" to be "char *here" then just allocate memory and set "here" equal to it.... ie:
C: here = malloc(size) C++: here = new char[size] And don't forget to deallocate the memory once you're done ![]()
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#3
|
|||
|
|||
|
hi
this methd will pop up a error message window. my code struct dat { unsigned char* here[100]; } dat get_dat; get_dat.here = new unsigned char[ 800 ]; ...... delete [] get_dat.here; ======================================= sorry the problem is raise by other functions |
|
#4
|
||||
|
||||
|
struct dat
{ unsigned char* here; } don't make "here" an array and a pointer, otherwise it will be an array of pointers, and you only need one pointer ![]() |
|
#5
|
|||
|
|||
|
simply use define statement.
which means after declaring header file #define SIZE 100 and then while using struct write here[SIZE] and when u run ur program keep changing the SIZE . or simply use the realloc |
|
#6
|
|||
|
|||
|
if you're using c++ you could use a template:
Code:
template<int size> struct gogogo
{
char here[size];
};
//...
gogogo<100> hundred;
gogogo<23> twenty_three;
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > how to modify the size of array in struct?? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|