I want to proceed an Array of string Arg[10][50] using function strtoArg(),
It should be return two values one is variable 'ArgNo' which is Numbers of Arg (would pass by value) & other is Arguments array variable 'arg' (would pass by reference).
Help me to solve this problem, how can I get proper values of Arg return back from function.
Thanks in advance.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define IN
#define OUT
int strtoArg (IN char* instr, OUT char &arg)
{
char *pch,temp[10][50];
int ArgNo=0,j;
pch = strtok (instr," -");
while (pch != NULL){
strcpy((char*)(temp[ArgNo]),pch);
arg = &temp[ArgNo];
pch = strtok (NULL, " -");
ArgNo++;
}
return ArgNo;
}
int main()
{
char Instr[50],Arg[10][50];
int ArgNum,i;
gets(Instr);
ArgNum = strtoArg(Instr,*Arg);
for(i=1; i<ArgNum; ++i)
printf("%s",Arg[i]);
return 0;
}
I have try to go this way,
int strtoArg (IN char* instr, OUT char* arg)
{
char *pch;
int ArgNo=0,j;
pch = strtok (instr," -");
while (pch != NULL){
strcpy((char*)arg[ArgNo],pch);
pch = strtok (NULL, " -");
ArgNo++;
}
return ArgNo;
}
int main()
{
char Instr[50],Arg[10][50];
int ArgNum,i;
gets(Instr);
ArgNum = strtoArg(Instr,*Arg);
printf("here");
for(i=1; i<ArgNum; ++i)
printf("%s",Arg[i]);
return 0;
}
It's remove error but create Segmentation fault. Am I on right path?