Starter on Understanding Pointers

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
The post below is in response to a EDMW spoof found at http://forums.hardwarezone.com.sg/e...mmers-who-dont-know-what-pointer-4880455.html

However I would rather newbie programmers who visit this forum to acquire better understanding about such things instead of the fast flowing EDMW which will quickly bury these information.

It is by no means a complete commentary on pointers. Interested parties should go and read up the relevant topics on your own. However what I have shared is the mentality on why a computer scientist should be proficient in the pointer theory, so that it can kick start more in-depth knowledge that will benefit him/her on long run in the computing career.

==========================================================

In any case, since there seems to be so much opinions about pointers, then I will give mine here - If you don't know pointers, your computing knowledge is very much limited. Does it means you can't do great things ? Nope, you still can produce good works, since there are a lot of areas of works that does not require good understanding of computer memory models.

First I saw a closest explanation about pointer, while correct, is not really explaining about pointers. There is a mixture of another topic known as memory models. Pointer is a high level abstraction from memory models, but does in some way related. Still the explanation shouldn't involve it. I will get to the part shortly.

First thing first. Pointer is nothing more than a variable that stores memory address. Here we don't go into what is exactly stored, but rather we assume it is a memory address to somewhere. In more concise, it is just an integer.

When we discuss about pointers, we often need to take into consideration another programming language concept known as Type. Without involving the typing system, pointer will be less powerful. Some have posted the difficulty of dealing with pointers having that it results in memory leakage that causes crashes or memory segmentation fault or bus faults is an incorrect perception. Pointers can co-exist with automatic memory management subsystem to garbage collect unreachable memory blocks. However, software developer must learn how to play nice with the AMM subsystem since these systems are normally not part of the programming language semantics and hence some rules have to be obeyed.

I will also expand the topic into references, object oriented view of the issue with relevant to pointers.

Lets start with something simple
Code:
int n = 10;
int *p = &n;

2 variables are created, a primitive integer and the other the pointer to an integer. The memory address of integer variable "n" is assigned to variable "p". Understanding computer memory model means we know the compiler will produce codes that allocate a block of memory with the placeholder called "n". The 2's complement binary representation of the integral value "10" bin=(1010) will be stored at the memory location allocated for variable "n". Since "n" is an integer, on 32/64bits memory width system, it normally take the form of 4 bytes.

When the following code "int *p = &n;" is run. What it does is to assign the memory address of variable "n"(&n) to another memory allocation created for variable "p". Depending on which kind of memory system, the pointer variable "p" will be either 16/32/64bits in storage size. However still explaining pointers normally don't need to involved it yet.

Assuming variable "n" is created at memory location 12345678 during runtime, then we can safely assume the value of variable "p" will be 12345678 too. This is still a high level explanation of Pointer. When one run codes like
'printf("%d", n);', the system is always dealing with memory address. The system don't care what "n" is. It's like a printed label that you pasted onto pigeon holes that you name the hole, but ultimately you can always count the pigeon holes #1, #2, #3, ...

A name makes it easier for human to refer to. That means simple instructions like 'printf("%d", n);' basically instruct the system to assign the memory address of variable "n" to a register, invoke the "printf" routine to print the contents found at memory location of variable "n". This is a naive way to looking at what is going on.

Variable "p" meanwhile stores 12345678, which is not very useful on it own. However when couple with the TYPE of the variable "p", the compiler understands that the value is a memory address at 12345678 and what this memory location store is an integer of 4 bytes. That means the data is at memory locations 12345678, 12345679, 12345680 and 12345681 assuming each memory address is 1 byte. Up to there, I think it's pretty clear why earlier I mentioned the typing system has to be discussed with pointer, because they are closely related.

From the programming language standpoint, despite that memory address is what system works with, the label is often the context for programmers to use. Hence if we want to refer to memory address of variable "n", we use "&n" to reference the label to a memory location. Since this value is assigned to variable "p", hence to get the variable "n", we dereference the memory location using *p;

Hence here we observe type of "*p"(int) is equivalent to type of variable "n"(int). Likewise &n(int*) is the same type as p(int*).

As we refer to pointers, there is another type of referencing technique known as References. Different programming languages have slightly different semantics to references, but ultimately it is derived from pointers. In C++, reference must refer to an entity. Unlike pointers, it is not allowed for reassignment. However in Java, there is also reference. The semantics in Java is different in that it functions similarly to pointers except that there is no pointer arithmetic, and you can't explicitly use it to reference any memory location. It must refer to either NULL or an existing entity with known memory addresses.

