Sinisterly
[C++] Understanding Pointers and Their Useage - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: [C++] Understanding Pointers and Their Useage (/Thread-C-Understanding-Pointers-and-Their-Useage)

Pages: 1 2


[C++] Understanding Pointers and Their Useage - TheUninvited_mybb_import15721 - 03-13-2014

Hello HC Smile

Today i introduce .. POINTERS!

As i have stated in another thread A pointer is just an address to a location in memory. Depending on the architecture the address is usually 32 or 64 bits. A pointer itself is just two instructions that add two 16 bit integers based on high and low bitvalues.

But to help you better understand them here is a tutorial for you Smile

One of the most powerful tools available to a C++ programmer is the pointer. Pointers provide the capability to manipulate computer memory directly. That power comes at a price. Pointers are one of the most difficult aspects of C++ for many beginners to learn.

A variable is an object that hold's a value. A integer variable holds a number. A character variable holds a letter. A pointer is a variable that holds a memory address.

Okay, so what is a memory address ? To fully understand this, you must know a little about computer memory. Computer memory is where variable values are stored. By convention, computer memory is divided into sequentially numbered memory locations. Each of these locations is a memory address

Every variable of every type is located at a unique address in memory. This next diagram i made shows a schematic representation of the storage of a unsigned long integer variable ' theAge ':

[Image: 7u4dH.jpg]

Different computers number the memory using different complex schemes. Usually, programmers don't need to know the particular address of any given variable because the compiler your using handles the details. If you want this information, you can use the address of operators & which is illustrated in this example

Code:
#include <iostream> int main() { unsigned short shortVar = 5; unsigned long longVar = 65535; long sVar = -65535; std::cout << "shortVar:\t" << shortVar; std::cout << "\tThe adress of shortVar:\t" << &shortVar << "\n"; std::cout << "longVar:\t" << longVar; std::cout << "\tThe adress of longVar:\t" << &longVar << "\n"; std::cout << "sVar:\t" << sVar; std::cout << "\tThe adress of sVar:\t" << &sVar << "\n"; return 0; }
Address produces output such as the following:

[Image: 7u4fx.png]

the actual address of each pointer will differ because each computer will store variables at different addresses, depending on what else is in the memory and how much memory is available.

When a pointer is allocated, the compiler assigns enough memory to hold an address in your hardware and operating system environment. The size of a pointer might or might not be the same size as an integer, so be sure not to make any assumptions

I Hope this tutorial gives you a basic idea of how pointers work Smile thanks for reading


RE: [C++] Understanding Pointers and Their Useage - Psycho_Coder - 03-13-2014

Nice effort!

You actually explained about memory and basics of pointer usage. But haven't actually taught or given any notes on Pointers and their implementations. I know that the thread is about understanding pointers but to understand pointers better their usage and and how they are implemented in C++ language must be said. Also the code example you gave doesn't have any pointers. If you may then give an example that shows the utility of pointers.


Thank you,
Sincerely,
Psycho_Coder.


RE: [C++] Understanding Pointers and Their Useage - TheUninvited_mybb_import15721 - 03-13-2014

(03-13-2014, 09:07 PM)Psycho_Coder Wrote: Nice effort!

You actually explained about memory and basics of pointer usage. But haven't actually taught or given any notes on Pointers and their implementations. I know that the thread is about understanding pointers but to understand pointers better their usage and and how they are implemented in C++ language must be said. Also the code example you gave doesn't have any pointers. If you may then give an example that shows the utility of pointers.


Thank you,
Sincerely,
Psycho_Coder.

Yes i did explain memory because i believe that it is an important topic especially when talking about pointers, this tut was really just to help beginners to c++ grasp the idea of how a point could be used, and the address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". For example:


Code:
foo = &myvar;


This would assign the address of variable myvar to foo; by preceding the name of the variable myvar with the reference operator (&), we are no longer assigning the content of the variable itself to foo, but its address Smile


RE: [C++] Understanding Pointers and Their Useage - Psycho_Coder - 03-13-2014

(03-13-2014, 09:23 PM)TheUninvited Wrote:
(03-13-2014, 09:07 PM)Psycho_Coder Wrote: Nice effort!

