Char and char * (pointer)

I would like to understand how pointers work, so I created this small program. First of all, I create a p-pointer that points to a char.

The first question is at this point. If I create a pointer, its value will be memoryaddress (if I point it to an object without a pointer), but this time it is β€œhaha” in my example. Why does this work in char *? And how can I add the value cin β†’ p to it?

My second question is that I created a q char, which has the value * p pointer at the point i that I created. BUT its meaning and address "h" too, but why? This must be the memory address of this char! This is pointless: D (mingw - gcc)

int main() { char *p; cin >> p; //forexample: haha char q = *p; cout << "&q = " << &q << endl; //&q = h cout << "q = " << q << endl; //q = h return 0; } 

MORE: If I first allocate memory using char a [100]; char * p = a; then & q = h "Ε’ΔΉ, so that" h "and some mess. but it should be a memoryaddress! and my question is, why is it not addressed then?

+4
source share
3 answers

Think of char* p; at the address in memory. You did not initialize this pointer so that it does not point to anything, you cannot use it.

Always be safe:
either initialize a pointer to zero:

 char *p = 0; // nullptr in C++11 

or initialize some automatic

 void foo() { char a[100]; char *p = a; } 

or global memory:

 char a[100]; void foo() { char *p = a; } 

or get dynamic memory:

 char* p = new char [100]; 

Then you can use p (if not NULL) to read data and read from p ...


For misunderstanding operator >> (std::istream&, char* p) . This operator expects p point to some memory (automatic, global, dynamic - it doesn't matter) - it does not allocate memory by itself. It just reads the characters from the input stream to the space and copies it to the memory indicated by p - but p should already point to some memory.


For address <<29>. Of course, you can get the address q : &q , and it is of type char* p . But &q differs in that p , and this q=*p just copies the first character pointed to by p to q , it cannot change the address q - its address is unchanged. For cout << &q - operator << (ostream&, char* p) expects p point to a NULL terminated string - and &q points to memory containing "H" , but that no one knows after this character - so that you get trash on the screen. Use cout << q to print a single character.

+5
source

The first thing you need to learn and always remember about pointers is to provide memory allocation for them, otherwise your program will not work properly.

Your code really needs to be modified as follows in order to allocate memory so that cin can write user input to the allocated memory:

 int main() { char *p = new char[1024]; // allocate memory so cin cin >> p; //forexample: haha char q = *p; cout << "&q = " << &q << endl; cout << "q = " << q << endl; return 0; } 

Now a symbol pointer is a variable indicating a position in memory containing a set of characters (not necessarily one character, possibly more than one, perhaps none, as in the case of the special null value), and the character variable actually contains one character (and not a character set).

The main operators when working with pointers are & (address) and * (value at). It returns the address of the variable, so if we have [char q;], then [& q] will be a character pointer. On the other hand, * retrieves the value in the specified pointer, so if we have [char * p;], then [* p] will be the character in memory pointed to by p.

Now back to your example, comments to illustrate

 int main() { // allocate a place of 1024 character in memory // and let p points to that place char *p = new char[1024]; // call cin to read input from the user and save // it in memory at the location pointed to by p // NOTE: cin would put an extra NULL character // at the end to terminate the string cin >> p; //forexample: haha // Now p would be pointing to a piece of memory like this // [h] [a] [h] [a] [\0] // use the value at operator to de-reference the pointer // p, the result would be a single character which // will be the first character in memory p is pointing at char q = *p; // printing the value of (the address of q) // Note you have a problem here as cout will // be handling the memory starting at the address // of q as a string while it is not, so you // will get a string starting with "h" and followed // by whatever is there in memory by the time of execution cout << "&q = " << &q << endl; // printing the value of the character q cout << "q = " << q << endl; return 0; } 

I hope this helps

+2
source

You should have something like:

 #include <iostream> using namespace std; 

at the top of your program. Or you can omit using and refer to std::cin and std::cout .

 int main() { char *p; 

p is a pointer, but you did not initialize it, so it could point anywhere or anywhere. You need to initialize it, for example:

 p = new char[100]; 

...

  cin >> p; //forexample: haha 

This is normal if you initialized p to point somewhere, except that you can overflow the buffer that it points to if you enter too much data. This is normal for a simple test program like this, but in practice you will want to take measures to avoid this.

  char q = *p; cout << "&q = " << &q << endl; //&q = h 

&q is of type char* , a pointer to char . Sending the char* value to cout does not print the pointer value (memory address); it prints the line it points to. (Although I get some odd results when I run it myself, maybe something is missing.) If you want to see the value of the pointer, add it to void* :

 count << "&q = " << (void*)&q << endl; 

(Or you can use one of the C ++ specific casts, I'm not sure which is the best.)

  cout << "q = " << q << endl; //q = h 

Here q is just a char , so it prints its value as a character: h .

  return 0; } 
0
source

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


All Articles