0

so I am having problems with making it so I can use argv as array of chars. I want to be able to use words from argv in the rest of my program (that puts them into tree trie, then counts how many occurences each one had and sorts it) so i tried to copy it to another 2d array but it didn't work. Please help, code: https://gist.github.com/anonymous/3f55b47ca062b60c8091

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

#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
#define ALPHABET_SIZE (26)
// Converts key current character into index
// use only 'a' through 'z' and lower case
#define CHAR_TO_INDEX(C) ((int)C - (int)'A')
#define INDEX_TO_CHAR(IX) ('A' + IX)


char words[3000][40]={{0}};
int counters[3000]={0};
int wordnr=0;

typedef struct trie_node trie_node_t;
struct trie_node
{
    int counter;
    trie_node_t *children[ALPHABET_SIZE];
};

typedef struct trie trie_t;
struct trie
{
    trie_node_t *root;
    int count;
};

// Returns new trie node
trie_node_t *getNode(void)
{
    trie_node_t *pNode = NULL;

    pNode = (trie_node_t *)malloc(sizeof(trie_node_t));

    if( pNode )
    {
        int i;

        pNode->counter = 0;

        for(i = 0; i < ALPHABET_SIZE; i++)
        {
            pNode->children[i] = NULL;
        }
    }

    return pNode;
}

// Initializes trie
void initialize(trie_t *pTrie)
{
    pTrie->root = getNode();
    pTrie->count = 0;
}

void setorder_rec(trie_node_t *pCrawl, char *str, int n)
{
    if (pCrawl == NULL) return;

    if (pCrawl->counter) {
    str[n]='\0';
        strcpy(words[wordnr],str);
        words[wordnr][strlen(str)]='\0';
        counters[wordnr]=pCrawl->counter;
        wordnr++;
        printf("%.*s: %d\n", n, str, pCrawl->counter);
    }

    for (int i = 0; i < ALPHABET_SIZE; i++) {
        str[n] = INDEX_TO_CHAR(i);
        setorder_rec(pCrawl->children[i], str, n + 1);
    }
}

void setorder(trie_t *pTrie)
{
    char tempword[40] = {0};

    setorder_rec(pTrie->root, tempword, 0);
}

void insert(trie_t *pTrie, char key[])
{
    int level;
    int length = strlen(key);
    int index;
    trie_node_t *pCrawl;

    pTrie->count++;
    pCrawl = pTrie->root;

    for( level = 0; level < length; level++ )
    {
        index = CHAR_TO_INDEX(key[level]);
        if( !pCrawl->children[index] )
        {
            pCrawl->children[index] = getNode();
        }
        pCrawl = pCrawl->children[index];
    }

    pCrawl->counter++;
}

void bubble_sort()
{
    int i,j,temp;
    int n=15;
    char tempwordy[40]={0};


    for(i=1;i< n;i++)
    {
        for(j=0;j< n-1;j++)
        {
            if(counters[j]>counters[j+1])
            {   
        printf("swapping counters[%d]=%d with counters[%d]=%d\n", j, counters[j], j+1, counters[j+1]);
                temp=counters[j];
                counters[j]=counters[j+1];
                counters[j+1]=temp;

        strcpy(tempwordy,words[j]);
        strcpy(words[j],words[j+1]);
        strcpy(words[j+1],tempwordy);
            }
         }
    }
}

int main(int argc, char *argv[])
{
    char *keys[argc];
    for(int i=0;i<argc;i++)
    keys[i] = (char *) malloc(strlen(argv[i+1]+1));

    for(int i=0;i<argc;i++)
        strcpy(keys[i], argv[i+1]);





    if (argc < 2) {
        fprintf(stderr, "Must pass an argument!\n");
        exit(1);
    }

    trie_t trie;

    initialize(&trie);

    // Construct trie
    for(int i = 0; i < ARRAY_SIZE(keys); i++)
    {
        insert(&trie, keys[i]);
    }

    setorder(&trie);
    bubble_sort();

    for(int i=argc-1; i>=0; i--)
    {
        printf("#%d %s=%d\n", i, words[i], counters[i]);
    }

    return 0;
}

2 Answers 2

0

Problem could be here:

keys[i] = (char *) malloc(strlen(argv[i+1]+1));

Try to rewrite to:

keys[i] = (char *) malloc(strlen(argv[i+1])+1);

Else you adding 1 to pointer.

Or if you need to ignore first char, you must add 1 char for end of string.

2
  • You have many errors in your code, I'll stopped to search after some. Compile your code with debug info and use GDB to locate the problem.
    – loentar
    Jan 20, 2014 at 11:52
  • Well, if you remove those lines that copy from argv to keys and just make a keys array with some words it works just fine.
    – deviance
    Jan 20, 2014 at 12:00
0

You can't do this:

for(int i=0; i<argc; i++) ... argv[i+1] ...

argv only goes from 0 to argc - 1.

I suggest, instead, you do

for(int i=1; i<argc; i++) keys[i] = (char *)malloc(strlen(argv[i]+1));

 for(int i=1; i&lt;argc; i++)
    strcpy(keys[i], argv[1]);</code>

Note the change in the loop start and the drop of the +1.

2
  • Thanks, but what does "i&lt;argc" mean? Also I have to start from i=0
    – deviance
    Jan 20, 2014 at 11:30
  • Sorry, that's a <. Stack Overflow was messing up my code blocks. Jan 20, 2014 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.