Learn. Spread. Learn.

Pointer in C and its implementation

Logo, C programming. We are looking pointers in c and its implementation

As we all know, C is the first language that many programmers learn during their career in programming. One of the reasons why C programming is different from many other high level programming languages is that, it give the access to memory to the programmer through pointers. For this reason, one should have a better understanding of pointer in C and its implementation. Although this may be true, pointers are among C’s most difficult capabilities to master.

An example without pointers

Let’s try to write a program to swap two integers without using pointers and check if that worked before understanding pointer in C and its implementation.

#include <stdio.h>
void swap (int x, int y);
int main() {
    int a, b;
    printf("Enter any two numbers: ");
    scanf("%d %d", &a, &b);
    swap(a, b);
    printf("The numbers after swapping are %d, %d\n", a, b);
    return 0;
}

void swap(int x, int y) {
    int temp;
    temp = x;
    x = y;
    y = temp;
}

Output:

Enter any two numbers: 8 16
The numbers after swapping are 8, 16

What Happened?

After compiling this program, we saw that we were not able to swap the numbers. That is because, in the swap function, we sent the values instead of the memory locations which keep the values. Therefore, those values of a and b allocate another space while we use them in swap function. Likewise, we created a new temp variable to swap the numbers inside the function only. As a result, the swap is not reflected in the main program, i.e. the swap occurs for new variables a and b, but not for the existing ones.

Nonetheless, we can pass the memory location instead of value. As a result, we can perform operations with the values stored in memory using pointers. For the purpose, we should know about pointer and its implementation.

For the implementation of pointers, we have to declare a pointer variable. The purpose of this variable is to reference a memory location. For example, we can define a pointer as follows:

int *countPtr, count;
countPtr = &count;

Here, we can see that, we defined a pointer variable countPtr and a normal variable count. We should keep in mind that, countPtr only takes memory addresses as its values. Therefore, we cannot set the value of countPtr manually, and perform arithmetic operations with another pointer. & is used before variable to get the memory location which we assigned it to the countPtr, i.e. it stores the memory address of count variable. Similarly, * is used to dereference the pointer variable, i.e. to get the value contained by the memory address. Now, the point is, we can send the memory location to anything and anywhere in the program to change the value of count.

Fixing above program

Likewise, in our swapping program above, we can send the memory location of the variables to swap instead of the values as follows

#include <stdio.h>
void swap (int *x, int *y);
int main() {
    int a, b;
    printf("Enter any two numbers: ");
    scanf("%d %d", &a, &b);
    swap(&a, &b);  // Send the location instead
    printf("The numbers after swapping are %d, %d\n", a, b);
    return 0;
}

void swap(int *x, int *y) {
    int temp;
    temp = *x; // get value by dereferencing the pointer variable
    *x = *y;
    *y = temp;
}

Output

Enter any two numbers: 8 16
The numbers after swapping are 16, 8

It is confusing to many people due to the asterisk symbol, however, you should only look at the variable, and understand what kind of value it can store. As we can see above, we changed the type of parameters to pointer variable of integer. Therefore, from this point onward, those variables inside the swap function hold the memory location instead of actual values.

So, we sent the address of a andb (using &) in swap function. Inside the function, we get the values by using dereference operator *, and perform the swapping operation. As a result, we were able to swap the variables, even after exiting the function.

In a nutshell, we knew the importance of using pointer with a simple example up to this point. I will update you about more advance concept of pointers in next posts.

There is an example of a simple web app in GoLang, where I have used pointer in GoLang which you can check out with this link here: https://nepcodex.com/blog/2019/07/create-a-simple-web-app-in-golang/

Also, it is difficult to combine react, redux and router together, however, you can find a tutorial and link to the github from this link here (it uses newer react, redux, router versions): https://nepcodex.com/blog/2019/07/a-guide-to-react-redux-router-latest-version/

Similarly, you can clone the github repo from here (it uses newer react, redux, router): https://github.com/kriss-u/react-redux-router

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments