Wild pointers

Even though a wild pointer is a straightforward idea, you still needed a separate tutorial to understand it. Let’s begin with the definition, then.

“Void pointers are uninitialized pointers.”

Example:

int *ptr;

In the example above, a pointer was generated but left empty, turning it into a wild pointer.

Its drawback is that it will hold any garbage value, which means it will hold any random portion of memory. Numerous faults in the software may result from the storage of data in an arbitrary location, and occasionally the programmer will not even be able to determine the source.

Solution: We would rather convert a void pointer to a NULL pointer in order to prevent the flaws and errors it may introduce into a programme. As a result, our pointer will always point to 0 or NULL instead of any memory location. Simply setting a wild pointer equal to NULL will make it a NULL pointer. Let’s look at it using C syntax.

Syntax:

int *ptr = NULL;

Therefore, if we are not utilising our pointer to point at a specific memory region, we will use this way.The pointer can now be initialised as a second option for preventing similar issues.

Example:

int x = 3;
int *ptr;
ptr = &3;

It will be a wild pointer if we simply run the first two lines of the aforementioned section of code because no value has been initialised to it. However, if we run the third line as well, it will point to a specific address and become a normal pointer.

Dereferencing:

We are unable to dereference a wild pointer because we are unsure of the data that it is pointing to in memory. Dereferencing a wild pointer can result in several problems as well as programme failure.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a =4354;
    int *ptr; // This is a wild pointer
    // *ptr = 34; // This is not a good thing to do
    ptr = &a; // ptr is no longer a wild pointer
    printf("The value of a is %d\n", *ptr);
    return 0;
}
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in