J
jkelt46
Hi,
I need to take a command line parameter and store it as an array.
i.e. program parameter1 should be stored like this:
wordarray[0]=p
wordarray[1]=a
wordarray[2]=r
wordarray[3]=a
wordarray[4]=m
wordarray[5]=e
etc
or if I do program para1 para2 it should be stored like this:
wordarray[0]=para1
wordarray[1]=para2
The following code works great when I do program para1 para2 however
it doesn't work for program parameter1. When I compile I get
program2c.c:209: warning: assignment makes pointer from integer
without a cast
The reason I use char **wordarray is because the program can take a
variable number parameters.
What am I doing wrong?
Thanks,
int main(int argc, char *argv[]) {
int numleft,temp;
char **wordarray;
numleft=(argc-i)-1;
if (numleft > 1) {
wordarray=malloc(numleft * sizeof(*wordarray));
if (wordarray == NULL) {
printf("can't allocate memory for wordarray\n");
exit(EXIT_FAILURE);
}
for (temp=0; temp < numleft; temp++) {
wordarray[temp] = argv[++i];
}
}
else {
numleft=strlen(argv[i+1]);
wordarray=malloc(numleft * sizeof(*wordarray));
if (wordarray == NULL) {
printf("can't allocate memory for wordarray\n");
exit(EXIT_FAILURE);
}
for (temp=0; temp < numleft; temp++) {
wordarray[temp] = argv[i+1][temp]; // line 209
}
}
dowork(wordarray,numleft,0);
}
I need to take a command line parameter and store it as an array.
i.e. program parameter1 should be stored like this:
wordarray[0]=p
wordarray[1]=a
wordarray[2]=r
wordarray[3]=a
wordarray[4]=m
wordarray[5]=e
etc
or if I do program para1 para2 it should be stored like this:
wordarray[0]=para1
wordarray[1]=para2
The following code works great when I do program para1 para2 however
it doesn't work for program parameter1. When I compile I get
program2c.c:209: warning: assignment makes pointer from integer
without a cast
The reason I use char **wordarray is because the program can take a
variable number parameters.
What am I doing wrong?
Thanks,
int main(int argc, char *argv[]) {
int numleft,temp;
char **wordarray;
numleft=(argc-i)-1;
if (numleft > 1) {
wordarray=malloc(numleft * sizeof(*wordarray));
if (wordarray == NULL) {
printf("can't allocate memory for wordarray\n");
exit(EXIT_FAILURE);
}
for (temp=0; temp < numleft; temp++) {
wordarray[temp] = argv[++i];
}
}
else {
numleft=strlen(argv[i+1]);
wordarray=malloc(numleft * sizeof(*wordarray));
if (wordarray == NULL) {
printf("can't allocate memory for wordarray\n");
exit(EXIT_FAILURE);
}
for (temp=0; temp < numleft; temp++) {
wordarray[temp] = argv[i+1][temp]; // line 209
}
}
dowork(wordarray,numleft,0);
}