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 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...
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.
char *array=new char[size];
By doing that, you can make an array of characters of the size the user inputs.
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 );
char Big = "Hello";
char *little = NewString( Big ); //...6 bytes allocates...
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...
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..
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.
But i was able to use the character array withut defining the size...
i only declared
and started using name to store strings etc etc.. So i want to know what happend now when the size was not declared anywhere....
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:
'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).
'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. :)