You actually explained about memory and basics of pointer usage. But haven't actually taught or given any notes on Pointers and their implementations. I know that the thread is about understanding pointers but to understand pointers better their usage and and how they are implemented in C++ language must be said. Also the code example you gave doesn't have any pointers. If you may then give an example that shows the utility of pointers.


Thank you,
Sincerely,
Psycho_Coder.

Yes i did explain memory because i believe that it is an important topic especially when talking about pointers, this tut was really just to help beginners to c++ grasp the idea of how a point could be used, and the address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". For example:


Code:
foo = &myvar;


This would assign the address of variable myvar to foo; by preceding the name of the variable myvar with the reference operator (&), we are no longer assigning the content of the variable itself to foo, but its address Smile


I know that well I was just asking that a simple introduction to pointers would be good and how to create pointers in C/C++, or you can even link any other tutorial on pointers Smile


RE: [C++] Understanding Pointers and Their Useage - TheUninvited_mybb_import15721 - 03-13-2014

(03-13-2014, 09:33 PM)Psycho_Coder Wrote:
(03-13-2014, 09:23 PM)TheUninvited Wrote:
(03-13-2014, 09:07 PM)Psycho_Coder Wrote: Nice effort!

You actually explained about memory and basics of pointer usage. But haven't actually taught or given any notes on Pointers and their implementations. I know that the thread is about understanding pointers but to understand pointers better their usage and and how they are implemented in C++ language must be said. Also the code example you gave doesn't have any pointers. If you may then give an example that shows the utility of pointers.


Thank you,
Sincerely,
Psycho_Coder.

Yes i did explain memory because i believe that it is an important topic especially when talking about pointers, this tut was really just to help beginners to c++ grasp the idea of how a point could be used, and the address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". For example:


Code:
foo = &myvar;


This would assign the address of variable myvar to foo; by preceding the name of the variable myvar with the reference operator (&), we are no longer assigning the content of the variable itself to foo, but its address Smile


I know that well I was just asking that a simple introduction to pointers would be good and how to create pointers in C/C++, or you can even link any other tutorial on pointers Smile


I Will make another tutorial tonight that is an introduction to pointers and some other things thank you for your feed back Smile


RE: [C++] Understanding Pointers and Their Useage - rudy - 03-13-2014

(03-13-2014, 09:33 PM)Psycho_Coder Wrote: I know that well I was just asking that a simple introduction to pointers would be good and how to create pointers in C/C++, or you can even link any other tutorial on pointers Smile

(03-13-2014, 09:35 PM)TheUninvited Wrote:
(03-13-2014, 09:33 PM)Psycho_Coder Wrote:
(03-13-2014, 09:23 PM)TheUninvited Wrote:
(03-13-2014, 09:07 PM)Psycho_Coder Wrote: Nice effort!

You actually explained about memory and basics of pointer usage. But haven't actually taught or given any notes on Pointers and their implementations. I know that the thread is about understanding pointers but to understand pointers better their usage and and how they are implemented in C++ language must be said. Also the code example you gave doesn't have any pointers. If you may then give an example that shows the utility of pointers.


Thank you,
Sincerely,
Psycho_Coder.

Yes i did explain memory because i believe that it is an important topic especially when talking about pointers, this tut was really just to help beginners to c++ grasp the idea of how a point could be used, and the address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". For example:


Code:
foo = &myvar;


This would assign the address of variable myvar to foo; by preceding the name of the variable myvar with the reference operator (&), we are no longer assigning the content of the variable itself to foo, but its address Smile


I know that well I was just asking that a simple introduction to pointers would be good and how to create pointers in C/C++, or you can even link any other tutorial on pointers Smile


I Will make another tutorial tonight that is an introduction to pointers and some other things thank you for your feed back Smile


Hi. Smile

Long ago when I was learning pointers, I searched a lot of the internet for a good tutorial. By far, the most clear tutorial I found (and my favorite) was this:

http://alumni.cs.ucr.edu/~pdiloren/C++_Pointers/

This tutorial doesn't explain dynamic memory (operators new & delete in C++, or if you're doing C it's malloc() and free()), if you want a tutorial on that, let me know. Smile

Good luck.


RE: [C++] Understanding Pointers and Their Useage - TheUninvited_mybb_import15721 - 03-13-2014

