EmbLogic's Blog

POINTERS IN C LANGUAGE

POINTERS IN C LANGUAGE

C is the most powerful programming language in the software development world. You can do whatever you want to do regarding development then go through the C language. You can’t even imagine the depth of C language. And the most powerful tool of the C is Pointer. If you are master in the Pointer you can be the master of C language.

So, now your curiosity is some questions arising in your mind, like:

  1. Why Pointer are that much powerful tool?, When we can use the Pointers in our program?

  2. How Pointer works?

  3. How Pointers can increase the efficiency and decrease the length of our program?

 Here are the answers of these question.

Definition of Pointers-: The simple definition of Pointer is the variable which holds the address in memory of some other variable. You can understand this like a arrow pointing towards your home and on the arrow your address is print means the arrow holding the address of your home, and your home is the variable. This Lehman definition will help you to understand what is pointer.

 The declaration of Pointer is as follows:

                   int *pointer;

Here’s the pointer is a variable which is integer type, and * sign is actually an operator to DE-reference a pointer. The only time it means “hey I’m a pointer” is during variable declaration.

And how it holds the address:

                int *pointer;

                int a;

             pointer = &a;

Here, & ampersand sign specify the address of the variable.

In the above expression, variable a is the integer type and pointer is holding the address of a with the help of & sign. The data type int shows, 4 bytes of data the variable stored in.

Pointers in Array-:

Arrays are continuous block of memory holds multiple objects and the type of objects are specified. An array variable is constant. You can’t assign a pointer to an array variable, even if the pointer variable actually points to the same or a different array. You also cannot assign one array variable to another. You can assign an array variable to a pointer though and that is where things get confusing. When assigning the array to the pointer we are actually assigning the address of the first element in the array to the pointer.

 int a[4] = {1,2,3};

int *pointer = a;

printf(“*pointer=%d\n”, *pointer);

 Firstly we initialize the array of integer type. After this we initialize the integer pointer and assign the array variable to it. Since the array variable actually is the memory address of the first element in the array, we have assigned the memory address of the first element in the array to the pointer. This is the same as doing int *pointer = &a[0], explicitly stating the address-of the first element in the array.

 Notice the pointer has to be the same type as the elements of the array, unless the pointer is a void pointer.

Pointers to structure-:

A pointer to a structure holds the memory address of the first memory of structures. And the pointers to structure must be declared to point to the structure type or be void type.

struct person {

int age;

   char *name;

};

struct person first;

struct person *ptr;

 first.age = 21;

char *fullname = “full name”;

first.name = fullname;

ptr = &first;

 printf(“age=%d, name=%s\n”, first.age, ptr->name);

 On the first 6 lines we declare the struct person, a variable to hold a person struct, and a pointer to a person struct. Line 8 we assign a literal int to the age member. Line 9-10 we declare a char pointer to a literal char array and then assign that to the struct name member. Line 11 we assign a reference to the first person struct to our struct pointer variable.

Line 13 we print out the age and name of our struct instance. Notice the two different notations, the . and the ->. With the age field we are accessing the struct instance directly and so we use the . notation. With the name field we are using our pointer to the struct instance and so we use the -> notation. This would be the same as doing (*ptr).name where we first derefence the pointer and then access the name field.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>