The semantics enforced by Java is stronger and more strict in order to facilitate the proper operation of the Garbage collector. In terms of programming features, references are weaker than pointers, but they do helps to provide safer programming techniques with the help of the automatic memory management subsystem known as the garbage collector.

In C++ references exist in the following form
Code:
int n = 10;
int &q = n;

May it be references or pointers, the concept exist in all programming languages and influence the perception of data structures.

Analogous to the classic wave theory and quantum mechanics of light in the physics. Pointers or References and Objects are interchangeable.

Using the Java example below
Code:
class Node {
  int data;
  Node next;
}
Node root = new Node();

We can tell this is a data structure of a Linked List. How would you have visualise it in both different approaches
2hcdk5s.jpg


The top shows the pointers/references concept, the one below is OOP approach. However you will find lecturers don't describe these 2 forms to you even though it is a linked list. The top one is even often the case no matter it is OOP or normal classic references and pointers.

The whole concept on how you visual thing comes from the knowledge of understanding what really is pointers, how does it related to references and how it is a linkage concept.

It is already a very long post and yet I must say it merely scratches the surface. If we dwell deeper, we can discuss about endianness of the system and how it affects pointers, the way to calculate the size of structures with respect to pointers and alignment.

We can also further look at real memory model, protected memory model and discuss why pointers concept are not directly affected by still in ways relevant. How in real memory model that segment selectors are used, how in the 32bits protected memory model that they are described with paging tables and so forth.

My point is it has to start from pointers because it built up the basic understanding about memory and hence it will broaden your knowledge on computing. That's why it comes back to my earlier saying - "If you don't know pointers, your computing knowledge is very much limited"

It is the touching stone to kick start deep understanding of memory and how they are managed. I'm afraid just proficient with high level programming language wouldn't get you very far. When performance tuning is required, there is always a dark area which you will not understand and hence prevented you from truly appreciating computer science.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Yet Another Stab at education through pointers concept

This post is a copy-and-paste from the following post http://forums.hardwarezone.com.sg/90153237-post179.html found in thread http://forums.hardwarezone.com.sg/e...prospect-being-software-engineer-4885569.html

This gave me the opportunity to discuss in matters on how I perceive IT practitioners has bee sharing their opinion in HWZ. It sounds like opening a can of worms, pour it out in the public for all to ponder over.

I shifted this post here because I would love to see that these information not get drowned by the fast flowing EDMW.

Hence read it at your own free time :)

x86 should be:
1663
127

PowerPC:
1663
0

Cos char is 1 byte, so when you cast to int, the other 3 bytes kena chopped..
PowerPC is BE, hence the 32 bit value would be swapped and when kenna chopped, becomes zero.

32 bit access not affected by endianess

Good finally someone got the idea of endianness, but I think there is some flaws in the explanation. Nothing get chopped, it's just what is observed.

In another thread on pointers, I have explicitly mentioned that pointers must always be discussed with the typing system.

Since most systems today are at least 32bits in register width, so you will find the answer you get may not be the same when working with 16bits system. You will also find bitwise operations in C wouldn't have endianness issues because C does shield us from some of these platforms incompatibilities. But when playing with pointers, you are quite directly dealing with memory addresses, as such, the underlying incompatibilities start to surface.

Lets demystify what is going on here.

First look at the value of "1663" on a 32bits system

Code:
00000000 00000000 00000110 01111111

This is normally how you will visialise it. This visualisation is big endian mode. If you are using little endian visualisation, it should looks like this

Code:
01111111 00000110 00000000 00000000

Why so ? Historically, computers do not start off using 16 or 32 or 64 bits register width as we see today. They operate on merely 8bits. In fact, for really early days, there could be small calculators(also known as computers) that operate on nibble mode (4bits)

However when it comes to 16 bits, a byte(8bits) is the mode of addressing memory. Any smaller is inefficient since this is the width that is built on. So you see history are very important to explain why things turn out this way right (not limited to science and computing) ?

So since memory are addressed in byte width, what happens to 16bits ? Which should comes first at the lower memory address ? Using the number 1663(00000110 01111111), should "00000110" be placed first at memory address X and "01111111" at memory address (X+1) or the other way round.

It could be due to architecture design for performance since not all machines are built the same from an electronic standpoint, or it could be just a man-made decision, so some platforms like Intel are on little endian and PowerPC from Motorola choose big endian.

