0

What I know about this code is that in the void cylinder function char** argv is a array of string and numbers are stored in this. I dont know how to convert this array into the int values and then use it.

    void cylinder(int argv_size, char** argv){
    //comlete this code
    }

    int main(){
    int n;
    scanf("%i",&n);
    char* *a = malloc(sizeof(char*) * n);
    for(int i = 0; i<n;i++){
        a[i] = (char *)malloc(1020 * sizeof(char));
        scanf("%s",a[i]);
        }
    cylinder(n,a);
    return 0;
    }
DrkEthics
  • 3
  • 4

2 Answers2

0

You might want to check if some arguments/ no arguements have been passed:

if(argc==1) 
        printf("\nNo Command Line Argument Passed Other Than Program Name"); 
    if(argc>=2) 
    { 
        printf("\nNumber Of Arguments Passed: %d",argc); 

        for(int counter=0 ; counter<argc ; counter++) 
            printf("\nargv[%d]: %s",counter,argv[counter]); 
    }

Then, to convert the passed integer(passed as a string) value use atoi, like: atoi(argv[1]) if only one integer value has been passed.

Note: use #include<stdlib.h> for atoi()

zeroSpook
  • 54
  • 10
0

I see some disadvantage in your code:

scanf("%s",a[i]);

Should change to (see disadvantages of scanf ):

scanf("%1019s",a[i]);
//OR
fgets(a[i], 1020, stdin);

You should check the result of malloc function, and do not cast malloc as i see you allocated for a[i]:

char* *a = malloc(sizeof(char*) * n);
if(!a) {
//handle the error
return -1; // for example
}

and

a[i] = malloc(1020 * sizeof(char));
if(!a[i]) {
//handle the error
}

You have to free the memory of each a[i] and a after cylinder function to avoid memory-leaks.

For cylinder function:

void cylinder(int argv_size, char** argv){
    for(int i = 0; i < argv_size; i++) {
        // convert argv[i] to int by using sscanf, atoi, strol, etc.
        // for example i use atoi function:
        printf("value of (int)argv[i] = %d", atoi(argv[i]);
    }
}

You can also find other function for converting string to int in this link How to convert a string to integer in C?

Hitokiri
  • 3,607
  • 1
  • 9
  • 29