<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Comfort Ajala</title>
    <description>The latest articles on DEV Community by Comfort Ajala (@comfortajalaoluwatimilehin).</description>
    <link>https://dev.to/comfortajalaoluwatimilehin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F52081%2F0b8a9584-e888-4b50-b850-9d493a80380d.png</url>
      <title>DEV Community: Comfort Ajala</title>
      <link>https://dev.to/comfortajalaoluwatimilehin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/comfortajalaoluwatimilehin"/>
    <language>en</language>
    <item>
      <title>I am learning C in 2020 – Beginner - What I learned about memory, variables and pointers in c</title>
      <dc:creator>Comfort Ajala</dc:creator>
      <pubDate>Wed, 06 May 2020 07:05:00 +0000</pubDate>
      <link>https://dev.to/comfortajalaoluwatimilehin/i-am-learning-c-in-2020-beginner-what-i-learned-about-memory-variables-and-pointers-in-c-51fj</link>
      <guid>https://dev.to/comfortajalaoluwatimilehin/i-am-learning-c-in-2020-beginner-what-i-learned-about-memory-variables-and-pointers-in-c-51fj</guid>
      <description>&lt;p&gt;Original Post : &lt;a href="https://ajalacomfort.com/i-am-learning-c-in-2020-beginner-what-i-learned-about-memory-variables-and-pointers-in-c/"&gt;HERE&lt;/a&gt;&lt;br&gt;
Book: &lt;strong&gt;Head First C&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Chapter: &lt;strong&gt;3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Platform used: &lt;a href="https://cplayground.com"&gt;cplayground.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;&lt;strong&gt;Pointers humbled me&lt;/strong&gt;&lt;/h2&gt;

&lt;h2&gt;Problem&lt;/h2&gt;

&lt;h3&gt;&lt;strong&gt;&lt;span&gt;Goal&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;

&lt;p&gt;Understand pointers at a foundational level.&lt;/p&gt;

&lt;h2&gt;Memory Zones in C&lt;/h2&gt;

&lt;p&gt;I figured it would be useful to gain a simple overview of where variables could live during the run of a c program.&lt;/p&gt;

&lt;h3&gt;Stack&lt;/h3&gt;

&lt;p&gt;Variables declared or/and initialised within a function are said to live the stack memory. Note that once the function is called and completes its task, all the local variables cease to exist in the stack memory. Based on several articles I have read and listed below, each local variable ( within the scope of a function) is pushed into the stack memory. Although the stack variables are pushed into the stack memory ( downward growth of stack or in a last-in-first-out manner ), they are removed via a POP function. This denotes the organized process in stack variable management.&lt;/p&gt;

&lt;h3&gt;Heap&lt;/h3&gt;

&lt;p&gt;Unlike stack variables, heap variables are allocated in a more disorganized manner. Heap variables are created dynamically by using the inbuilt &lt;em&gt;malloc &lt;/em&gt;function and removed or their memory allocation freed by the &lt;em&gt;free &lt;/em&gt;function. To access heap variables, pointers are used.A really cool thing about heap variables is that since they are dynamically allocated, they can be accessed outside the context of a function using their pointers.&lt;/p&gt;

&lt;h3&gt;Static&lt;/h3&gt;

&lt;p&gt;These are variables that retain their value even after the function is complete. Additionally after the first initialisation of a declared static variable, subsequent initialisations in the same scope/ function are ignored. Keep in mind that uninitialized static variables are set to 0; The code snippet below demonstrates this:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;


void foo(){
    
    static int num = 5; // initialized 
    printf("The value of the static local NUM in foo() is %d\n", num);
    num++;
}

void doo(){
    int num = 5; // initialized 
    printf("The value of the local NUM in doo() is %d\n", num);
    num++;
}

void moo(){
    static int num; // uninitialized 
    printf("The value of the static local NUM in moo() is %d\n", num);
    num++;
}
int main() {
        
        for(int i; i &amp;lt; 5; i++){
            foo();
            doo();
            moo();
printf("...................................................\n\n");
        }
    return 0;

}&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Run…&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="prettyprint"&gt;The value of the static local NUM in foo() is 5
The value of the local NUM in doo() is 5
The value of the static local NUM in moo() is 0
...................................................

