Using *char

This is a discussion on Using *char within the C++ Programming forums, part of the General Programming Boards category; Hi, I was working with a program where the size of an character array depends on the user input.. May ...

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Using *char

    Hi,
    I was working with a program where the size of an character array depends on the user input.. May vary from size 200 to 5600 characters.. Instead of allocating this by char space[5600] i wanted to know what effect char *space; would have and how it works.. I found that using pointers was better to allocate memory.. But what happens when i do this.. I even tried new to allocate memory but that did not suit my needs...

    Thanx

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    the string class may be a better solution for you. i think for the char pointer you can use malloc or new to create new space.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    char *array=new char[size];

    By doing that, you can make an array of characters of the size the user inputs.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,669
    Well, another way is to declare a very large variable that will be reused throughout the term of the program. Then, copy the result into a perfect size container.

    char *NewString( char *string )
    {
    int length = strlen( string ) + 1;

    char *copy = new char[ length ];

    memset( copy, 0, length );

    strcpy( copy, string );

    return copy;
    }



    int main()
    {
    char Big[1028] = "Hello";

    char *little = NewString( Big ); //...6 bytes allocates...

    //...reuse "Big"...

    return 0;
    }




    The only danger in this scheme is that you are allocating JUST what you need at that time. But what if you want to "strcat()" onto it? Well, the obvious solution is to (A) use a class wrapper, like the STL string and overoading the +(strcat() ) operator to realoc more memory, or (B) if you want to stick with the plain ole char * (my preference...), then just redefine your functions like:

    char *Strcat( char *s, char *p)
    {
    //...realloc, then call strcat...
    }



    ITSA
    Socket Library!

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Well by using char *name; i was able to write n number of elements into it and also read it... Well i never ever declared the size of the array anywhere.. Can any one expalin this to me... I was also able to return this from a function.. The return type i used was
    cahr * function()

    So how is this advantageous or disadvantegous..

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if you did something like this:

    int size = 30;
    char * name = new char[size];

    then whether you knew it or not, you declared an array of char with 30 elements on the heap/free store. size can be changed in your program, but once name has been declared as above it will always have whatever value size was at the time of the above declaration. Once you have assigned a value to name you can use it like any other array. One advantage of using this syntax is that you can return name from a function, as the scope of name is not local to the function in which it is declared, which can be a problem for other local variables. The memory for name will only go out of scope in your program when you call use this line:

    delete [] name;

    This can be a blessing or a curse depending on how you use the knowledge.

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Ok thanx...

    But i was able to use the character array withut defining the size...

    i only declared

    char *name;

    and started using name to store strings etc etc.. So i want to know what happend now when the size was not declared anywhere....

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    I think you may be confusing the difference between the fact that an array variable is a "pointer", and that you've declared a char pointer.

    By way of explanation:

    char arr[20];

    'arr', in this context, is a char pointer to the first position of an array, i.e. the '0' position of an array that is limited to 19 chars + the '\0' character, if used properly (that is, not overwritten).

    char *arr;

    'arr' is now an open-ended pointer to a memory location of type 'char'. Size is not an issue here. (Of course, in reality...)

    char *arr is not an array.

    Don't confuse the two.
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21