| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello. I have a problem with pointers to char.
First, I know that the following is valid:char *a = "I am having a cup of coffee."; However, the following is invalid, right? I must use strcpy, right?In addition, can I leave the [] to be blank (i.e. not including any integer)? char *a = new char[100]; a = "I am having a cup of coffee."; Is the following acceptable? If no, why not?char *a = "I am having a cup of coffee."; cin >> a; How about this?char *a = new char[100]; cin >> a; |
|
#2
|
|||
|
|||
|
However, the following is invalid, right? I must use strcpy, right?
In addition, can I leave the [] to be blank (i.e. not including any integer)? char *a = new char[100]; a = "I am having a cup of coffee."; just put it in an array from the get go char a[100] = "I am having a cup of coffee."; |
|
#3
|
|||
|
|||
|
when you use pointers, you have to do something special to write to the actual value of the variable being pointed to. for instance, this:
int b; int *a = &b; a = 0; does not set b to 0, it sets the pointer a to point to memory location 0. however, this: int b; int *a = &b; *a = 0; should set b to 0 someone who understands pointers better than me, feel free to correct me! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Pointers to char |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|