The value of the static local NUM in foo() is 6
The value of the local NUM in doo() is 5
The value of the static local NUM in moo() is 1
...................................................

The value of the static local NUM in foo() is 7
The value of the local NUM in doo() is 5
The value of the static local NUM in moo() is 2
...................................................

The value of the static local NUM in foo() is 8
The value of the local NUM in doo() is 5
The value of the static local NUM in moo() is 3
...................................................

The value of the static local NUM in foo() is 9
The value of the local NUM in doo() is 5
The value of the static local NUM in moo() is 4
...................................................&lt;/pre&gt;

&lt;p&gt;However static and global variables neither live in the stack nor in the heap memory. They are said to be stored in another area.&lt;/p&gt;

&lt;h3&gt;Text Segment&lt;/h3&gt;

&lt;p&gt;Also known as the code segment, is the portion of a program where the executables or bytecode instructions of the program lives. Constants also reside in this memory.&lt;/p&gt;

&lt;h3&gt;Data Segment&lt;/h3&gt;

&lt;h4&gt;Initialized data segment&lt;/h4&gt;

&lt;p&gt;Here exist all the global and static variables ( global and local statics) of a program. This segment is not read-only, meaning variables stored here can be re-initialized or assigned new values.&lt;/p&gt;

&lt;h4&gt;Uninitialized data segment&lt;/h4&gt;

&lt;p&gt;Also called the bss segment, holds variables that were not given values by the programmer and consequently all set to zero.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;To be frank, I can't say I fully grasp these concepts as I am new to these, but hopefully it helps you a bit.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;Exercise&lt;/h3&gt;

&lt;p&gt;Where are the following variables stored ?&lt;/p&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
int globalInt;
int intializedInt = 23;
static int initializedStaticInt = 11;
int main() {
   static int staticInt = 5;
    int unIntializedInt;
    int val  = 3;
    char name[] = "Comfort Ajala";
    char list[32];
    char initList[32] =  {0}; 
    char * allocatedforchar = (char * ) malloc(20* sizeof(char));
    char *lastname = "Ajala";
    return 0;

}&lt;/pre&gt;

&lt;h3&gt;My Solution&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;globalInt → &lt;/strong&gt;an uninitialized global variable → &lt;span&gt;BSS&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;intializedInt&lt;/strong&gt; → initialized global variable → &lt;span&gt;Initialized data segment&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;initializedStaticInt &lt;/strong&gt;→ initialized global static variable → &lt;span&gt;Initialized data segment&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;staticInt &lt;/strong&gt;→ initialized static local variable → &lt;span&gt;Initialized data segment&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;unIntializedInt &lt;/strong&gt;→ uninitialized local variable → &lt;span&gt;stack&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“Only variables with static storage duration end up in .bss and .data. Local variables always end up on the stack, or in CPU registers, no matter if they are initialized or not.” - &lt;a href="https://stackoverflow.com/a/47301825/5386933"&gt;Lundin&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Val &lt;/strong&gt;→ initialized local variable → &lt;span&gt;stack&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Name &lt;/strong&gt;→ initialized local variable → &lt;span&gt;stack&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;List &lt;/strong&gt;→ uninitialized local variable ( static array ) → &lt;span&gt;stack&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;initList &lt;/strong&gt;→ initialized local variable → &lt;span&gt;stack&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Allocatedforchar &lt;/strong&gt;→ initialized local variable ( dynamic array ) → &lt;span&gt;heap&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lastname &lt;/strong&gt;→ initialized local variable → &lt;span&gt;stack&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The string literal does not live in the stack though, it is stored in read-only memory. Read &lt;a href="https://stackoverflow.com/questions/12795850/string-literals-pointer-vs-char-array"&gt;here &lt;/a&gt;for more.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;Variable&lt;/h2&gt;