Going back to pointers and memory addressing concepts and using the code shared earlier
Code:
#include <stdio.h>

int main(int argc, char **argv) {
  int n = 1663;
  int *m = &n;
  char *b = &n;
  printf("%d\n", *m);
  printf("%d\n", *b);
  return 0;
}

Pointer "b" is of type "char" which is 8 bits in C. since pointer store memory addresses, hence it will store only ONE(1) memory address which I assume to be YYY.

Thus using the following illustration
Code:
MEM ADDR (YYY)    (YYY+1)  (YYY+2)  (YYY+3)     

BIG ENDIAN
         00000000 00000000 00000110 01111111
         
LITTLE ENDIAN
         01111111 00000110 00000000 00000000

Despite both pointer "m" and "b" store the same memory address YYY, the typing system allows for endian-ness exposure or not. For integer pointer(32bits) on 32bits system in C, endianness is resolved either by choosing 32bits width register or resolved when C produces the necessary operation codes for swapping.

For pointer "b", endianness is still settle, but it's pointless for 8bits type, hence you will get a different value "127" for little endian and "0" for big endian.

If you are on 16bits system, you should get the decimal value of "00000110", since it should look like this
Code:
MEM ADDR (YYY)    (YYY+1)     

BIG ENDIAN
         00000110 01111111
         
LITTLE ENDIAN
         01111111 00000110

So does it means if I use very high level programming languages like Java which provide it's own VM environment or Javascript that works in browser, and hence we do not need to understand memory concepts ? Answer is far from NO.

Refer to one of my earlier post found at http://forums.hardwarezone.com.sg/p...erter-partial-big-integer-module-4855408.html

I will share a fragment of Java codes
Code:
BigInt add(BigInt b) { 
  BigInt d = new BigInt(); 
  int over = 0; 
  for (int i = 0; i < store.length; i++) { 
    d.store[i] = (byte)(store[i] ^ b.store[i] ^ over); 
    over = (byte)(((store[i] ^ b.store[i]) & over) | 
                  (store[i] & b.store[i])); 
  } 
  return d; 
}

Can you guys identify that it is a full adder electronic concept ?
Full-Adder-Circuit.gif


Would you be able to write out this piece of code, if you have no slightest idea of how a full adder works ? My code here theoretically will allow you to add to infinite number of any bits width, limited by hardware resources. From performance standpoint, the approach I took will play very well with Predictive Branching in modern processors since there is no chance for instruction pipeline flushing due to a one way traffic in such tight loop. When implemented into native machine opcodes, you should get a very low latency and highly packed pipeline in a deep prefetch queue processor design. And where do you think I learnt about these stuffs ? Right here in NUS SOC studying Computer Organisation and Architecture

I really hope this whole session will get drill into some of you guys mind and benefit it in your life as a life long lesson on how you should equip yourself with knowledge. It's not the certificate that you should be chasing, it's the KNOWLEDGE.

I can be daring to say, you wouldn't find a lot of NUSSOC students or graduates that can write codes the way I would have approach them. To me, my skill is only bounded by knowledge. Even reality is changing as we speak, where theoretical physicist challenge the facts as we know today. NP problems turning P as we discover different methods of doing the same thing.

As always I end my words with the following -

Stay Hungry Stay Foolish
 

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
The post below is in response to a EDMW spoof found at http://forums.hardwarezone.com.sg/e...mmers-who-dont-know-what-pointer-4880455.html

However I would rather newbie programmers who visit this forum to acquire better understanding about such things instead of the fast flowing EDMW which will quickly bury these information.

It is by no means a complete commentary on pointers. Interested parties should go and read up the relevant topics on your own. However what I have shared is the mentality on why a computer scientist should be proficient in the pointer theory, so that it can kick start more in-depth knowledge that will benefit him/her on long run in the computing career.

==========================================================

In any case, since there seems to be so much opinions about pointers, then I will give mine here - If you don't know pointers, your computing knowledge is very much limited. Does it means you can't do great things ? Nope, you still can produce good works, since there are a lot of areas of works that does not require good understanding of computer memory models.

First I saw a closest explanation about pointer, while correct, is not really explaining about pointers. There is a mixture of another topic known as memory models. Pointer is a high level abstraction from memory models, but does in some way related. Still the explanation shouldn't involve it. I will get to the part shortly.

First thing first. Pointer is nothing more than a variable that stores memory address. Here we don't go into what is exactly stored, but rather we assume it is a memory address to somewhere. In more concise, it is just an integer.

