1. ## Pointers... Geez

I'm a newbie so don't yell. I'm advancing up to Pointers now. And it is so confusing. I know pointers have something to do with memory addressing, but thats about it. Can anyone help me out? Just to make this easier to understand.

2. A pointer is a type of variable that contains a memory adress. That memory adress refers to a block of memory. Consider these examples:
Code:
```char c;
char* pc = &c;

int i;
int* pi=&i;

void* pv= malloc(100);```
When I declare 'c', c will be a variable of type char, or in other words, will be 1 byte big (traditionaly, but can be different). When I declare pc, that variable will be a pointer to that character. The block of memory that pc points to will, then, be 1 byte big.
When I declare 'i', i will be 4 bytes big (traditionaly, again...), and pi will point to a 4 bytes memory block.
Now that you're thinking in memory blocks, what about assigning a block of 100 bytes to a pointer? That's done with the call to malloc, which return a pointer, and it's stored in pv.
Now why void*? Or why int*? Or why char*?
The difference bettween these types is the way there treated, because there all (traditionally) 32 bits long, or in other words, contain a memory adress of 32 bits.
In that adress one will find the block of memory with something stored in it.
The types are used to when de-referencing the pointer, getting something from the pointed memory block.
If you have a memory block at 0x001000, acessing to that memory with ptr[0], ptr[1], ptr[2], being ptr a pointer to char ( char* ), would match, respectively, reading memory from position 0x001000, 0x001001, 0x001002, because chars are 1 byte big. But if the pointer is to ints acessing ptr[0], ptr[1], ptr[2] would result int acessing 0x001000, 0x001004, 0x001008, because ints are 4 bytes big!
And doing this
Code:
```int *ptr=.../*something*/;
int i= *ptr```
will read 4 bytes from that memory position and assign then to the variable. If you're using chars it would be read only 1 byte.

IMPORTANT NOTE: I used 32 bits for size as example, but these may change due to computer achitecture, or compiler implementation. Variables size, or memory adresses size aren't defined by any programming standard. Only by hardware's. The same for int and char sizes! These also vary.

Now look at a little example
Code:
```#include<iostream>
int main(){
int my_int = 0x002b2b43;
char *my_ptr_char1 = (char*)&my_int;
char *my_ptr_char2 = "C++";

std::cout << "The memory from the int var: "<<my_ptr_char1<<"\n";
std::cout << "The declared string: "<<my_ptr_char2<<"\n";
return 0;
}```
Hope this clears a bit

3. I kinda get it. Pointers are like arrows that points to how much memory a variable allocates? like if I declare a "short" variable, the pointer will carry its memory slot?

4. I kinda get it. Pointers are like arrows that points to how much memory a variable allocates
Yes, if you assign a pointer to a memory adress of a variable. If instead you allocate size of n bytes to that pointer, the pointer then will not only refer to a single variable, but a block of memory of several aligned and consecutive variables
like if I declare a "short" variable, the pointer will carry its memory slot?
I didn't get the short part.. If you declare a variable, that variable will be somewhere in your memory. That variable will contain it's value, and will have also a constant memory adress. Then you can read that adress with the '&' operator and store it in a pointer to manipulate it however you may want. A pointer is nothing more than a variable, which content is a memory adress of another variable.

5. int contains an integer

double contains a floating point value

string contains letters

bools contain true/false

6. I kinda get it now. Malloc is a pointer that points to a block with 100 bytes right? and whenever I wanna delete a pointer just enter this "delete ptr"?

7. NOOOOOOOOOOOOO!!!
malloc is a function that returns a block of n bytes, being n the parameter you send.
So
Code:
```int *pi = malloc(4);
char *pc = malloc(1);
char *pc = malloc(34);
void *pi = malloc(3456);```
So we'll have respectively, blocks of 4 bytes, 1 yte, 34 bytes and 3456 bytes...
AND your learning C++. Memory allocations in C++ are done with the new operator. And pointers are deleted with the delete operator. This operator frees the memory previously allocated.
So the above code could be
Code:
```int *pi = new int[1];
char *pc = new char[1];
char *pc = new char[34];
void *pi = new char[3456];```
malloc is only a function, such as calloc, which allocate memory. These functions are from the C standard, not C++, but I used them only as example.