&lt;p&gt;Which can be of type integer ( int), float, char etc stores of a value of the designated type at a specific address.&lt;/p&gt;

&lt;h3&gt;Memory address&lt;/h3&gt;

&lt;p&gt;According to this &lt;a href="http://www.cs.ecu.edu/karl/3300/spr14/Notes/C/Memory/memory.html"&gt;article&lt;/a&gt;, memory is a place where variables are stored while a program is running. I am curious about what happens when the program stops. Does the memory disappear ?&lt;/p&gt;

&lt;p&gt;The machine memory is apparently a list of bytes, where each byte,a collection of 8 bits, is a unique address. However depending on the type of data stored, a single byte or a group of bytes may be used.This means, the number of bytes used to store an integer or a long value may differ from that used to store a character. It is also important to keep in mind that the size or number of bytes for storage may also vary based on operating systems/ platforms. The address of a given variable, which is also referred to as a &lt;strong&gt;pointer&lt;/strong&gt;, can be accessed by using &amp;amp;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Variable type&lt;/td&gt;
&lt;td&gt;Size(byte)&lt;/td&gt;
&lt;td&gt;Size (bit)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;int&lt;/td&gt;
&lt;td&gt;2 or 4 bytes&lt;/td&gt;
&lt;td&gt;16 or 32 bits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;long&lt;/td&gt;
&lt;td&gt;8 bytes&lt;/td&gt;
&lt;td&gt;64 bits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;char&lt;/td&gt;
&lt;td&gt;1 byte&lt;/td&gt;
&lt;td&gt;8 bits&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;Example…&lt;/h3&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int main() {
    size_t sizeofachar = 10 * sizeof(char);
    printf("size of char %li\n How many bytes will be allocated for 10 characters = %li\n", sizeof(char), sizeofachar);
    
    
     size_t sizeofint = 10 * sizeof(int);
    printf("size of int %li\n How many bytes will be allocated for 10 integers  = %li\n", sizeof(int), sizeofint);
    
    
     size_t sizeoffloat = 10 * sizeof(float);
    printf("size of float %li\n How many bytes will be allocated for 10 floats = %li\n", sizeof(float), sizeoffloat);
    
    //%li --&amp;gt; signed integer
    return 0;
}&lt;/pre&gt;

&lt;h3&gt;Result&lt;/h3&gt;

&lt;pre class="prettyprint"&gt;size of char 1
 How many bytes will be allocated for 10 characters = 10
size of int 4
 How many bytes will be allocated for 10 integers  = 40
size of float 4
 How many bytes will be allocated for 10 floats = 40&lt;/pre&gt;

&lt;h4&gt;Zum Beispiel…&lt;/h4&gt;

&lt;p&gt;When you create a variable of type int and give it a value, the computer looks at the type, determines how many bytes are required for that type based on the OS, selects the byte or bytes, throws the value in and locks the door. Our value is stored and secured.&lt;/p&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;


int main(){
    
    int integer = 3;
    printf("The variable 'integer' is stored at address %p\n", &amp;amp;integer);
    printf("The variable holds value %i\n", integer);
    return 0;
}&lt;/pre&gt;

&lt;p&gt;When ran, looks like this:&lt;/p&gt;

&lt;pre class="prettyprint"&gt;C:\Users\....\practice\c&amp;gt; &amp;amp; cmd.exe /c "gcc practice.c -o a &amp;amp;&amp;amp; a"  
The variable 'integer' is stored at address 0061FF1C
The variable holds value 3&lt;/pre&gt;

&lt;h2&gt;Indirection operator&lt;/h2&gt;

&lt;p&gt;This unary operator (*),when used on a pointer, helps us access the value that is pointed at by the pointer. Whew… so many points.&lt;/p&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int main() {
    int favnumber = 6;
    int * ptr = &amp;amp;favnumber;
    printf("What value does the variable ptr hold ? = %p\n", ptr);
    printf("What value does the variable ptr point to ? = %d\n", * ptr);
    printf("Is the pointer's home address, %p, identical to that of the favnumber's address, %p, ?\n", &amp;amp;ptr, ptr);
    //%p --&amp;gt; format specifier for address of pointers or any variable (void *)
    //%d ---&amp;gt; informs printf to treat the incoming value as an integer
    return 0;
}&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Result…&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="prettyprint"&gt;What value does the variable ptr hold ? = 0x7ffe30cb607c
What value does the variable ptr point to ? = 6
Is the pointer's home address, 0x7ffe30cb6080, identical to that of the favnumber's address, 0x7ffe30cb607c, ?&lt;/pre&gt;