When we discuss about pointers, we often need to take into consideration another programming language concept known as Type. Without involving the typing system, pointer will be less powerful. Some have posted the difficulty of dealing with pointers having that it results in memory leakage that causes crashes or memory segmentation fault or bus faults is an incorrect perception. Pointers can co-exist with automatic memory management subsystem to garbage collect unreachable memory blocks. However, software developer must learn how to play nice with the AMM subsystem since these systems are normally not part of the programming language semantics and hence some rules have to be obeyed.
Hi, what is AMM subsystem, can you explain more how it is linked to programming? Actually i dont understand the part in bold

I will also expand the topic into references, object oriented view of the issue with relevant to pointers.

Lets start with something simple
Code:
int n = 10;
int *p = &n;

2 variables are created, a primitive integer and the other the pointer to an integer. The memory address of integer variable "n" is assigned to variable "p". Understanding computer memory model means we know the compiler will produce codes that allocate a block of memory with the placeholder called "n". The 2's complement binary representation of the integral value "10" bin=(1010) will be stored at the memory location allocated for variable "n". Since "n" is an integer, on 32/64bits memory width system, it normally take the form of 4 bytes.
4 byte is it because "1010" individual "number" take 4 bit ( so 1010 is actually 0001 0000 0001 0000)

When the following code "int *p = &n;" is run. What it does is to assign the memory address of variable "n"(&n) to another memory allocation created for variable "p". Depending on which kind of memory system, the pointer variable "p" will be either 16/32/64bits in storage size. However still explaining pointers normally don't need to involved it yet.

Assuming variable "n" is created at memory location 12345678 during runtime, then we can safely assume the value of variable "p" will be 12345678 too. This is still a high level explanation of Pointer. When one run codes like

why would "p" be 12345678 too? Wouldn't "p" be allocated a different memory address?
Similar to this image
http://upload.wikimedia.org/wikipedia/commons/thumb/b/b4/Pointers.svg/2000px-Pointers.svg.png


'printf("%d", n);', the system is always dealing with memory address. The system don't care what "n" is. It's like a printed label that you pasted onto pigeon holes that you name the hole, but ultimately you can always count the pigeon holes #1, #2, #3, ...

A name makes it easier for human to refer to. That means simple instructions like 'printf("%d", n);' basically instruct the system to assign the memory address of variable "n" to a register, invoke the "printf" routine to print the contents found at memory location of variable "n". This is a naive way to looking at what is going on.

Variable "p" meanwhile stores 12345678, which is not very useful on it own. However when couple with the TYPE of the variable "p", the compiler understands that the value is a memory address at 12345678 and what this memory location store is an integer of 4 bytes. That means the data is at memory locations 12345678, 12345679, 12345680 and 12345681 assuming each memory address is 1 byte. Up to there, I think it's pretty clear why earlier I mentioned the typing system has to be discussed with pointer, because they are closely related.

From the programming language standpoint, despite that memory address is what system works with, the label is often the context for programmers to use. Hence if we want to refer to memory address of variable "n", we use "&n" to reference the label to a memory location. Since this value is assigned to variable "p", hence to get the variable "n", we dereference the memory location using *p;

Hence here we observe type of "*p"(int) is equivalent to type of variable "n"(int). Likewise &n(int*) is the same type as p(int*).

As we refer to pointers, there is another type of referencing technique known as References. Different programming languages have slightly different semantics to references, but ultimately it is derived from pointers. In C++, reference must refer to an entity. Unlike pointers, it is not allowed for reassignment.
I dont really understand, can you show example? Does it mean if i change int n,by using pointer, pointer p would get the new value of n. However if i use reference, p would not get the new value?

However in Java, there is also reference. The semantics in Java is different in that it functions similarly to pointers except that there is no pointer arithmetic, and you can't explicitly use it to reference any memory location. It must refer to either NULL or an existing entity with known memory addresses.

The semantics enforced by Java is stronger and more strict in order to facilitate the proper operation of the Garbage collector. In terms of programming features, references are weaker than pointers, but they do helps to provide safer programming techniques with the help of the automatic memory management subsystem known as the garbage collector.

In C++ references exist in the following form
Code:
int n = 10;
int &q = n;

May it be references or pointers, the concept exist in all programming languages and influence the perception of data structures.

Analogous to the classic wave theory and quantum mechanics of light in the physics. Pointers or References and Objects are interchangeable.

Using the Java example below
Code:
class Node {
  int data;
  Node next;
}
Node root = new Node();

