Showing posts with label pointers. Show all posts
Showing posts with label pointers. Show all posts

Jun 13, 2020

Pointers in C and C++ | Coding Winds | We care about the future of future

                                                    POINTERS

Hey people in this blog you are going to read about “Pointers”. 

What are pointers? Well, the given definition is, “pointer is a variable whose value is the address of another variable, or pointers are said to ‘point to’ a variable whose address they store.” It is basically used for passing the required value by pass by reference method.

Well, we use pointers in C and C++. Why not Java or python?

Pointers reduce the data security in python.

In java, no pointer support make Java more secure because they point to memory location that loses the security as we use them directly.

So we will first see pointers in C

Pointers in C

The general form of pointer declaration is,

                                type *var_name;

How to use pointers?

1)    1. Declare a variable

2)    2. Declare a pointer variable

3)    3. Point the address of the variable to the pointer variable

W
                                 We would suggest you to type this code and run on your own in your IDE.

NULL pointer concept

It is said that assigning the pointer variable to NULL is a good practice in cases you don’t have an exact address.

                                  Check out the output to understand it more clear.

Programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. But the memory address 0 gesticulates that the pointer is not intended to point to an accessible memory location.

A simple application of NULL pointer you can see here,

In this if pointer is NULL the pointer will have value as 0 and if the pointer is not NULL then it will have some non zero value. Here, "if()" will not work in first case and will work in the second one.


Pointers in C++


C++ pointers are easy and fun to learn. Some C++ tasks are performed more easily with pointers, and other C++ tasks, like dynamic memory allocation, cannot be performed without them.

Look at the code below and consider the output on your own for better understanding.



The notation is same as of C,

                                          type *var_name;

Using pointers in C++

The procedure is same as of C.

Look the code below,


               We would suggest you to type this code and run on your own in your IDE.

The concept of NULL pointer in C++ is also the same as of C,




Note:- We actually have just gave you a brief idea about pointers only. Pointers has many other application which will be available soon.


Hope all your doubts regarding this are clear now.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via instagram "@coding.winds".


We also have an article on space complexity and time complexity , do give them a read.