



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




- Chapter RAM (random access memory) is the place in a computer where the operating system, application programs, and data in current use are kept so that they can be quickly reached by the computer's processor. RAM is much faster to read from and write to than the other kinds of storage in a computer, i.e. the hard disk, floppy disk, and CD-ROM. However, the data in RAM stays there only as long as your computer is running. When you turn the computer off, RAM loses its data. When you turn your computer on again, your operating system and other files are once again loaded into RAM, usually from your hard disk.
RAM is the best known form of computer memory. RAM is considered "random access" because you can access any memory cell directly if you know the row and column that intersect at that cell.
Every byte in Ram has an address.
00000000 00000000 00000000 00000001
00000000 00000010
... ... ... ... 11111111 11111111
Essentially, the computer's memory is made up of bytes. Each byte has a number, an address, associated with it. “Pointer is a kind of variable whose value is a memory address, typically of another variable”. Think of it as an address holder, or directions to get to a variable.
*int p; int i = 3; p = &i;
... yPtr-=3; // Since int takes 2 byte, and assume that yPtr was //pointing to address of 109, now it points to address of // 103 as 109 - (3 * 2) = 103
yPtr+=1; // now yPtr points to address of 105
This means that in the above statement when we will add 1 to the yPtr, where yPtr is a pointer to integer, then the pointer will skip two bytes once and will point to an address of 105 instead of 103.
... if (xPtr == zPtr)
cout << "Pointers point to the same location";
else
cout << "Pointers point to different locations";
int b[100]; // b is an array of 100 ints.
int p; //* p is a pointer to an int.
p = b; // Assigns address of first element of b to p.
p = &b[0]; // Exactly the same assignment as above.
In this piece of code, we have used the name of an array as pointer to first address of array. In fact the name of the array is a constant pointer i.e. b is a constant pointer whereas p is a variable pointer. We can change the contents of variable pointer but not or constant pointer. In the last statement, we are assigning the address of b, i.e. the constant pointer to the variable pointer i.e. p.
As we have already discussed above that when you declare an array, the name is a pointer. You cannot alter the value of this pointer. In the previous example, you could never make this assignment.
b = p; // ILLEGAL because b is a constant pointer.
**float x[15]; float y = &x[0]; float z = x;
*int ptr; int j = 10;
*ptr = &j; printf ("%d\n", ptr);
**ptr = 15; printf ("%d %d\n", ptr, j);
*if (ptr != 0) { printf ("Pointer ptr points at %d\n", ptr); }
**double d; double ptr_d; char c; char ptr_c; ptr_d = &d; ptr_c = &c; //This operation will skips 8 bytes in memory because ptr_d is a pointer to double. ptr_d = ptr_d + 1; //This operation will skips 1 byte in memory because ptr_c is a pointer to character.
float x[15]; float y, z; y = x; / Right / z = x; / Wrong /*
Tips
Summary
In this lecture we started our discussion by revising our basic concepts like RAM. Then we have discussed pointers, how pointers are initialized, what is meant by Pointer Arithmetic. Pointers are very important and useful as with the help of them we can access a very large data structure, similarly other advantages and a few disadvantages of pointers have also been discussed.