We can tell this is a data structure of a Linked List. How would you have visualise it in both different approaches
2hcdk5s.jpg


The top shows the pointers/references concept, the one below is OOP approach. However you will find lecturers don't describe these 2 forms to you even though it is a linked list. The top one is even often the case no matter it is OOP or normal classic references and pointers.

The whole concept on how you visual thing comes from the knowledge of understanding what really is pointers, how does it related to references and how it is a linkage concept.

It is already a very long post and yet I must say it merely scratches the surface. If we dwell deeper, we can discuss about endianness of the system and how it affects pointers, the way to calculate the size of structures with respect to pointers and alignment.

We can also further look at real memory model, protected memory model and discuss why pointers concept are not directly affected by still in ways relevant. How in real memory model that segment selectors are used, how in the 32bits protected memory model that they are described with paging tables and so forth.

My point is it has to start from pointers because it built up the basic understanding about memory and hence it will broaden your knowledge on computing. That's why it comes back to my earlier saying - "If you don't know pointers, your computing knowledge is very much limited"

It is the touching stone to kick start deep understanding of memory and how they are managed. I'm afraid just proficient with high level programming language wouldn't get you very far. When performance tuning is required, there is always a dark area which you will not understand and hence prevented you from truly appreciating computer science
.

what the point you trying to bring across? i dont get the whole paragraph.

Why would we need pointer, why cant we directly use int n?

Hi TS,
After reading through, i have quite a few question, hope you can help to clear my doubts.
Thanks.

Regarding array, how to solve the problem if i need to get a non fixed number to put into an array.

For example,
The number of element input from the user can be 1-100. However in array we need to declare in the for like myArray[10]. If the user input 11, the 11th element will not be passed into myArray. Therefore can you create a tutorial for array?

Thank you
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Hi TS,
After reading through, i have quite a few question, hope you can help to clear my doubts.
Thanks.

P.s Hope you will be doing one for array also. I am struggling with array

You have to be more specific on what you want. Please be noted I don't provide answers to your assignments ok ? However I will be glad to help answer your doubts as long as you are specific on what you are asking. The better and more inputs you can provide, the better I can answer to what you wanted.

What are you referring to when you asked for me doing one for array also. Elaborate please.
 

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
You have to be more specific on what you want. Please be noted I don't provide answers to your assignments ok ? However I will be glad to help answer your doubts as long as you are specific on what you are asking. The better and more inputs you can provide, the better I can answer to what you wanted.

What are you referring to when you asked for me doing one for array also. Elaborate please.

I am not asking you to help me in my assignment.

I have a question regarding array. Currently i am learning c++, so my array will be refered to c++.

My problem is that, array work like this

myArray[5]={0,1,2,3,4}; this is a fixed array
myArray[5]; this is a "free" array, which means it is waiting for element to pass be pass into this array?

i am not sure if there is a way for me to declare an array that is not limited and will be design to suit the user input.

From what i know i cant declare an array like this;
myArray[]
if i initialised like this i will get compilation error. What would be a way to overcome it.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
I am not asking you to help me in my assignment.

I have a question regarding array. Currently i am learning c++, so my array will be refered to c++.

My problem is array work like this

myArray[5]={0,1,2,3,4}; this is a fixed array
myArray[5]; this is a "free" array, which means it is waiting for element to pass be pass into this array?

so i am wonder is there anyway for me to declare an array that does is not limited. It will be design to suit the user input. From what i know i can declare an array like this

myArray[]; if i initialised like this i will get compilation error. What would be a way to overcome it.

In C/C++, both the following notations are actually pointers. Arrays and pointers are very close in nature.
Code:
func(int arr[]);
int *arr = ...;

However you can't declare the following, this is not a correct syntax in both C/C++
Code:
int arr[];

With regards to the following
Code:
int myArray[5]={0,1,2,3,4};
int myArray[5];

Both of the above create 5 elements of int array, the first include an initialiser which initialise the values in the array. The latter is still a 5 elements array but initialisation will only happens if it is a static or global variable. Stack variables do not get initialise.

Compile and run the code below and you will get the following output
OUTPUT:
Code:
$ ./test
IN MAIN: 0
IN B: 0  <--- NOT NECESSARILY ZERO, MOST LIKELY, BUT CAN BE ALSO ANY NUMBER (including 123)
IN A: 123
IN B: 123 <--- notice how the "123" left over from the last stack frame when inside function A get into here ?
IN MAIN: 0

CODE:
Code:
#include <stdio.h>

int a[1];

