1

I have found piece of C code which would be very useful for what I want do to under this link: All possible combinations of characters and numbers

#include <stdio.h>

//global variables and magic numbers are the basis of good programming
const char* charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
char buffer[50];

void permute(int level) {
  const char* charset_ptr = charset;
  if(level == -1){
    puts(buffer);
  }else {
   while(buffer[level]=*charset_ptr++) {
    permute(level - 1);
   }
  }
}

int main(int argc, char **argv)
{

  int length;
  sscanf(argv[1], "%d", &length); 

  //Must provide length (integer < sizeof(buffer)==50) as first arg;
  //It will crash and burn otherwise  

  buffer[length]='\0';
  permute(length - 1);
  return 0;
}

However, when I try to compile it as it is suggested, I get following errors. Can anyone please help me to correct it?

$ make CFLAGS=-O3 permute && time ./permute 5 >/dev/null
make: Nothing to be done for 'permute'.
./permute: line 3: //global: No such file or directory
./permute: line 4: const: command not found
./permute: line 5: char: command not found
./permute: line 7: syntax error near unexpected token `('
./permute: line 7: `void permute(int level) {'

Also when I try to use gcc I get Segmentation fault error:

$ mv permute permute.c
$ gcc permute.c -o permute.bin
$ chmod 755 permute.bin 
$ ./permute.bin 
Segmentation fault (core dumped)
1
  • Give an argument when running ./permute.bin
    – Vombat
    Dec 15, 2016 at 10:40

2 Answers 2

1

It appears that you initially named the C file permute; when the make failed, you tried to execute it with your shell, which resulted in all of those syntax errors (as the shell does not know how to execute C code).

In the second case, you hit the comment:

//Must provide length (integer < sizeof(buffer)==50) as first arg;

//It will crash and burn otherwise

because you did not provide a first (or any) arguments to the program. Try ./permute.bin 10.

0

In the first case, it looks like you saved the C code as permute and then tried running

make CFLAGS=-O3 permute && time ./permute 5 >/dev/null

Since there’s no Makefile with a target for make, it exited without error while printing the informative message,

Nothing to be done for 'permute'.

Asmake did not return an error code, the second part of that command (time ./permute 5 >/dev/null) was run. Since permute is source code and not an executable binary, it was interpreted as being a shell script resulting in the following output:

./permute: line 3: //global: No such file or directory
./permute: line 4: const: command not found
./permute: line 5: char: command not found
./permute: line 7: syntax error near unexpected token `('
./permute: line 7: `void permute(int level) {'

Better instructions would be to save the C source code into permute.c and then run the following command to compile it (and link to required library files):

gcc -O3 -o permute permute.c

This creates permute as an executable binary which can be run, e.g.:

./permute 2

Not the answer you're looking for? Browse other questions tagged .