Pointers are one among the foremost confusing/challenging parts of C, in my opinion.Especially if you are new to programming, but also if you come from a higher
level programming language like Python or JavaScript.A pointer may be a variable that contains the address of a variable.

Pointers are much used in C, partly because they are sometimes the only way to express a computation, and partly because they usually cause more compact and efficient code than are often obtained in other ways.

Pointers and arrays are closely related; this chapter also explores this relationship and shows how to exploit it.

Pointers are lumped with the goto statement as a wonderful thanks to create impossibleto-understand programs.This is certainly true when they are used carelessly, and it is easy to create pointers that point somewhere unexpected.

With discipline, however, pointers can also be used to achieve clarity and simplicity. This is the aspect that we’ll attempt to illustrate.

The main change in ANSI C is to form explicit the principles about how pointers are often
manipulated, in effect mandating what good programmers already practice and good
compilers already enforce.

In addition, the type void * (pointer to void) replaces char * as the proper type for a generic pointer.A pointer is the address of a block of memory that contains a variable.
When you declare an integer number like this:

int age = 37;

We can use the & operator to get the value of the address in memory of a variable:

printf("%p", &age);

I used the %p format specified in printf() to print the address value.
We can assign the address to a variable:

int *address = &age;

Using int *address in the declaration, we are not declaring an integer variable, but rather a pointer to an integer.We can use the pointer operator * to get the value of the variable an address is pointing to:

int age = 37;
int *address = &age;
printf("%u", *address);

This time we are using the pointer operator again, but since it’s not a declaration this time it means “the value of the variable this pointer points to”. we declare an age variable, and we use a pointer to initialize the value:

int age;
int *address = &age;
*address = 37;
printf("%u", *address);

Pointers are a great opportunity because they force us to think about memory
addresses and how data is organized.Arrays are one example. When you declare an array:

int prices[3] = { 5, 4, 3 };

The prices variable is actually a pointer to the first item of the array. You can get the value of the first item using this printf() function in this case:

printf("%u", *prices);

The cool thing is that we can get the second item by adding 1 to the prices pointer:

printf("%u", *(prices + 1));

And so on for all the other values.We can also do many nice string manipulation operations, since strings are arrays under the hood.

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Pointers In C you should know

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Explore Tutorials By Categories

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password