Thursday, July 30, 2009

What does char* = in C++?

what is the char*, and why is the "*" not considered a multiplication operator? I really don't care about the second question I really need the answer to the first.





I am under the assumption char* = char array or char[]





Thank you in advance.

What does char* = in C++?
actually this is a character pointer


character pointer is a variable thats stores the address of the variable of character type


"*" this hwere u have used is not multiplication operator but known as indirectional operator
Reply:I just learned about it yesterday.


It is a pointer.


When u declare an interger/char/etc follow by * it means its going to be a pointer.
Reply:Its a dereferencing pointer, if you haven't seen on before wait. Im sure you come across them. So depending on context the compiler will use it as a multiplication or a deref or char pointer initialization.


Instead of holding the value it holds the adress showing where something is.
Reply:The asterisk is both the dereference and the multiplication operator in C++. When you put the asterisk between the variable type and the variable name in a variable declaration, you're creating a pointer. Your compiler figures out which you mean by the context it's used in.





When you create an array in C, you allocate space for the data and you create a pointer to the first element in the array. You can treat your array variable as a pointer.





// Regular char


char aChar = 'a';





// Pointer to char


char* aPointer = new char;


*aPointer = 'a';





// Pointer to an array of 10 chars


char* anArray = new char[ 10 ];


*anArray = 'a';


anArray[ 1 ] = 'b';





// Also a pointer to an array of 10


char[] anArray = new char[ 10 ];


*anArray = 'a';


anArray[ 1 ] = 'b';


No comments:

Post a Comment