8. While malloc and calloc are supported in C++ (and of course free() too), as xErath has pointed out the preferred method of allocating extended blocks of memory is using the new keyword, and the C++ equivalent of free() is delete[] if you want to get rid of a block of memory. Be careful not to mix malloc() with delete[], and same goes for new and free() - they're not compatible.

Code:
```char* c = new char[100];  //allocate enough memory to hold 100 char's
char* d = new char[200];  //allocate enough memory to hold 200 char's
int* e = new int[100];  //allocate enough memory to hold 100 int's

delete[] c;  //Be sure to free any memory that you allocated earlier!
delete[] d;
delete[] e;```

9. Oh okay. Pointers just allocates memory for variables. I get it now. Thanks alot guys!

10. >>Pointers just allocates memory for variables.
Ahhhhh nononono, pointers just point to locations in memory. new and [b]delete[] are keywords that CREATE blocks of memory (or delete), and then give you the memory address of that block of memory so that you can then store the ADDRESS of the memory in a pointer.

11. The pointer itself is just an address (nothing more). The new operator allocates memory of the given size, initializing the data block if appropriate, and simply returns the address of the newly created object (or first of the newly created objects in the case of new[]).

12. Code:
`char text[]="happiness";`
when the compiler stores data it picks a location in memory for simplicity the text[] array starts at 0000;
|0000 | h |
|0001 | a |
|0002 | p |
|0003 | p |
|0004 | i |
|0005 | n |
|0006 | e |
|0007 | s |
|0008 | s |
|0009 | ! |
|000A | \0 |
|000B | ? | <---our pointer

When you create a pointer the computer reserves space in memory to store it. our pointer
is located at 000B It could be anywhere but to make it simple we put it here. It contains
undefined data yet we haven't initialized it. Now what we put in the data field would be
the address of one of the other variables so we would put address 0000 to get to the data
h:
|000B |0000| h |
Now to access the next letter we simply increment the pointer by the size of the type
(char=1byte) so we add 1 to the address stored in our pointer it now points to a;
000B| 0001| a |

if we were working with an integer array (integers are 4bytes) then the address would be
multiples of 4
Code:
`int numberarray[5]={10,300,40,64,27};`
|0000 | 10 |
|0004 | 300 |
|0008 | 40 |
|000F | 64 |
|0014 | 27 |
|000B | ? | <---our pointer

The computer handles the addressing for different types(int char) automatically so you
don't have to worry about it all you do is increment the pointer by 1 and it figures out the
rest.

Whatever address we store in the pointer the data located at the address is what we are
able to access.
[code]
char text[]="happiness";
char*charptr=text; <--- this is saying put the address
of the start of text[] array in the pointer charptr;

char *charptr;

charptr=&text[0]; <--- this is also saying put the address of the first element of text[]
array in the pointer charptr;

char*charptr=text;

and

char *charptr;
charptr=&text[0];

are equalivlent statements they accomplish the same thing. It is just a matter of how you
want to look at it.

The first one is saying set the pointer to point to the array the other is saying set the
pointer to point to the first element in the array. The first one is treateing is like a block of
memory and will always point to the first element, and the second a single memory location
that just happens to be the first one in the array..

The second method allows you to point directly to a specific element inside the array.

so you use the & to make it point to the data structure( ie variable, array, class, function etc).
now to get the data u use the * to access the actual data stored at the address stored in your pointer.

Code:
```cout<<*charptr<<"\n"; would print the letter
cout<<charptr<<"\n"; would print the address of the letter.

*charptr='g'; would change the data from a to g```

13. Originally Posted by Kai_Legends
I'm a newbie so don't yell. I'm advancing up to Pointers now. And it is so confusing. I know pointers have something to do with memory addressing, but thats about it. Can anyone help me out? Just to make this easier to understand.
I won't yell. but aside from all the help you've received on the board, you should also learn the board is a
resource itself.. pointers are usally the most confusing ascpect of C programming. I'd assume C++ as well, so don't feel bad, even vetrans its said get confused on it sometimes.