Results 1 to 7 of 7

Thread: Character Arrays - Cast Help (Simple - I think)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Posts
    102

    Question 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!

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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>
    Tresize(T*& x,int size){Ttemp=(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.

  3. #3
    jim mcnamara
    Guest
    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);

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2001
    Posts
    102

    Cool Awesome!

    Awesome!

    It works!

    Thanks!

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width