Pointers in C and C++

Posted by Tushar Bedekar
The correct understanding and use of pointers is crucial to successful C programming. There are several reasons for this: First, pointers provide the means by which functions can modify their calling arguments. Second, pointers support dynamic allocation. Third, pointers can improve the efficiency of certain routines. Finally, pointers provide support for dynamic data structures, such as binary trees and linked lists.
Pointers are one of the strongest but also one of the most dangerous features in C. For example, a pointer containing an invalid value can cause your program to crash. Perhaps worse, it is easy to use pointers incorrectly, causing bugs that are very difficult to find. Because of their importance and their potential for abuse, this chapter examines the subject of pointers in detail.

What Are Pointers?

A pointer is a variable that holds a memory address. This address is the location of another object (typically another variable) in memory. For example, if one variable contains the address of another variable, the first variable is said to point to the second. 


Pointer Variables

If a variable is going to be a pointer, it must be declared as such. A pointer declaration consists of a base type, an *, and the variable name. The general form for declaring a pointer variable is
type *name;
where type is the base type of the pointer and may be any valid type. The name of the pointer variable is specified by name.
The base type of the pointer defines the type of object to which the pointer will point. Technically, any type of pointer can point anywhere in memory. However, all pointer operations are done relative to the pointer's base type. For example, when you declare a pointer to be of type int *, the compiler assumes that any address that it holds points to an integer— whether it actually does or not. (That is, an int * pointer always ''thinks" that it points to an int object, no matter what that piece of memory actually contains.) Therefore, when you declare a pointer, you must make sure that its type is compatible with the type of object to which you want to point.

The Pointer Operators

There are two pointer operators: * and &. The & is a unary operator that returns the memory address of its operand. (Remember, a unary operator only requires one operand.) For example,
m = &count;
places into m the memory address of the variable count. This address is the computer's internal location of the variable. It has nothing to do with the value of count. You can think of & as returning "the address of." Therefore, the preceding assignment statement can be verbalized as "m receives the address of count."
Image result for pointer in cTo understand the above assignment better, assume that the variable count uses memory location 2000 to store its value. Also assume that count has a value of 100. Then, after the preceding assignment, m will have the value 2000.
The second pointer operator, *, is the complement of &. It is a unary operator that returns the value located at the address that follows. For example, if m contains the memory address of the variable count,
q = *m;
places the value of count into q. Thus, q will have the value 100 because 100 is stored at location 2000, which is the memory address that was stored in m. You can think of * as "at address." In this case, the preceding statement can be verbalized as "q receives the value at address m."

note:-These are the basic things that we have discussed today for more articles on pointer stay connected with us.

For any kind of doubts feel free to ask on contact us
     

0 comments:

Post a Comment

back to top