diffrence between a char pointer and char pointer inside a class(private)

rafeek610's Avatar, Join Date: Sep 2010
Light Poster
what is the diffrence between a char pointer and char pointer inside a class(private)
jimblumberg's Avatar
Ambitious contributor
The char pointer in the class can only be accessed by class members.
rafeek610's Avatar, Join Date: Sep 2010
Light Poster
i asked about not that diffrence...............
in normal char pointer we can have to allocate contineous memory for storing string(implicitly)..........
is it possible in the case of a class member..........................
eg:
char *str;
scanf("%s",str);
will store the strring inside str(without allocating memory explicitly(means not using malloc))............
but is it possibe for a class private member..............
plz mension an eg!!!!!!!!!!!!
jimblumberg's Avatar
Ambitious contributor
You must explicitly allocate memory for a string in any scope in c/c++. You must first allocate memory for a pointer before using. Your example will produce undefined results. Some times it may seem to work, but it is writing to memory it does not own.

Jim
shabbir like this
rafeek610's Avatar, Join Date: Sep 2010
Light Poster
but in the case char pointer its always true in normal case...........
what happent in the case of class variable
jimblumberg's Avatar
Ambitious contributor
You must explicitly allocate memory for a cstring in any scope in c/c++. I does not matter if it is in a class/structure, global, or local pointer.
shabbir like this