Docsity

Basic C Language Concepts - Computer Programming - Lecture Notes, Study notes of Computer Engineering and Programming

Toggle sidebar
bg1
Chapter 2
Random access memory (RAM).................................................................................... 2
Pointer Definition .......................................................................................................... 2
How to assign a value to the pointer?................................................................... 2
Pointers Arithmetic....................................................................................................... 3
Example: pointers increment and decrement ...................................................... 3
Example: pointers addition and subtraction......................................................... 4
Example: pointers comparison (>, <, ==) ............................................................ 4
Arrays as Pointers........................................................................................................ 4
Array name is a const pointer ................................................................................ 4
A pointer can point to element of an array ........................................................... 5
Example: Pointer De-referencing .......................................................................... 5
Example: Pointer arithmetic. .................................................................................. 5
Example:.................................................................................................................... 6
Pointer Advantages...................................................................................................... 6
Pointer Disadvantages ................................................................................................ 6
What's wrong here? ................................................................................................. 7
Summary ....................................................................................................................... 7
Tips................................................................................................................................. 7
pf3
pf4
pf5

Partial preview of the text

Download Basic C Language Concepts - Computer Programming - Lecture Notes and more Study notes Computer Engineering and Programming in PDF only on Docsity!

 - Chapter 
  • Random access memory (RAM)....................................................................................
    • Pointer Definition
      • How to assign a value to the pointer?...................................................................
    • Pointers Arithmetic
      • Example: pointers increment and decrement
      • Example: pointers addition and subtraction
      • Example: pointers comparison (>, <, ==)
    • Arrays as Pointers........................................................................................................
      • Array name is a const pointer
      • A pointer can point to element of an array
      • Example: Pointer De-referencing
      • Example: Pointer arithmetic.
      • Example:....................................................................................................................
    • Pointer Advantages......................................................................................................
    • Pointer Disadvantages
      • What's wrong here?
    • Summary
    • Tips

Random access memory (RAM)

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

Pointer Definition

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.

How to assign a value to the pointer?

*int p; int i = 3; p = &i;

  • read & as "address of"

Example: pointers addition and subtraction

... 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.

Example: pointers comparison (>, <, ==)

... if (xPtr == zPtr)

cout << "Pointers point to the same location";

else

cout << "Pointers point to different locations";

Arrays as Pointers

  • An array name is actually a pointer to the first element of the array. For example, the following is legal.

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.

Array name is a const pointer

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.

A pointer can point to element of an array

**float x[15]; float y = &x[0]; float z = x;

  • y is a pointer to x[0]
  • z is also a pointer to x[0]
  • y+1 is pointer to x[1]
  • thus *(y+1) and x[1] access the same object
  • y[1] is same as *(y+1)
  • integer add, subtract and relational operators are allowed on pointers

Example: Pointer De-referencing

*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); }

  • *ptr de-references pointer to access object pointed at
  • *ptr can be used on either side of assignment operator
  • if ptr is equal to 0, then pointer is pointing at nothing and is called a null pointer
  • dereferencing a null pointer causes a core dump

Example: Pointer arithmetic.

**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.

What's wrong here?

float x[15]; float y, z; y = x; / Right / z = x; / Wrong /*

  • Y is a pointer to float so it can contain the starting address of array x
  • z is a float and not a float pointer

Tips

  • Use pointers when you want efficient results.
  • To develop plug-ins of existing software use pointers as much as you can.
  • Take extreme care while manipulating arrays with pointers.
  • Many bugs in large programmes arise due to pointers so only use pointers when necessary.
  • Make sure to initialize pointers with some valid value.
  • Don’t try to modify the contents of constant pointers.
  • Be sure that the data types of pointer variable and the pointed variable are same.
  • Do not assign system area addresses to pointers

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.