by Joel Limardo
Home | Twitter | LinkedIn | Consulting | About | RSS | Map

Thu, 02 Feb 2017


This is a tool you can get using:

 sudo apt-get install c-repl

It has a really small footprint but it is a finnicky tool that is a bit of fun to play with. For instance, you can normally just do the following to test out making different types of arrays in C (store this in a file called simplearrays.c):

#include <stdio.h>
int main() {

char sentence1[] = "This is a sentence!\n";
  int m[10] = {1,2,3,4,5,6,7,8};
  int i;
  for (i = 0; i<=9; i++) {
      printf("This is the element: %d\n", m[i]);
  return 0;

And to compile just do this on the command line:

     make -f simplearrays

Should produce:

This is the element: 1
This is the element: 2
This is the element: 3
This is the element: 4
This is the element: 5
This is the element: 6
This is the element: 7
This is the element: 8
This is the element: 0
This is the element: 0
This is a sentence!

Now, in c-repl when you try to directly create an array in that fashion the thing crashes:

/usr/lib/c-repl/codesnippet.rb:19:in with_semi': undefined method+' for nil:NilClass (NoMethodError)
    from /usr/lib/c-repl/codesnippet.rb:46:in parse'
    from /usr/lib/c-repl/codesnippet.rb:14:ininitialize'
    from /usr/lib/c-repl/codesnippet.rb:57:in new'
    from /usr/lib/c-repl/codesnippet.rb:57:inparse'
    from /usr/bin/c-repl:161:in c_command'
    from /usr/bin/c-repl:196:inblock in inputloop'
    from /usr/bin/c-repl:184:in loop'
    from /usr/bin/c-repl:184:ininputloop'
    from /usr/bin/c-repl:203:in `

Bamph. But you can get around this by including a header and using a simple trick:

.h string.h
char foo[];
<stdin>:3:6: warning: array ‘foo’ assumed to have one element [enabled by default]
strcpy(foo,"This is a string");
This is a string

To see what c-repl is doing use the .d flag to toggle debugging:

debug is on

#include <stdio.h>
#include "string.h"

extern char foo[];
void dl4() {
  #line 1
  printf("repl is ok\n");
gcc -xc -g -fPIC -shared -o /tmp/c-repl.uZqS0iwFXV/  -
repl is ok

As you can see, c-repl creates a shared library in the /tmp/c-repl.uZqS0iwFXV/ directory for every instruction, increments the name of the routine (dl1...N), includes some variables previously used during the session, and then runs it.

Whenever you see c-repl in examples in this section you'll know what it is referring to.

posted at: 21:36 | path: /technical/C | this entry | top

© 2015-2017. All Rights Reserved. All original code appearing on this site is MIT Licensed unless otherwise noted.