void funcA() {
  int a[1];

  a[0] = 123;

  printf("IN A: %d\n", a[0]);
}

void funcB() {
  int a[1];

  printf("IN B: %d\n", a[0]);
}

int main(int argc, char **argv) {

  printf("IN MAIN: %d\n", a[0]);

  funcB();
  funcA();
  funcB();

  printf("IN MAIN: %d\n", a[0]);

  return 0;
}
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,550
Reaction score
1,302
Hi TS,
After reading through, i have quite a few question, hope you can help to clear my doubts.
Thanks.

Regarding array, how to solve the problem if i need to get a non fixed number to put into an array.

For example,
The number of element input from the user can be 1-100. However in array we need to declare in the for like myArray[10]. If the user input 11, the 11th element will not be passed into myArray. Therefore can you create a tutorial for array?

Thank you

Hi, what is AMM subsystem, can you explain more how it is linked to programming? Actually i dont understand the part in bold

Automatic Memory Management system is a runtime system that manage dynamic memory in applications without the explicit intervention from developers. Without AMM, developers normally are required to deallocate memory from their applications back to the operating system such as the following
Code:
int *ptr = new int[100];
// do something with the dynamic array
delete[] ptr; // relinquish the block of memory occupied by "new int[100]" back to the operating system.

With automatic memory management such as what you will see in Java,
Code:
int[] array = new int[100];
array = null;

There is no explicit call to the operating system to return the memory block back to the OS for reuse. There is normally a runtime system running together with the application in the background, performing all the allocation and deallocation of dynamic memory acquired by the application during its runtime. This is commonly known as a garbage collector. Refer to http://en.wikipedia.org/wiki/Boehm_garbage_collector for more information regarding GC for C/C++

4 byte is it because "1010" individual "number" take 4 bit ( so 1010 is actually 0001 0000 0001 0000)

Pointers are memory address container. As such, their width must correspond to the addressable memory addresses possible in a process. In 32bits, an application in theory can address from 0 - ((2^32)-1) memory addresses, as such, it's 32bits and hence 4 bytes. 64bits can address from 0 - ((2^64)-1) and therefore require 8 bytes. It has nothing to do with what is stored in the pointer value.

why would "p" be 12345678 too? Wouldn't "p" be allocated a different memory address?

"p" is an integer pointer, that means it store value to point to an integer type. When I refer to integer type, it doesn't need to be a variable. It can be just any memory location that is used to store an integer.

Code:
int n = 10;
int *p = &n;

The code above is assigning the memory location of variable "n" to integer pointer "p". The concept of pointing is to point to a memory location, hence the memory location is the value contained by "p". I am just giving an example of a imaginary memory location 12345678, it can be any memory location allocated at runtime by the operating system, so it doesn't matter. What is stored in "p" is not the memory location of "p", it is the memory location of "n". Don't be confused here.

I dont really understand, can you show example? Does it mean if i change int n,by using pointer, pointer p would get the new value of n. However if i use reference, p would not get the new value?

Go read up about REFERENCES in C++. It is reading exercise for you. There is nothing for me to explain here over C++ semantics on how REFERENCES are used in C++.

what the point you trying to bring across? i dont get the whole paragraph.

The image shows how a simple data structure called Linked List can be mentally visualised using different concepts of programming language design model. The top is a traditional referenced model. The bottom is an Object Oriented Design model. I can also visualise linked list in a tuple model or tree model used by functional languages

This is a linked list if you can visualise it
Code:
(A, (B, (C, (D, (E, (F, NIL))))))
 
 /\
A /\
 B /\
  C /\
   E /\
    F NIL

Regarding array, how to solve the problem if i need to get a non fixed number to put into an array.

For example,
The number of element input from the user can be 1-100. However in array we need to declare in the for like myArray[10]. If the user input 11, the 11th element will not be passed into myArray. Therefore can you create a tutorial for array?

2 approaches normally done depending on how advance you want things to be

For array approaches, you can do this using dynamic array expansion
Code:
int size = 10;
int length = 0;
int *arr = new int[size];

while (...) {
  int input = ...; // get input from user
  if (length >= size) {
    int *new_arr = new int[size * 2];
    memcpy(new_arr, arr, size);
    delete[] arr;
    arr = new_arr;
    size *= 2;
  }
  arr[length++] = input;
}

Another way is use linked list or a dynamic vector(actually is a dynamic array like what I have shown above), meaning dynamic size containers to contain your inputs.
 
Last edited:
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top