Home » » what is pointer in c

what is pointer in c

what is pointer in c

Pointers are the powerful feature of C language. It is easy and fun to learn. Actually, pointers are used in C program to access the memory and manipulate the memory address. A number of C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation. Dynamic memory allocation cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect C programmer.


Benefits of pointers in c

The usages of pointers will give the following benefits:

1. By using pointers we can access a data which is available outside a function.

2. By using pointer variable we can implement dynamic memory allocation.

3. Pointers provide a way to return more than one value to the functions.

4. Pointer reduces the storage space and complexity of the program.

5. Pointer reduces the execution time of the program and increases the execution speed of program.

6. Provides an alternate way to access array elements.

7. Pointers can be used to pass information back and forth between the calling function and called function.

8. A pointer allows us to perform dynamic memory allocation and de-allocation.

9. With the help of pointer we can build complex data structures such as linked list, stack, queues, trees, graphs etc.

10. A pointer allows us to resize the dynamically allocated memory block.


Disadvantages of pointers in C

The drawbacks of pointers in c are mentioned bellow:

1. Uninitialized pointers might cause segmentation fault.

2. Dynamically allocated block needs to be freed explicitly. Otherwise, it would lead to memory leak.

3. Pointers are slower than normal variables.

4. If pointers are updated with incorrect values, it might lead to memory corruption.


WHAT IS POINTER?

A pointer is a nothing but a variable that contains an address of another same type of variable. Since, a pointer is a variable; its value is stored in the memory in another location. Suppose, we have two variables such as value1 and value2 and we want to assign the address of value1 to variable value2. The link between the variables value1 and value2 can be visualized


ACCESSING THE ADDRESS OF A VARIABLE

In C language, the address of a variable can be known with the help of the operator ‘&’. The operator

‘&’ immediately prior a variable returns the address of the variable associated with it. For instance, the

statement: value2=&value1; would assign the address 80500(the location of variable value1. The ‘&’ operator can be said as ‘address of’. This operator can be used only with a simple variable or an array element.


DECLARING A POINTER

We know that a pointer is a variable that contains an address of another same type of variable. Since pointer variables contain addresses that belong to a separate data type, they must be declared as pointers before we use them. The general syntax or form of pointer declaration is as follows:


data-type * pointer_name;


Here, data-type is the valid C data type and pointer_name is the pointer variable name. The above declaration tells the compiler three things about the variable pointer_name :


i. The asterisk (*) tells that pointer_name is a pointer variable

ii. pointer_name needs a memory location

iii. pointer_name points to a variable of type data-type


Declaration Example

For example,


int *ptr;

declares the variable ptr as a pointer variable that points to an integer (i.e., int) data type. Likewise the

following statement,

float *ptr;


declares ptr as a pointer to floating point variable. Once a pointer has been declared, it can be made to

point to a variable using an assignment operator like:


int *ptr, value;

ptr = &value;


Where ptr to point variable value so pointer variable ptr contains the address of value. This process is

also called pointer initialization. Before a pointer is initialized, it should not be used.

Such valid pointer declaration example as follows:


int a,b,*ptr;

float x,*y;

ptr = &a;

y = &x;


Such invalid pointer declaration example as follows:


int a,b,*ptr;

float x,*y;

ptr = &x; //Error, type mismatch, because pointer variable always points to same type of data.

y = &a; //Error, type mismatch, because pointer variable always points to same type of data.



INITIALIZATION OF POINTER VARIABLE

In general, pointer initialization is the process of assigning address of a variable to pointer variable. Pointer variable contains address of variable of same data type. In C language address operator ‘&’ is used to determine the address of a variable. In C, it is possible to initialize and declaration together.


For example:


int p = 100 ;

int *ptr ; //pointer declaration

ptr = &p ; //pointer initialization


or,


int *ptr = &p ; //initialization and declaration together


Here, p is integer variable and ptr is pointer variable. So initializes ptr to the address of p. Here note

that this is an initialization of ptr, not *ptr and also remember that the target variable p is declared first.


ACCESSING A VARIABLE THROUGH ITS POINTER

When a pointer has been assigned the address of variable, in this situation the question is how to access the value of the variable using the pointer. Accessing a variable through its pointer we should follow three steps such as (a) at first we define a pointer variable (b) secondly, assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using indirection operator which is also known as unary operator (*), that returns the value of the variable located at the address specified by its operand. Consider the following statements:


int data, *p, x;

data = 1000;

p=&data;

x=*p;


In the above statements, the 1st line declares data and x as integer variables and p as a pointer variable that pointing to an integer. The 2nd line assigns the value1000 to variable data and then 3rd line assigns the address of data to the pointer variable p and then 4th line contains the indirection operator *. When the operator * is placed before a pointer variable in an expression, the pointer returns the value of the variable of which the pointer value is the address. In this situation, * p returns the value of the variable data, because p is the address of data. For this reason, the value of x would be 1000. In the above statements, the two lines like


p=&data;

x=*p;


are equivalent to x=*&data;

and also is equivalent to x=data;

0 comments:

Post a Comment

Office/Basic Computer Course

MS Word
MS Excel
MS PowerPoint
Bangla Typing, English Typing
Email and Internet

Duration: 2 months (4 days a week)
Sun+Mon+Tue+Wed

Course Fee: 4,500/-

Graphic Design Course

Adobe Photoshop
Adobe Illustrator

Duration: 3 months (2 days a week)
Fri+Sat

Course Fee: 9,000/-

Web Design Course

HTML 5
CSS 3

Duration: 3 months (2 days a week)
Fri+Sat

Course Fee: 8,500/-

Video Editing Course

Adobe Premiere Pro

Duration: 3 months (2 days a week)
Fri+Sat

Course Fee: 12,000/-

Digital Marketing Course

Facebook, YouTube, Instagram, SEO, Google Ads, Email Marketing

Duration: 3 months (2 days a week)
Fri+Sat

Course Fee: 15,000/-

Advanced Excel

VLOOKUP, HLOOKUP, Advanced Functions and many more...

Duration: 2 months (2 days a week)
Fri+Sat

Course Fee: 6,500/-

Class Time

Morning to Noon

1st Batch: 08:00-09:30 AM

2nd Batch: 09:30-11:00 AM

3rd Batch: 11:00-12:30 PM

4th Batch: 12:30-02:00 PM

Afternoon to Night

5th Batch: 04:00-05:30 PM

6th Batch: 05:30-07:00 PM

7th Batch: 07:00-08:30 PM

8th Batch: 08:30-10:00 PM

Contact:

Alamin Computer Training Center

796, West Kazipara Bus Stand,

West side of Metro Rail Pillar No. 288

Kazipara, Mirpur, Dhaka-1216

Mobile: 01785 474 006

Email: alamincomputer1216@gmail.com

Facebook: www.facebook.com/ac01785474006

Blog: alamincomputertc.blogspot.com

Contact form

Name

Email *

Message *