0

Im trying to create a C program to create a small wordlist .txt file. I created char array of 10 elements, and I need every possible 4 letter combination of those 10 chars.

So, i figured to have 4 for loops inside each other to take elements from that array and create 4 letter combinations and write it to a .txt.

My problem is with using pointers to access elements of the array. I am doing:

char array[10] = {'A', 'S' ,'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M'};
char input[4];
char *p;
p=array;
FILE *pFile = NULL;
char *filename = "output.txt";
pFile = fopen(filename, "w");

for(i=0;i=3;i++) {
  for(j=0;j=3;j++) {
    for(k=0;k=3;k++) {
      for(l=0;l=3;l++) {
        strcpy(input, *(p+i));
        strcat(input, *(p+j));//"gluing" first and second element of new string together
        strcat(input, *(p+k));//same as line before
        strcat(input, *(p+l));
        strcat(input, "\n");
        fprintf(pFile, input);

//end of for loops, closing .txt file etc.

This compiles nicely and terminal starts, but then crashes. I think its because of some error in accessing array elements.

Any ideas? Much appreciated!

Additional info> When I create:

char string[10] = "assd";
//and insert that instead of *(p+i) anywhere it works as it is supposed to
2
  • char array[10] = {A, S ,D, F, G, H, J, K, L, M}; This compiles nicely...? Hard to believe... Commented Jun 21, 2014 at 13:07
  • warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast; and /usr/include/string.h:127: note: expected ‘const char * restrict’ but argument is of type ‘char’… gcc even without any warning explicitly enabled. Commented Jun 21, 2014 at 13:14

2 Answers 2

1

strcpy and strcat both continue until they find a NULL, and your array doesn't have one. Each slot in the array is a single character, followed by the next single character, etc. So strcpy will begin by copying from the selected letter till beyond the end of the entire list. Finally, you're also adding a 5th element ("\n") to a 4 element array. Instead, do this:

input[0] = *(p+i);
input[1] = *(p+j);
input[2] = *(p+k);
input[3] = *(p+l);
input[4] = "\n";

This should work. Note, however, that fprintf might be an easier way to go:

fprintf(pFile, "%c%c%c%c\n", *(p+i), *(p+j), *(p+k), *(p+l));

I'm tempted to continue playing "code golf" with your code, because there are a number of things that could improve it in the long run, but I think I'll stop at getting it working (though I find it amazing your compiler accepts the first line without listing the letters using quotes like 'A'). One final comment though: you don't need the p variable at all. You can actually do array[i] in my above just easily as *(p+i]).

Sign up to request clarification or add additional context in comments.

2 Comments

strcpy and strcat won't start copying from the selected letter. OP's passing integers as pointer values.
Thanks, works great. Decided to use fprintf like you used it. So, the problem was in using strcpy and strcat functions. Thanks a lot!
0
#include <stdio.h>

int main(void){
    char array[] = {'A', 'S' ,'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M'};
    char input[5] = {0};
    char *filename = "output.txt";
    int size = sizeof(array)/sizeof(*array);
    FILE *pFile = NULL;
    int i,j,k,l;
    pFile = fopen(filename, "w");

    for(i=0;i<size;++i){
        input[0] = array[i];
        array[i] = 0;//selected
    for(j=0;j<size;++j){
        if(!(input[1] = array[j]))
            continue;//skip
        array[j] = 0;
    for(k=0;k<size;++k){
        if(!(input[2] = array[k]))
            continue;
        array[k] = 0;
    for(l=0;l<size;++l){
        if(!(input[3] = array[l]))
            continue;
        fprintf(pFile, "%s\n", input);//10*9*8*7 lines
    }
        array[k] = input[2];
    }
        array[j] = input[1];
    }
        array[i] = input[0];//restore
    }

    fclose(pFile);
    return 0;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.