I thought it would be pretty basic stuff. My C/C++ is rusty.. used it 14+ years back.. have been coding in perl, shell & python since and have a frustrating problem at hand in C code:
I have a string something like below in argv[7]:
Min:Max:Both:Both
I want to break it using colon and store it into array. then I want to access a certain element of array, pass it to a string var and pass it around a couple of functions.. my problem is in defining the string array and then passing around the string across functions, which is pretty basic stuff in scripting languages
I am doing something like this:
int main(int argc, char ** argv){
int iy = 0;
char * y = (char *)malloc(20*sizeof(char));
char * tky = strtok(argv[7], ":");
do {
sprintf(y[iy],tky);
printf("as string = %s and as array value = %s\n", tky, y[iy]);
//printf("as string = %s \n", tky);
iy++;
} while((tky=strtok(NULL,":"))!=NULL);
int measquant = 3;
char colminmax[20];
for(i=0; i<measquant; i++){
sprintf(colminmax,"%s",y[i]);
testfunction(colminmax);
}
return 0;
}
testfunction (char* dir){
printf("dir is %s",dir);
}
It is printing tky correctly and y[iy] as NULL Then I am trying to assign and pass it as:
Please help what am I missing. I have searched numerous C/C++ help sites but unable to get to something as basic.