&lt;h2&gt;The “char”&lt;/h2&gt;

&lt;pre class="prettyprint"&gt;char character = 'n';&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Where in memory does the character variable live ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If declared in a function → &lt;span&gt;stack&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If declared outside a function → global → &lt;span&gt;data segment&lt;/span&gt;&lt;/p&gt;

&lt;h3&gt;The “char” pointer variable&lt;/h3&gt;

&lt;pre class="prettyprint"&gt;char *firstchar = 'c';
char *message = "every good boy does fine";&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Where in memory do these variables live ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If declared in a function → &lt;span&gt;stack&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If declared outside a function → global → &lt;span&gt;data segment&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where do each pointer point to ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s log everythang..&lt;/p&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int main() {
  char *firstchar = 'c';
char *message = "every good boy does fine";
printf("*firstchar points to %p\n", firstchar);
printf("*message points to %p while the first character of the constant string lives in %p\n", message, &amp;amp;message[0]);
    return 0;

}
                   Compiled in 192.949 ms                   
                        Executing...                        
*firstchar points to 0x63
*message points to 0x4005c8 while the first character of the constant string lives in 0x4005c8&lt;/pre&gt;

&lt;p&gt;As you can see, these pointers point to the first character of the given string&lt;/p&gt;

&lt;pre class="prettyprint"&gt;message[0] = 'm';
*message[0] = 'm';
*(&amp;amp;message[0]) = 'm';&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Why would the above expression not work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because the pointer points to a string literal, an unmodifiable value. I was a bit confused by the pointer myself, since I always associated POINTERS with MODIFIABLE. But apparently this pointer here only means one can bind the pointer to another string literal. For instance :&lt;/p&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int main() {
  char * message  = "every good boy does fine";
  printf("1. Where does message point to &amp;lt;%s&amp;gt; at address %p\n", message, message);
  message = "Just another string";
  printf("2. Where does message point to &amp;lt;%s&amp;gt; at address %p\n", message, message);
    return 0;

}
1. Where does message point to &amp;lt;every good boy does fine&amp;gt; at address 0x4005d8
2. Where does message point to &amp;lt;Just another string&amp;gt; at address 0x40062b&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;How does this pointer know the length of the string literal? &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I assume it uses '\0' or NULL characters to determine the end of the string. &lt;a href="https://stackoverflow.com/questions/1392200/sizeof-string-literal"&gt;Read here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;The char array&lt;/h3&gt;

&lt;pre class="prettyprint"&gt;char notes[] = "every good boy does fine";&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Where do notes live in memory?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If declared in a function → &lt;span&gt;stack&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If declared outside a function → global → &lt;span&gt;data segment&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Here the program allocates 25 bytes ( 1 char → 1 byte ) for the 24 characters and the terminating character.&lt;/p&gt;

&lt;pre class="prettyprint"&gt;notes[0] = 'E';&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Why can I change the character at index 0 of notes but not do the same in the message pointer variable above ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because string literals are stored in the read-only area of memory, while the notes variable, since initialised, is stored in the read-write region. If declared and initialised in a function, then it is stored in the stack, else most likely in the initialised data segment.&lt;/p&gt;

&lt;pre class="prettyprint"&gt;*(notes[0]) = 'W';&lt;/pre&gt;

&lt;p&gt;Notes[0] → returns a char not an address&lt;/p&gt;

&lt;p&gt;The unary operator expects an address, so this would break.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why will the above &lt;span&gt;not &lt;/span&gt;work ?&lt;/strong&gt;&lt;/p&gt;

