title: A question about allocating memory : C [UBUNTU]
body: <p>I'm currently doing an exercise where I have to create a program that takes all the code that is written inside it, and outputs it to the screen when the program is executed.</p>
  <p>The exercise suggests that we may find it appropriate to change the file names of the program in the future - and  assuming that the renaming is done in a coordinated manner, i.e. the source file and the execution file are given the same new name (except for the extension), the program should work correctly, without the need for any changes to the source code, and without the need to recompile.</p>
  <p>The C program itself is called 'prnt.c'-</p>
  <p>I wrote the following code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #define ENDFILEC &quot;.c&quot; /* extension  */
  
  int main(int argc, char *argv[])
  {
     FILE *filePointer;
     int character;
     char *fileNameToOpen;
  
     fileNameToOpen = (char *)malloc(strlen(argv[0]) + 3); /* allocating memory for string + 3 for the extension - '.c' and \0 */
  
     strcpy(fileNameToOpen, argv[0]); 
     strcat(fileNameToOpen , ENDFILEC); /* ending '.c' in the end */ 
  
     filePointer = fopen(fileNameToOpen, &quot;r&quot;);
       while(!feof(filePointer))
       {
         character = fgetc(filePointer);
         printf(&quot;%c&quot; , character);
       }
  
  
     fclose(filePointer);
     return 0;
  }
  </code></pre>
  <p>I made a 'makefile' to compile the program and I made it so that the executable would be called 'prnt1'.</p>
  <p>basically, like the following:</p>
  <pre><code>prnt1 : prnt.c
       gcc -ansi -Wall -pedantic prnt.c -o prnt1
  </code></pre>
  <p>The compilation worked, but whenever I run the program itself, it gives me a runtime error, saying: &quot;Segmentation fault (core dumped)&quot;. When I look at the code itself, I don't seem to reach a memory that doesn't belong to me, so what could be an explanation for that problem and what can be done about it? Thank you in advance for your help.</p>