for example i enter an string lenght is n.how can i take space from memory for that string
for example my array is word[32] but my name has 4 characters
the rest of this array dont be used. how can i prevent this?
for example i enter an string lenght is n.how can i take space from memory for that string
for example my array is word[32] but my name has 4 characters
the rest of this array dont be used. how can i prevent this?
Last edited by condorx; 05-24-2002 at 01:28 PM.
By allocating memory dynamically.
If I remember well, C99 supports dynamical arrays in this way:Code:char *word; int length; length = .... <determine length here or get it from somewhere>..; /* Create a dynamic array */ word = malloc (sizeof (char) * length); ..... <do some stuff with word> ... /* Release the allocated memory */ free (word);
Code:void function (int length) { char word [length]; ... <some stuff> ... }
By the way, what is C99?
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
C99 is the code of the latest C standard, ISO/IEC 9899:1999.
Does VC++.NET support this standard?
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
They claim they do but no compiler conforms 100%
Close, but no cigar. This is a "Bad Thing(TM)". You fail to allocate enough space for the null terminator (unless you've included that in your "lenght" count).Originally posted by Shiro
By allocating memory dynamically.
Code:char *word; int length; length = .... <determine length here or get it from somewhere>..; /* Create a dynamic array */ word = malloc (sizeof (char) * length); ..... <do some stuff with word> ... /* Release the allocated memory */ free (word);
word = malloc( sizeof(char) * (length + 1) );
word[length] = '\0';
Now it's safe to use. (Actually you may want to memset it, but that's optional.)
Quzah.
Hope is the first step on the road to disappointment.