&lt;pre class="prettyprint"&gt;*(&amp;amp;notes[0]) = 'W';&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Why will the above work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Notes[0] → returns a char not an address&lt;/p&gt;

&lt;p&gt;&amp;amp;notes[0] → returns the memory location (address) of the char at index 0&lt;/p&gt;

&lt;p&gt;Since the indirection operator expects and gets an address, this would work.&lt;/p&gt;

&lt;pre class="prettyprint"&gt;Wvery good boy does fine&lt;/pre&gt;

&lt;h2&gt;The “Int”&lt;/h2&gt;

&lt;pre class="prettyprint"&gt;int num = 4;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Where does num variable live ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If declared in a function → &lt;span&gt;stack&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;If declared outside a function → global → &lt;span&gt;data segment&lt;/span&gt;&lt;/p&gt;

&lt;h3&gt;Pointer variable to an integer&lt;/h3&gt;

&lt;pre class="prettyprint"&gt;int *numptr = #&lt;/pre&gt;

&lt;h3&gt;Array of integers&lt;/h3&gt;

&lt;pre class="prettyprint"&gt;int var[5] = {1, 2, 3, 4};&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Does each integer in the var array live 2 or 4 byte blocks away from each other ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s test this, shall we….&lt;/p&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int main() {

int var[5] = {1, 2, 3, 4};
int * address_of_first_int = &amp;amp;var[0];
for(int i = 0; i &amp;lt; 5; i++){
    
    printf("Address at &amp;lt;%d&amp;gt; = &amp;lt;%p&amp;gt;; &amp;lt;%d * %li&amp;gt; bytes from the first int address &amp;lt;%p&amp;gt;\n", *(var + i), &amp;amp;var[i],i, sizeof(int), (address_of_first_int + i));
}
    return 0;

}
Address at &amp;lt;1&amp;gt; = &amp;lt;0x7ffe1d81e020&amp;gt;; &amp;lt;0 * 4&amp;gt; bytes from the first int address &amp;lt;0x7ffe1d81e020&amp;gt;
Address at &amp;lt;2&amp;gt; = &amp;lt;0x7ffe1d81e024&amp;gt;; &amp;lt;1 * 4&amp;gt; bytes from the first int address &amp;lt;0x7ffe1d81e024&amp;gt;
Address at &amp;lt;3&amp;gt; = &amp;lt;0x7ffe1d81e028&amp;gt;; &amp;lt;2 * 4&amp;gt; bytes from the first int address &amp;lt;0x7ffe1d81e028&amp;gt;
Address at &amp;lt;4&amp;gt; = &amp;lt;0x7ffe1d81e02c&amp;gt;; &amp;lt;3 * 4&amp;gt; bytes from the first int address &amp;lt;0x7ffe1d81e02c&amp;gt;
Address at &amp;lt;0&amp;gt; = &amp;lt;0x7ffe1d81e030&amp;gt;; &amp;lt;4 * 4&amp;gt; bytes from the first int address &amp;lt;0x7ffe1d81e030&amp;gt;&lt;/pre&gt;

&lt;p&gt;It is safe to say that the answer is yes.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Read up pointer arithmetics&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is an array a pointer ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ja und nein.depending on how it is used.&lt;/p&gt;

&lt;p&gt;A pointer to an int array → points to the memory location of first element of the array → does not hold values of the array&lt;/p&gt;

&lt;p&gt;An int array → holds N integers → when its identifier is assigned to a pointer, it changes to the pointer of the first element&lt;/p&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int main() {

int var[5] = {1, 2, 3, 4};
int * address_of_first_int = &amp;amp;var[0];
address_of_first_int[3] = 12;
printf("Value at %d is %d", 3, var[3]);
    return 0; 

}&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Why does the above work though ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;According to a stack overflow response, the symbol [] works with pointers. The integer within the symbol is used as the offset value from the first element of the array and the value residing is returned. This indicates that the “var” variable has to be converted to a pointer, before a value at a specific index can be set or returned.&lt;/p&gt;

