What is the difference between char and char * in C ++?

Studying the code in a Schaum C ++ book, I saw a lot of code using char *, int *, etc. While doing the exercises, I also saw that the solutions have char *, and in my code I used char (without a star).

I want to know what is the difference between char and char - integer pointer and pointer integer? Where should I use them? What is their meaning?

+3
source share
8 answers

Variables with *are pointers.

A "normal" variable, such as char or int, contains the value of this data type itself - the variable can contain a character or an integer.

- ; , . , char * , - .

"" &:

char c = 'X';
char * ptr = &c;  // ptr contains the address of variable c in memory

, * :

char k = *ptr;  // get the value of the char that ptr is pointing to into k

. () .

+6

7 , - , , , .

+5

. , wikipedia.

A char * - , '\ 0'. char . Int * .

:

int* x = new int();

integer , . , . , , :

int y = *x;

; . y , .

+2

char int long - , , char - 1 .

- , .

, char, , .

, , .

+1

char int - 'a' 42. char * int * - , . , * , , .

0

OK

. char char* , , .

char c - . , , . , c " " .

char *c - . , , , , " ". , c " ", , , . , c , , delete, .

*, deferencing operator.

wiki Dereference_operator

0

char - , char. .

char ch = 'a';
cout << ch; // prints a

char * - char, . ,

char* s = "hello";
cout << s; // prints hello
s++;
cout << s; // prints ello
0

* . , , * .

, char , 'a' 'Z'. char * , char char.

int , 1 12345. int * , int int.

.

:

// pointer declaration
char* pchar = new char[5];

This allocates a 5 * sizeof (char) memory zone and allows you to access 5 characters.

Remember to free up memory when your pointer is no longer needed.

// pointer destruction
delete pchar;
-1
source

Source: https://habr.com/ru/post/1718609/


All Articles