Let’s dive into the world of coding, shall we?
Here we are now… This is my very first trial to dive into the world of coding in this blog. Even though I’ve been working as a Java developer for the past three years, I never thought about sharing my knowledge through my writing. But here, by outputting what I’ve learned also far on my blog just as my diary keeps tracking my personal progress, not only is it beneficial for readers like you but also is beneficial for me by explaining the very basic concepts of coding language’s mechanisms. So, let’s dive into the world of coding, shall we?
As I mentioned in my previous post, I’ve been learning C language for the past few weeks along with other skills, such as Linux commands and its basics. The reason I’m learning the language is simply for my eternal curiosity to understand the Linux operating system and its kernel – its deepest core mechanisms. I bought a book about Linux programming almost five years ago and haven’t even started reading it, mainly because my entire focus has been on Java, the language I’ve been required to master for my professional career.
However, my relentless curiosity about Linux has never waned over the past couple of years, and I could no longer resist my inner desire to start reading the book and understanding the Linux kernel and the open-source ecosystem from scratch. Linux played a significant role in my starting my career in the tech field several years ago, and this time, I’m ready to start something a little extraordinary.
To do so, mastering C language is a must. Since the legacy language is responsible for not only our major OSes, including Windows, macOS, Linux, iOS, and Android, it plays an integral part in IoT (Internet of Things). If you ever programmed a robotic mechanism on a platform, such as Arduino, probably you can understand what I mean.
And I want to share my C-language journey with you. The reason is simple: it’s all about output. Every time you learn something new, you may create your personal manuals by using Microsoft Word or Excel. I do that too. But isn’t it even amazing if I could share my knowledge with you? To explain something to someone who has no idea about it, I need to understand what I’m doing, and my explanation must be logically coherent. So, this journey is also my personal challenge to explore the world of C language and Linux.
To begin with, let’s dive into the world of C. For a starter, we’ll learn about address and pointer. I know, I know – everyone says pointer is a difficult concept to grasp. But as far as I knew after having learned its basic concept, it’s not that difficult (I guess…).
Without further adieu, let’s get into it, shall we?
Address
To understand the C language’s basics, you need to know about addresses. So, address is literally variables’ addresses in the computer’s memory. And it frequently changes every time you run the program. Let’s look at sample_ex2-1.c. What it does is print out all of those variables’ values, sizes, and addresses from lines 8 to 11.
sample_ex2-1.c
#include <stdio.h>
void main() {
int a = 100; // int num
double b = 123.4; // double num
float c = 123.4f; // float num
char d = 'a'; // char string
printf("%d %lu %p\n", a, sizeof(int), &a);
printf("%f %lu %p\n", b, sizeof(double), &b);
printf("%f %lu %p\n", c, sizeof(float), &c);
printf("%c %lu %p\n", d, sizeof(char), &d);
}
And this is the output result of sample_ex2-1.c. Can you see those outputted values, sizes, and addresses of the aforementioned variables?
And here, I run the program for the second time, and you can see that the addresses are now changed. So this is the concept of address. It’s just a variable’s temporary address on memory that changes frequently every time you run the program.
Pointer basics
From here, let’s take a closer look at the pointer. In sample_ex2-2.c, there are two int-type variables ‘a’ and ‘b’, And what’s most important is the third int-type pointer variable ‘*p’. The asterisk * is the sign that it is a pointer variable. So what exactly is a pointer? Simply put, it’s a special variable that allows you to substitute another variable’s value. And pointer literally pin-points the address of the variables.
sample_ex2-2.c
#include <stdio.h>
void show(int, int, int);
void main() {
int a = 100; //int num
int b = 200; //int num
int *p = NULL; //int pointer num
p = &a; // substitute a to p
show(a, b, *p);
*p = 300; // substitute 300 to p -> both a and p will be 300
show(a, b, *p);
p = &b; // substitute b to p -> both b and p will be 200, a will remain 300
show(a, b, *p);
*p = 400; // substitute 400 to p -> both b and p will be 400, a will remain 300
show(a, b, *p);
}
void show(int n1, int n2, int n3) {
printf("a = %d b = %d *p = %d\n", n1, n2, n3);
}
And the output result is image 3.
Let me explain what’s going on here. In line 9 of sample_ex2-2.c (p = &a;), the variable a is substituted to the pointer variable p, so p’s value became a’s value, which is 100.
The output result: a = 100 b = 200 *p = 100
In line 11 of sample_ex2-2.c (*p = 300;), the number 300 is substituted to the pointer variable p, so p’s value became a’s value, which is 300. And what’s interesting here is the fact that at this point a equals p, right? Because in line 9, we’ve substituted the variable a for the pointer variable p. Take a look at the result – not only did p become 300, but also a became the same number.
The output result: a = 300 b = 200 *p = 300
In line 13 of sample_ex2-2.c (*p = 300;), the variable b is substituted to the pointer variable p, so p’s value became b’s value, which is 200. And as you may expect, both b and p became 200.
The output result: a = 300 b = 200 *p = 200
Finally, in line 15, the number 400 is substituted to the pointer variable p, so both b and p became 400.
The output result: a = 300 b = 400 *p = 400
Interesting, isn’t it? So, what we’ve learned here is that pointer is basically a special type of variable that can disguise as other variables. Is this a difficult concept to grasp? I don’t think so… What do you think?
Pointer: actual usage
Here’s another source code. In sample_ex2-3.c, there are two int-type variables: a and b, and what we going to do is to swap the values of the two. In line 8 – swap(&a, &b) -, the variables a and b are transferred to the method swap.
sample_ex2-3.c
#include <stdio.h>
void swap(int*, int*);
void main() {
int a = 1, b = 2;
printf("a = %d b = %d\n", a, b);
swap(&a, &b);
printf("a = %d b = %d\n", a, b);
}
void swap(int* num1, int* num2) {
int temp = *num1;
*num1 = *num2;
*num2 = temp;
}
Below is the output result. As you can see the values of the two variables are swapped.
Take a look at line 13, num1 is temporality substituted for the variable temp.
line 13: int temp = *num1;
In line 14, num2 is substituted for the variable num1.
line 14: *num1 = *num2;
In line 15, temp is substituted for the variable num2.
line 15: *num2 = temp;
Finally, in line 9, when we run the program, the swapped result will be printed out in the console.
line 9: printf(“a = %d b = %d\n”, a, b);
What’s interesting here is that the method swap is a void-type method and doesn’t return anything. So why are we able to refer to the swapped variables? It’s simply because it handles the pointer-type variables. As I mentioned above, pointer points out the addresses of variables, so whatever type the method is, you can always refer to the pointers no matter what.
Pointer: dont’s
In the below code sample_ex2-4.c, we’re handling an int-type pointer variable p where the number 1 is substituted in line 7. As I said, pointer is a variable that handles other variables’ addresses, you can’t substitute a value for it.
line 7: *p = 1;
sample_ex2-4.c
#include <stdio.h>
void main() {
int *p = NULL;
*p = 1;
}
The output result is below. yes, it failed… The problem here is that you can compile it without any errors, but it doesn’t work when running the program.
Afterthoughts
So, how was it? Pointer is usually considered something hard to understand, but when we take a closer look at it, it’s not that hard, isn’t it? This is only the beginning of the C-language journey, so hopefully, we can enjoy it while learning the core concept of the language. At the same time, I’ve been reading the book I bought five years ago, and it’s all about Linux programming with C language. So, along with the C’s pointer concept, I’ll keep posting about Linux programming as well.
Keep coding, learning, and growing…
See you soon!!