&lt;pre class="prettyprint"&gt;int *ptr = var;
int *secondptr = &amp;amp;var[0];&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;What is the difference between the two pointers ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Nada = Nothing = Nichts&lt;/p&gt;

&lt;pre class="prettyprint"&gt;const int ARRAY_SIZE = 5;
int(*wholeptr)[ARRAY_SIZE] = &amp;amp;var;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;WTH is that ????&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I fell into this randomly in the GeeksforGeeks blog and I thought it would also be nice here :D. It is a pointer to an array. I think I would need to create another blog post for this alone because I honestly do not get it.&lt;/p&gt;

&lt;pre class="prettyprint"&gt;int *fiveintptr[ARRAY_SIZE];
for (int i = 0; i &amp;lt; ARRAY_SIZE; i++)
    {
        /* code */
        fiveintptr[i] = &amp;amp;var[i];
    }&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Before the loop, where do each pointer in the fiveintptr pointer array point to ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The fiveintptr variable is an array of pointers with each pointer expected to point to an int type. But since each pointer isn't assigned a value / address, it holds garbage values.&lt;/p&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
const int ARRAY_SIZE = 5;
int main() {

int var[5] = {1, 2, 3, 4};
int *fiveintptr[ARRAY_SIZE];
for (int i = 0; i &amp;lt; ARRAY_SIZE; i++)
    {
        /* code */
        printf("The value pointed to by the pointer at index %d is %p\n", i,  fiveintptr[i]);
    }
    
    puts("----------------------------------------------\n");
    for (int i = 0; i &amp;lt; ARRAY_SIZE; i++)
    {
        /* code */
        fiveintptr[i] = &amp;amp;var[i];
        printf("The value pointed to by the pointer at index %d is %p\n", i,  fiveintptr[i]);
    }

    
    return 0; 

}
The value pointed to by the pointer at index 0 is (nil)
The value pointed to by the pointer at index 1 is (nil)
The value pointed to by the pointer at index 2 is (nil)
The value pointed to by the pointer at index 3 is 0x756e6547
The value pointed to by the pointer at index 4 is 0x9
----------------------------------------------

The value pointed to by the pointer at index 0 is 0x7ffd70e07df0
The value pointed to by the pointer at index 1 is 0x7ffd70e07df4
The value pointed to by the pointer at index 2 is 0x7ffd70e07df8
The value pointed to by the pointer at index 3 is 0x7ffd70e07dfc
The value pointed to by the pointer at index 4 is 0x7ffd70e07e00&lt;/pre&gt;

&lt;pre class="prettyprint"&gt;*fiveintptr[4] = 25;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Why would it work?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fiveintptr → array of pointer&lt;/p&gt;

&lt;p&gt;Fiveintptr[4] ---&amp;gt; a pointer&lt;/p&gt;

&lt;p&gt;*pointer → dereferencing a pointer → value at given address / pointer&lt;/p&gt;

&lt;p&gt;*fiveintptr[4] → value living at an address of 3rd element of the var int array&lt;/p&gt;

&lt;p&gt;= → basic assignment operator&lt;/p&gt;

&lt;p&gt;// valid operation&lt;/p&gt;

&lt;h2&gt;The array of strings&lt;/h2&gt;

&lt;pre class="prettyprint"&gt;char top5femalartists[5][20] = {
        "beyonce",
        "jessie j",
        "christina aguilera",
        "maria carey",
        "mary j blige"
};&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Do you agree with my list ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;yesssss!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What do the 5 and 20 mean ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;5 → number of char arrays that can be stored here&lt;/p&gt;

&lt;p&gt;20 → number of characters that can be stored in each char array&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What happens when a string like "beyonce" is shorter than 20?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The remaining bytes are either empty or filled up with jargon. For example…&lt;/p&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
int main() {

 char person[20] = "beyonce";
 printf("Value at 6 %c &amp;lt;&amp;gt;\n", person[6]);
 printf("Value at 7 %c&amp;lt;&amp;gt;\n", person[7]);
printf("Value at 8 %c&amp;lt;&amp;gt;\n", person[8]);
    return 0; 

}
Value at 6 e &amp;lt;&amp;gt;
Value at 7 &amp;lt;&amp;gt;
Value at 8 &amp;lt;&amp;gt;&lt;/pre&gt;