(03-13-2014, 10:36 PM)rudy Wrote:
(03-13-2014, 09:33 PM)Psycho_Coder Wrote: I know that well I was just asking that a simple introduction to pointers would be good and how to create pointers in C/C++, or you can even link any other tutorial on pointers Smile

(03-13-2014, 09:35 PM)TheUninvited Wrote:
(03-13-2014, 09:33 PM)Psycho_Coder Wrote:
(03-13-2014, 09:23 PM)TheUninvited Wrote:
(03-13-2014, 09:07 PM)Psycho_Coder Wrote: Nice effort!

You actually explained about memory and basics of pointer usage. But haven't actually taught or given any notes on Pointers and their implementations. I know that the thread is about understanding pointers but to understand pointers better their usage and and how they are implemented in C++ language must be said. Also the code example you gave doesn't have any pointers. If you may then give an example that shows the utility of pointers.


Thank you,
Sincerely,
Psycho_Coder.

Yes i did explain memory because i believe that it is an important topic especially when talking about pointers, this tut was really just to help beginners to c++ grasp the idea of how a point could be used, and the address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". For example:


Code:
foo = &myvar;


This would assign the address of variable myvar to foo; by preceding the name of the variable myvar with the reference operator (&), we are no longer assigning the content of the variable itself to foo, but its address Smile


I know that well I was just asking that a simple introduction to pointers would be good and how to create pointers in C/C++, or you can even link any other tutorial on pointers Smile


I Will make another tutorial tonight that is an introduction to pointers and some other things thank you for your feed back Smile


Hi. Smile

Long ago when I was learning pointers, I searched a lot of the internet for a good tutorial. By far, the most clear tutorial I found (and my favorite) was this:

http://alumni.cs.ucr.edu/~pdiloren/C++_Pointers/

This tutorial doesn't explain dynamic memory (operators new & delete in C++, or if you're doing C it's malloc() and free()), if you want a tutorial on that, let me know. Smile

Good luck.


Hi thank you very much i will defiantly check it out and i understand how pointers work i think psycho_coder was just stating i could do a better job at explaining how a point works, rather than touch on memory, i will make another thread on how to get around DMA (dynamic memory allocation) in the future thanks again though Smile


RE: [C++] Understanding Pointers and Their Useage - ArkPhaze - 03-14-2014

If you're using C++11, I'd suggest using std::addressof() in special cases as well, because it will provide the proper address even with an overloaded reference & operator.

Code:
#include <iostream> struct mystruct_t { int x; int y; mystruct_t* operator &() { return nullptr; } }; int main() { mystruct_t obj; std::cout << &obj << std::endl; std::cout << std::addressof(obj) << std::endl; }



RE: [C++] Understanding Pointers and Their Useage - invisal - 03-14-2014

Thanks for the making this tutorial. Here a few things to point out

Quote:A variable is an object that hold's a value. A integer variable holds a number.
A character variable holds a letter. A pointer is a variable that holds a memory address.

To be precise, variable is not an object. variable is just an alias of memory address.
Once program is compiled, variable will be a mere memory address and the variable
name is lost.

All the variable are the same. It has binary-represented-value and address.
Variable type provide the meaning to those binary-represented value. Same
binary representation may mean different thing for different data type.

The following example shows that same binary representation has different
meaning based on the data type.
Code:
int main() { float a = 0.25f; // assuming 4 bytes int b = *((int*)&a); // assuming 4 bytes std::cout << a << std::endl; // 0.25 std::cout << b << std::endl; // 1048576000 }

Pointer is no different from other variable. It does not need to hold
memory address. However, like other data type. It adds the meaning
of the value. So when people see pointer variable, they confidently
assume that it holds memory address.

It is also true to other variable type. It also have ability to hold
memory address. For example:

Code:
int main() { float a = 0.25f; int b = (int)&a; *((float*)b) = 2.25; std::cout << a << std::endl; }



RE: [C++] Understanding Pointers and Their Useage - TheUninvited_mybb_import15721 - 03-14-2014

@invisal
In ways you are correct but everything in the syntax of a language is considered an object Tongue if you create compilers like i have you learn that because lexical analysis especially when it comes to parsing lexical tokens specifies everything in the syntax as an object, but other than that thank you for your feedback