|
|
|
|
Lesson#2
|
Basic C Language Concepts
|
|
|
|
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
Basic C Language Concepts 2
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"
Basic C Language Concepts 3
In this piece of code, we have taken a pointer to integer denoted by “*p”.
In second
statement, an integer is declared and initialized by ‘3’. The next step is
the most
important one. Here we are passing the “Address” of the integer “i” in the
pointer.
Since pointers hold the variable addresses; so now the pointer “p” contains
the
address of the integer “i” which has a value of 3.
Pointers Arithmetic
• Pointer Arithmetic deals with
performing addition and subtraction operations on
pointer variables.
• increment a pointer ( ++ )
• decrement a pointer ( -- )
• Address in pointer is incremented or
decremented by the size of the object it
points to (char = 1 byte, int = 2 bytes, ...)
Example: pointers increment and decrement
char x = 'A'; // variable declaration and initialization
int y = 32;
char *xPtr = &x;
int *yPtr = &y; // pointer declaration and initialization
...
xPtr--; //Since char takes 1 byte, and if xPtr has
// a value of 108 now it would have a value of
// address 107
xPtr++; // pointer would have a value of address 108
Here, we have explained that if we add 1 to a pointer to integer, then that
pointer will
point to an address two bytes ahead of its current location. Similarly, when
we
incremented the xPtr, the address it contained is incremented to one value
since xPrt
is a pointer to integer.
Note:
Value added or subtracted from pointer is first multiplied by size of object
it points to
Basic C Language Concepts 4
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.
Basic C Language Concepts 5
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.
Basic C Language Concepts 6
ptr_c = ptr_c +1;
Example:
float x[5];
Our memory model is
• x is a pointer to the first
element
• *x and x[0] are the same
• x and &x[0] are the same
• elements of an array can be accessed
either way
• x is an array object, not a pointer
object
Pointer Advantages
• This allows a function to "return" more than
a single value (we are not really
returning more than a single variable, but we are able to directly modify
the
values that are in main, from within a function).
• This allows us to refer to larger data
structures with just a single pointer. This cuts
back on creating multiple copies of a structure, which consumes both memory
and
time.
• This also opens the door to dynamic memory
allocation.
Pointer Disadvantages
• The syntax may be confusing
initially.
• Harder to debug, have to follow
pointers to make sure they are doing what is
expected.
• More Segmentation faults / Bus
errors
Basic C Language Concepts 7
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.
|
|
|
|