Int pointer !!! | SoloLearn: Learn to code for FREE!

+2

Int pointer !!!

why we declare pointer as integer and the pointer is string variable ... ? for example : int score = 5; int *pointer; pointer = &score; output 0xbu77io 0xbu77io is not integer ?

7/4/2018 4:27:45 PM

Mohammad Alshareef

7 Answers

New Answer

+6

Did you remember to dereference the pointer when printing it?

+5

Well no, it's not an integer, it could be one of 2 things: 1– the address of the integer being pointed to 2– the address of the pointer. Since you stated that 0xbu77io was not an integer, I assumed you were asking why you don't get the integer. I apologize for the misunderstanding, but you should clarify that in your question. By the way, int *pointer is NOT a string variable. It's a pointer to an integer.

+5

You can declare a pointer of any type. Pointers are just memory that holds the location of one or more items of a certain type. So an int pointer is a variable that holds the address of one or more int values.

+1

Ace This is not my question you didnt understand me ^_^

+1

why that is not mistake to declare pointer as integer ... this as so clear answer it please .... 😚😙😗

+1

Perhaps this will explain: int score = 10; std::cout << &score << "\n"; std::cout << *(&score) << "\n"; int *ptr = &score; std::cout << ptr << "\n"; std::cout << *ptr << "\n"; You a pointer just stores a memory location. A memory location must be dereferenced to access its data. The pointer type tells us _how_ to dereference it: treat it like a char (1 byte), an int (4 bytes), a short (2 bytes) -- 32-bit x86 arch -- and so on. It tells us how to add those N bytes and compute the answer (well, not quite, but let's stick with that analogy).

0

Ace Thank you pro ^_^