简体   繁体   中英

How to create a similar char pointer like char *argv

The command line pointer char *argv [] used in C language allows operations such as;

ptintf ("%c", argv [3][6]); // to print the 7th element of 4th argument

printf ("%s", argv [3]); // to print the 4th parsed argument

Now I understand that argv char array is created before main function is called and it char *argv [] is merely a pointer directing towards that array created by the compiler.

How can we create a pointer in C code, which would allow similar operations?

I tried doing,

 #include <stdio.h>

int main() {

char array [20] [20];
       char *ptr_array = array ;
      array [0] [0]= 'a';
      array [0][1]= 'b'; 
      array [0] [2] = '\0'; 

      array [1] [0]= 'a';
      array [1][1]= 'b'; 
      array [1] [2] = '\0'; 

      array [2] [0]= 'a';
      array [2][1]= 'b'; 
      array [2] [2] = '\0';          

 // assuming there is content in the array
    printf("%s\n", ptr_array [1]);
    return 0; }

but I end up with a warning upon compilation and a core dump upon execution.

What you want is an array of pointers. So the declaration of ptr_array should be

char *ptr_array[20];    // an array of 20 pointer-to-char

The following code uses strcpy to fill in two of the strings in the array . The {{0}} makes sure that all the other strings are zeroed. Then the ptr_array is declared and all 20 pointers are initialized. Finally one of the strings is printed.

int main( void )
{
    char array[20][20] = {{0}};
    strcpy( array[0], "hello" );
    strcpy( array[1], "world" );

    char *ptr_array[20];
    for ( int i = 0; i < 20; i++ )
        ptr_array[i] = array[i];

    printf( "%s\n", ptr_array[0] );
    return 0;
}

You can use the array of pointers like

int main() {

       char *ptr_array[5] = { "ab","cd", "ef","gh" };     
       printf("%s\n", ptr_array [1]);
       return 0; 
    }

As you see argv was a array of pointers char *argv [] you can come up with the same like shown above.

Note that the ptr_array here is just readable you can make it writable by allocating memory or making the pointers point to writable memory.

int main() {

       char *ptr_array[5]; 
       char str[20];
       int i=0;   
       for(i=0;i<5;i++)
       {
         ptr_array[i] = malloc(20);
         scanf("%s",str);
         strcpy(ptr_array[i],str);
       }
       printf("%s\n", ptr_array [1]);
       return 0; 
    }

PS: argv can be *argv[] or **argv

You can accomplish what you want with simply array . No need for ptr_array .

char array[20][20];
// array initialization
printf("%s\n", array[1]);
printf("%c\n", array[1][1]);

argv is an array of char arrays. Exactly like array in the above code.

Command line arguments are passed as strings ( char arrays) and argv is an array containing all those strings.


About use of * and [] :

char array[10];

array is now a pointer to the first element in that array, thus it is a char* .

For example, char* argv[] is the same as char** argv :

  • argv is a pointer to the first element in the array containg char* elements, thus it is a char** .

  • argv[x] accesses the argv array at index x , thus argv[x] is a char* , pointing to the first char in a string (a char array).

  • argv[x][y] accesses the char array argv[x] at index y , thus argv[x][y] is a char , an element in a char array string.

Your array has type char[20][20] .

In most contexts, using simply array converts it to a pointer to its first element: &(array[0]) which has type (*)char[20] (pointer to array 20 of char).

You can assign this address to a pointer of the same type:

char array[20][20];
char (*ptr_array)[20]; // ptr_array is a pointer to an array of 20 char
ptr_array = array;     // types of ptr_array and (the converted) array are compatible
printf("%s\n", ptr_array[1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM