Answered my question from yesterday on the default value in a declared pointer that is never assigned an address of another variable to store as it’s value. Turns out it just gets a garbage value. It’s weird how that works but I’m barely keeping up with all this so I will just have to accept it and not dig any deeper for now.
TLDR;
Okay, so here are the highlights of what I did:
- Continued work on the “Technical Questions” section of the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. Continued my study of Linked Lists. I am trying to improve on my first example while noting what I am changing and how the improvements help improve the quality of my code. I figured out how declaring pointers works and their default values. To clear up my confusion I will try to adopt the supposed best practice of assigning pointers with a NULL pointer value to start so that it never holds a garbage value. I got a long way to go but at least I understand this small part now… I hope.
Notes on Pointers in C
Pointers
Pointers are a type of data that works with the memory address of variables. Just like with any other data type we can store them in a variable. The difference is that pointers reference existing variables. We cannot make up an address in memory (I think…).
The syntax is also weird so here are some examples:
int age = 30;
int *pAge = &age;
double gpa = 3.4;
double* pGpa = &gpa;
char grade = 'A';
char * pGrade = &grade;
We will often see them without the space between the “*” and the pointer variable name. For example: char pAge = &age
. Ultimately there is no difference in functionality but the *pVar
syntax with no spaces is preferred most of the time. Here is a great question/answer on the subject from Stack Overflow.
Declaring and Initializing Pointers
When declaring or initializing a pointer there are a few rules that we must be aware of.
- The data type of the pointer and the variable to which the pointer variable is pointing must be the same.
- The pointer variable is a variable and therefore has an address itself in the memory heap where it stores it’s value.
- Pointer initialization is the process of assigning an address of a variable to a pointer variable. It must contain the address of a variable with the same data type.
- If we declare a pointer variable and do not assign it any value it will contain a garbage value by default. Therefore it is recommended to assign a value of
NULL
to it.
#include <stdio.h>
int main()
{
int a;
a = 10;
int *p = &a; // declaring and initializing the pointer
//prints the value of 'a'
printf("%d\n", *p);
printf("%d\n", *&a);
//prints the address of 'a'
printf("%u\n", &a);
printf("%u\n", p);
printf("%u\n", &p); //prints address of 'p'
return 0;
}
NULL Pointers
A “Null pointer” is a special reserved value of a pointer. A pointer of any type has this reserved value. Formally, each specific pointer type (int *
, char *
, etc) has its own dedicated null-pointer value. Conceptually, when a pointer has that Null value it is not pointing anywhere.
A “Void pointer” is a specific pointer type. void *
which is a pointer that points to some data location in storage, which doesn’t have any specific type.
A Null pointer is a value whereas, a Void pointer is a type.
Below is a program on NULL pointer:
#include <stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int *ptr = NULL; // ptr is a NULL pointer
printf("\n\n The value of ptr is: %x ", ptr);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Conclusion
That’s all for today. This is my sixth round of the “#100daysofcode” challenge. I will be continuing my work from round five into round six. I am currently working through the book “Cracking the Coding Interview” by Gayle Laakmann McDowell. My goal is to become more familiar with algorithms and data structures. This goal was derived from my goal to better understand operating systems and key programs that I use in the terminal regularly e.g. Git. This goal was in term derived from my desire to better understand the fundamental tools used for coding outside of popular GUIs. This in turn was derived from my desire to be a better back-end developer.
I have no idea if my path is correct but I am walking down this road anyways. Worst case scenario I learn a whole bunch of stuff that will help me out on my own personal projects.