DEV Community

Jasper Oh/YJ Oh
Jasper Oh/YJ Oh

Posted on

C - Pointer & Struct

🔥 This post will discuss about pointer which is the one of the most important concept in C language to actually implement a LinkedList.

So, what is the pointer? what is the definition? (I captured below paragraph from KNK C programming)

Although addresses are represented by numbers, their range of values may differ from that of integers, so we can't necessarily store them in ordinary integer variables. We can, however, store the in special p, we say that p "points to" i. In other words, a pointer is nothing more than an address, and a pointer variable is just a variable that can store an address. KNK C programming P.242

We can understand the concept with the Excel (Google Spreadsheet).

Image description

According to above image,

  1. Cell B6, I put value 15.
  2. Cell D9, I put a cell-address with =B6.

You can understand with the code below,

int num = 15; // 1. Cell B6, I put value 15.
int *numPtr = # // 2. Cell D9, I put a cell-address with =B6.
Enter fullscreen mode Exit fullscreen mode

Of course, We should think like computer memory, not a cell in excel. B6 and D9 (address value) is almost randomly assigned by compiler.

So, the valuable named "num" is allocated the address of memory. (the size is 4 byte b/c int type)

✅ Just sec in here, I think we should recap the structure of memory.

Memory Structure in C

When we execute a program, OS allocate the memory space for the program. And this allocated memory space is divided by

  1. Data Area
  2. Stack Area
  3. Heap Area

And I'll deal with this memory area soon.

Before that, I'll put more detailed code under about pointer.

Example 1

int main(){
 int x; // 1. declare x -> int variable
 int *ptr_x; // 2. declare pointer var ptr_x, which means this variable can save the memory address and read only 4 bytes because this is int pointer var
 x = 100; // 3. init x with 100
 ptr_x = &x; // 4. save x's memory address in ptr_x
 printf("x : %d, &x : %p\n", x , &x); // 5.using %d to print a decimal format, and %p to pointer(address) format and & operator showing the memory address of x
 printf("*ptr_x: %d, ptr_x: %p\n",*ptr_x, ptr_x); // 6. if we put * operator in pointer var then, It shows the variable that saved in address
 *ptr_x = 200; // 7. change the value to 200 that ptr_x indicate
 printf("x: %d, *ptr_x: %d\n" , x, *ptr_x);
 return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas

A better analogy is a set of Post Office boxes where each has a single number (like a real memory address) rather than a spreadsheet cell that has a row & column (that has no analog in memory). In a box, you can put an index card. On said card, you can write the number of another box.

Address values are not "randomly assigned by the [the] compiler." They're assigned by the OS. The compiler has nothing to do with it.

It's not clear how you can write something describing pointers and never once mention null pointers.

Collapse
 
rjsgml profile image
David

Very informative, Jasper!