&lt;pre class="prettyprint"&gt;top5femalartists[1] = "jennifer hudson";&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Why would the above expression not work ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;error: assignment to expression with array type&lt;/p&gt;

&lt;p&gt;Because they are not identical. “Beyonce” is an array of chars ( 20 ), while "jennifer hudson" is stored in read-only memory. They are of different types.&lt;/p&gt;

&lt;pre class="prettyprint"&gt;char *top5maleartist[5] = {
        "bruno mars",
        "michael jackson"
};&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;What does this look like ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An array of pointers, each pointing to string literals.&lt;/p&gt;

&lt;pre class="prettyprint"&gt;top5maleartist[0] = "DO of exo";&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Why did the above work ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You simple rebound the pointer to another string literal&lt;/p&gt;

&lt;pre class="prettyprint"&gt;top5maleartist[0][1] = 'd';&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Why wouldn't the above work ? &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You are trying to modify the second character of the string literal pointed by the first pointer. Remember this value type is treated as read-only, which is said to have an “undefined” behaviour.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Got the last part from stack overflow :D&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In what way could I make this modifiable ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Array of chars.&lt;/p&gt;

&lt;pre class="prettyprint"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
int main() {


char * top5maleartist[3];
char ch_arr[3][20] = {
                        "bruno mars",
        "michael jackson"
                     };
                     
for(int i = 0; i &amp;lt; 3; i++){
    
    top5maleartist[i] = &amp;amp;ch_arr[i][0];
}

*(top5maleartist[0] + 1) = 'd';
printf("Value of %s\n", (top5maleartist[0]));
    return 0; 

}
Run.....
Value of bduno mars&lt;/pre&gt;

&lt;ol&gt;
    &lt;li&gt;Create an array of pointers containing 3 pointers&lt;/li&gt;
    &lt;li&gt;Create an array of an array of characters&lt;/li&gt;
    &lt;li&gt;Set each pointer to the address of the first character of each char list&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Top5maleartist[0] → pointer to ch_arr[0][0]&lt;/p&gt;

&lt;p&gt;Top5maleartist[0] + 1 → shift by char characters * 1 → &amp;amp;ch_arr[0][1]&lt;/p&gt;

&lt;p&gt;*(Top5maleartist[0] + 1 ) → *(&amp;amp;ch_arr[0][1]) → dereferencing&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What happens to the string literal “bruno mars”?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I dunno.. .seriously.&lt;/p&gt;

&lt;h2&gt;The double pointer&lt;/h2&gt;

&lt;pre class="prettyprint"&gt;int num = 3;
int  *numptr = #
int **doubleptr = &amp;amp;numptr;
printf("%d", **doubleptr);&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;What value does it dereference ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;*doubleptr → &amp;amp;num&lt;/p&gt;

&lt;p&gt;**doubleptr → *(&amp;amp;num) → 3&lt;/p&gt;

&lt;pre class="prettyprint"&gt;printf("%d\n", *numptr);
printf("%d\n", **doubleptr);
printf("%p\n", *doubleptr);&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;What gets logged ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;0x7fff685046f4&lt;/p&gt;

&lt;h2&gt;Author Notes&lt;/h2&gt;

&lt;p&gt;This is most likely not the cleanest implementation, just a heads up.&lt;/p&gt;

&lt;h2&gt;Links&lt;/h2&gt;

