Been searching through here but please forgive me if this post is a duplicate... I just want to clarify things wherein I am not sure of...
First of all, let me give an introduction to what I have in mind:
- The user will be asked to input a command (similar to the command line/shell commands).
- The command will be received/inputted as a string separated by white spaces.
- Next the string will be tokenized (split), the number of words will be counted.
- With that number of words at hand, I will have to create a dynamic multi-dimensional array of strings, where the size/number of strings are the number of words we counted.
Example:
I Name 1234123 123123
The number of words/strings inside that command is 4, so the number of string pointers will be dynamically allocated.
I want this strings to be stored in a dynamic multi-dimensional array where:
char ** arguments;
int count;
I want to access the values inside the arguments array as:
for(i = 0; i < count; i++)
printf("%s", arguments[i]);
MY PROBLEM IS
When I try to access these variables in my main function, the values inside the arguments variable are garbage and the count variable continues to increment, knowing that I have initialized the count variable back to 0.
You can check on my code if I have done something vague or wrong. Thanks!
Here are the codes that I was able to create where in which I think are useful:
/* This function counts the number of words inside the command input. */
int word_count(char * str)
{
int i, ctr;
for(ctr = i = 0; str[i] != '\0'; i++)
{
while(isspace(str[i]))
i++;
if(str[i] != '\0')
{
ctr++;
while(str[i] != '\0' && ! isspace(str[i]))
i++;
}
}
return ctr;
}
/* This function splits the strings inside the command. */
void split_command(int count, char *** argv, char * command)
{
char buffer[31];
int i, j, k;
*argv = (char **) malloc(sizeof(char *) * count);
for(i = k = 0; command[i] != '\0'; i++)
{
while(isspace(command[i]))
i++;
if(command[i] != '\0')
{
for(j = 0 ;j < 30 && command[i] != '\0' && ! isspace(command[i]); j++, i++)
buffer[j] = (j != 0) ? command[i] : toupper(command[i]);
buffer[j] = '\0';
if(strlen(buffer) > 0)
{
(*argv)[k] = (char *) malloc(sizeof(char) * (strlen(buffer) + 1));
strcpy((*argv)[k], buffer);
k++;
}
}
}
}
/* This function will re-initialize the provided arguments and counters. */
void prepare_command(int * count, char *** argv)
{
int i;
for(i = 0; i < *count; i++)
{
(*argv)[i] = NULL;
free((*argv)[i]);
}
*count = 0;
free(*argv);
*argv = NULL;
}
Main
void main(void)
{
char ** arguments, * command;
int count, i;
boolean end = False; // Assume I created an enum for boolean {False, True}
/* Some initialization here. */
count = 0;
arguments = NULL;
do
{
gets(command);
count = word_count(command); /* This will return the number of strings inside the command */
split_command(count, &arguments, command); /* This will allocate an array. */
/* When I try to display something from here, some values are garbage and some are from the previous allocation... */
for(i = 0; i < count; i++)
printf("%s\n", arguments[i]);
prepare_command(&count, &arguments);
}while(!end);
}
PS: I know strtok and I just don't want to use it, and I don't have strtok_r inside any of my libraries here. So I created my own function to take care of something similar to that.
This might be a long post, but I would like you to help me guys... Thanks! Will have to edit this post if necessary. Thank you, hope you shed some light to me. :3 :)