I can't be the only one who begins to feel overwhelmed by all the asterisks... I'm sorry if you find this question redundant but I am really having trouble applying the pointer concepts I've already read about to this particular instance. I'm trying to figure out why it is that I get a core dump every time I run this bit of code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void parseInput(char *input, char **argv){
printf("%s",input);
int count=0;
char delimit[] = " \t\n\r";
char* token;
/* get the first token */
token = strtok(input, delimit);
argv[count] = &token;
count++;
while( argv[count]!= NULL ) {
printf("%s\n", *argv[count]);
token = strtok(NULL, delimit);
argv[count] = &token;
count++;
}
}
main(){
int bytes_read;
int bytes = 100;
char *input, c;
char prompt[] = "% ";
int length, j;
do{
printf("%s",prompt);
input = (char *)malloc(bytes);
bytes_read = getline(&input, &bytes, stdin);
length = sizeof(input)/sizeof(input[0]);
char *argv[length];
parseInput(input, argv);
free(input);
}while(bytes_read != -1);
return 0;
}
What I want to do is take the input array, tokenize it, and then set each element of argv[] to point to the beginning of each token. I think my troubles are with really not knowing how to reference the arrays in a manner suitable to this purpose. What can I do to remedy this?
Thanks.