|
-
Nov 11th, 2001, 11:33 AM
#1
Thread Starter
Lively Member
Character Arrays - Cast Help (Simple - I think)
Hey!
Ok heres the scoop, I want to manipulate an input string, but first requiring a copy from the input text into another character array.
My question is: Can you set the cast/char size of a character array at runtime: a.k.a.:
Code:
char string[len(inputstr)]
or something like that where I can alter the cast size depending on the length of the input string.
Thanks!
-
Nov 11th, 2001, 12:20 PM
#2
transcendental analytic
You need to use dynamically allocated char arrays, those you will allocate like follows:
char* array=new array[size];
to resize an array you can use my resize function, note the arrays have to be on the heap, otherways it won't work.
PHP Code:
template <class T>
T* resize(T*& x,int size){T* temp=(T*)memcpy(new T[size],x,_msize(x));delete[]x;return x=temp;}
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 11th, 2001, 06:57 PM
#3
Code:
// a simple C version
// you have a char arr[100] you need a duplicate
char *t;
char t = calloc(sizeof(char) * 100);
strcpy(t,arr);
-
Nov 11th, 2001, 09:54 PM
#4
Thread Starter
Lively Member
Awesome!
Awesome!
It works!
Thanks!
-
Nov 13th, 2001, 12:23 PM
#5
Monday Morning Lunatic
Originally posted by jim mcnamara
Code:
// a simple C version
// you have a char arr[100] you need a duplicate
char *t;
char t = calloc(sizeof(char) * 100);
strcpy(t,arr);
What's different with calloc and malloc? Can you still use realloc and free with calloc?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Nov 13th, 2001, 12:26 PM
#6
transcendental analytic
I think calloc fills the allocated space with nulls :S
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 13th, 2001, 12:30 PM
#7
Monday Morning Lunatic
So it's a bit pointless for VC++, since in debug mode they fill it with 0xCDCD or something like that (there's different values for different ways of allocating things - it's how the RTL checks for invalid pointer use).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|