JJ.kah JJ.kah - 2 months ago 8
C Question

scan a value in the main and pass it to a function that convert that value C language

hi friends I have that this function in C language

struct webtech tramafunction();


this function receive a value like this

/*¶bL0 L3,01,+08590323,-079343001,010215,00000000000000,-tN,000,012689997,001219456,000,7FF2,C07F,0,4,*/


and then the function make this:

struct webtech tramafunction(){
struct webtech wbt;
char buf[103]="";
scanf("%[^\t\n]s",buf);
printf("\n \n data destinated to convert=[%s]\n\n", buf);
int z;
z=strlen(buf);
printf("size of data: %d",z);
int i = 0;
char *p = strtok (buf, ",");
char *array[16]={0};
while (p != NULL)
{
array[i++] = p;
p = strtok (NULL, ",");
}

for (i = 0; i <16; ++i){
wbt.x15 = 0;
if (array[i] != NULL){
wbt.x15=atoi(array[15]);
}

printf("data: [%s]\n", array[i]);
}



printf("\n \t \t --data--\n");

strcpy(wbt.indice,array[0]);
printf("INDEX: [%s]\n",wbt.indice);


wbt.prid=atoi(array[1]);
printf("PRE INDEX: [%d]\n",wbt.prid);
return wbt;
}


the main do this:

int main()
{

struct webtech con;

con = tramafunction();
return 0;

}


the question is how can I make that the data to be converted(char buf[]) get read in the main and not in the function.

Answer

you just have to declare the variable

char buf[103] = ""; 

inside the function, in the header too

struct webtech tramafunction(char buf[103]);

in the main :

int main()
 {

 struct webtech con;
 char tr[103]="";
 scanf("%[^\t\n]s",tr);
 con = tramafunction(tr);
return 0;

 }