Pointers in C Programming [Complete Guide]

pointers in c programming

Pointer is one of the most important concepts in C Programming and it is very difficult to master in Pointers.

Pointers are just like variables. We can declare a pointer of any data type (int, char, float, double) just like we declare variables.

If we declare a variable of int, then that variable can contain only integer value or if we declare a variable of char then that variable can contain the only character in it.

The same concept is here with just one difference.

What are Pointers?

Pointers are variables whose values are memory addresses. Pointers only point to variables.

We can declare a pointer of any data type, if we declare a pointer of integer then that pointer can contain the address of an integer variable or if we declare a pointer of character then that variable can contain the address of any character variable.

In short, variables can contain the values directly and pointers can contain the addresses of that variables.

Declaration of a Pointer:

If we declare a variable and put an asterisk before variable name then that variable becomes a pointer just like that
int *numberPtr;
Here, numberPtr is a pointer of int and can contain the address of an integer variable in it.

How to assign the address to Pointer?

Let, say we have an integer variable xyz and have value 7 in it.
int xyz = 7;
We can assign the address of this integer variable to the pointer we have declared first by using address operator (&). Just like that
numberPtr = &xyz;
Now the pointer numberPtr have the address of variable xyz in it or pointing to variable xyz.

You can understand by this
pointer pointing to the variable xyz
The pointer numberPtr is now pointing to the variable xyz.

Dereferencing:

If we print the value of pointer numberPtr just as we print values of variables then this would not print the exact result you expect because the numberPtr does not have the value you want to print. It has the address of the variable xyz.

For example, if you print like
printf("Value of numberPtr: %d ",numberPtr);
then this would not print the value 7 because numberPtr has the address of xyz.

In my computer, it is printing 6356744.

If you want to print the exact value of the variable to which the pointer is pointing then you have to dereference it by using the dereferencing operator (*) in printing. Just like that
printf("Value of numberPtr: %d ",*numberPtr);

This will print the exact value of variable xyz which is 7.

If the pointer is not initialized correctly with some address then the dereferencing will lead to error.

13 comments:



  1. 2 of 65
    comment

    Tuhin Pabna
    Attachments
    Nov 28, 2019, 9:29 AM
    to noyon0461


    2 Attachments

    keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
    digital marketing training in bangalore / https://www.excelr.com/digital-marketing-training-in-bangalore

    ReplyDelete
  2. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!
    Data Science Certification in Bangalore

    ReplyDelete
  3. I feel very grateful that i read this.It is very helpful and very informative and i really learned a lot from it.
    learn360digitmg artificial intelligence course

    ReplyDelete
  4. I am impressed by the information that you have on this blog. It shows how well you understand this subject.
    Data Science Institute in Bangalore

    ReplyDelete
  5. I'm happy to see the considerable subtle element here!.
    360digitmg ai online course

    ReplyDelete
  6. Nice knowledge gaining article. This post is really the best on this valuable topic.
    360digitmg business analytics online course

    ReplyDelete
  7. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
    360digiTMG data science training in guduvanchery

    ReplyDelete
  8. There are many custom finance assignment writing services and Finance Coursework Writing Services to choose from for those stuck with their finance case study writing services and finance research paper help services.

    ReplyDelete
  9. Glad to chat your blog, I seem to be forward to more reliable articles and I think we all wish to thank so many good articles, blog to share with us.
    360digitmg data science courses

    ReplyDelete
  10. Nice post found to be very impressive to come across such an awesome content along with a unique content. Lots of appreciation to the blogger who took an initiative to write this particular blog. Thanks for sharing and keep posting such an informative content.

    360DigiTMG Cyber Security Course

    ReplyDelete
  11. Great post found to be very impressive while going through the blog. I really enthusiased while reading and enjoyed every bit of your content. Certainly, since this blog is being more informative it ia an added advantage for the users who are going through this blog. Once again nice blog keep it up.

    360DigiTMG IoT Course

    ReplyDelete