&lt;ol&gt;
    &lt;li&gt;&lt;a href="https://craftofcoding.wordpress.com/2015/12/07/memory-in-c-the-stack-the-heap-and-static/"&gt;MEMORY IN C – THE STACK, THE HEAP, AND STATIC&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html"&gt;Memory : Stack vs Heap&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.sciencedirect.com/topics/engineering/stack-memory"&gt;Stack Memory Operations&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://stackoverflow.com/questions/2308751/what-is-a-memory-heap"&gt;What is a Memory Heap?&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/pointers-in-c-and-c-set-1-introduction-arithmetic-and-array/"&gt;Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/memory-layout-of-c-program/"&gt;Memory Layout of C Programs&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://denniskubes.com/2012/08/17/basics-of-memory-addresses-in-c/"&gt;Basics of Memory Addresses in C&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://computer.howstuffworks.com/c23.htm"&gt;Memory addresses wonderfully explained&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.tutorialspoint.com/cprogramming/c_data_types.htm"&gt;C - Data Types&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="http://www.cs.ecu.edu/karl/3300/spr14/Notes/C/Memory/memory.html"&gt;The Memory and Memory Addresses&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/whats-difference-between-char-s-and-char-s-in-c/"&gt;What’s difference between char s[] and char *s in C?&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://overiq.com/c-programming-101/array-of-strings-in-c/"&gt;Array of Strings in C&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://stackoverflow.com/questions/20347170/char-array-and-char-array"&gt;char *array and char array[]&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/pointer-array-array-pointer/"&gt;Array pointers&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/pointer-vs-array-in-c/?ref=rp"&gt;Pointer vs Array in C&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.computerhope.com/jargon/a/array-of-pointers.htm"&gt;Array of pointers&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://overiq.com/c-programming-101/array-of-pointers-to-strings-in-c/"&gt;Array of pointers to strings&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/double-pointer-pointer-pointer-c/"&gt;Double pointers&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://codeforwin.org/2015/05/list-of-all-format-specifiers-in-c-programming.html"&gt;%li&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/size_t-data-type-c-language/"&gt;Size_int&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://medium.com/@nonuruzun/where-global-variables-are-stored-e1a8b7e90275"&gt;Where global variables are stored?&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://stackoverflow.com/questions/52463689/rationale-behind-code-segment-and-data-segment"&gt;Rationale behind code segment and data segment&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.uninformativ.de/blog/postings/2015-10-25/0/POSTING-en.html"&gt;Storage of global and static variables in C&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Executable"&gt;Executable&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Code_segment"&gt;Code segment&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Data_segment"&gt;Data segment&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/"&gt;C Strings (Arrays vs. Pointers)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://stackoverflow.com/questions/5033627/static-variable-inside-of-a-function-in-c"&gt;Static variable inside of a function in C&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.tutorialspoint.com/where-are-static-variables-stored-in-c-cplusplus"&gt;Where are static variables stored in C/C++?&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://stackoverflow.com/questions/47301556/difference-between-stack-segment-and-uninitialized-data-segment"&gt;difference between stack segment and uninitialized data segment&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://stackoverflow.com/questions/3113668/location-of-pointers-and-global-variables-in-c"&gt;Location of pointers and global variables in C&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.tutorialcup.com/cprogramming/array-memory-allocation.htm"&gt;Array Memory Allocation in C Programming&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer"&gt;Is an array name a pointer?&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/pointer-array-array-pointer/"&gt;Pointer to an Array | Array Pointer&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/difference-between-pointer-to-an-array-and-array-of-pointers/"&gt;Difference between pointer to an array and array of pointers&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="https://stackoverflow.com/questions/2589949/string-literals-where-do-they-go"&gt;String literals: Where do they go?&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>c</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>CockroachDB ?? who and how do they come up with these names </title>
      <dc:creator>Comfort Ajala</dc:creator>
      <pubDate>Fri, 17 Jan 2020 00:02:15 +0000</pubDate>
      <link>https://dev.to/comfortajalaoluwatimilehin/cockroachdb-who-and-how-do-they-come-up-with-these-names-1h38</link>
      <guid>https://dev.to/comfortajalaoluwatimilehin/cockroachdb-who-and-how-do-they-come-up-with-these-names-1h38</guid>
      <description>&lt;p&gt;Genuinely curious about the "thought-process" involved in naming libraries and frameworks. What are the sources of inspiration... etc.&lt;/p&gt;

</description>
      <category>curious</category>
      <category>help</category>
    </item>
  </channel>
</rss>
