2

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.

4
  • 1
    ptr_array is a pointer-to-char. I don't see what it's doing in your code. You want either a two-dimensional array (which you have, it's array), or a pointer-to-pointer (which in reality argv is). Just remove ptr_array from the code and use array instead. printf("%s\n", array[1]); should work fine. (Also, it seems you are confusing strings and individual characters – please read a basic tutorial on C strings and characters, and also read the man page for printf().) Commented Feb 8, 2015 at 8:35
  • 1
    I will do as advised. regards. and appreciate your time. Commented Feb 8, 2015 at 8:39
  • 1
    But one question, Why do we use *argv inside the int main (int argc , char * argv)? If argc has all properties of an array I do not understand why we are using argv as a pointer array. Commented Feb 8, 2015 at 8:40
  • 1
    @deeplglicious What do you mean by "argc has all properties of an array"? It doesn't. It's just an int. Neither does argv, since it's not an array, it's a pointer-to-pointer. And we are using it like this because the language specification says so (it's the most practical way of getting command line arguments, since their length is unknown at compile time, so one can't really say that argv should be a pointer to array of known size.) Commented Feb 8, 2015 at 8:42

4 Answers 4

2

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

there's no need for creating a separate array of pointers. Simply printing array[i] would work because array[i], which is an array, decays into a pointer.
@TheParamagneticCroissant True, but the whole point of the question was how to create an array of pointers similar to the argv array that main has. So the array[20][20] is just the backing store that enables ptr_array without a bunch of mallocs. The only thing that OP cares about is the ptr_array, because that's the array-of-pointer-to-char like argv.
1

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

2 Comments

The strings pointed to by argv are writable so this does not quite achieve OP's requirements
@MattMcNabb Yes yes .. I just showed what is *argv[] Will include your comment in my answer
1

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.

5 Comments

this is a mere effort made to understand the application of argv. I do not understand why we use argv to be a pointer array. Why is it defined to be used as one? why couldnt we use it to be a notmal char argv instead of coding it to be a char *argv?
@deepIglicious if it were a 2-D array you'd have to specify a maximum length for each row which is unnecessarily restrictive and wastes a huge amount of memory
@deepIglicious Made an edit to clear that out. Does that answer your questions?
@deepIglicious Added some more explanation.
This is wrong on so many levels. You are conflating pointers and arrays continuously and repeatedly. "argv is an array of char arrays" – no, it's a pointer-to-pointer. That's different from an array-of-arrays. then: "array is now a pointer to the first element in that array, thus it is a char*" – no, it's an array of 10 characters, or a char [10]. It may decay into a pointer to its first element, but it's not a pointer by itself.
0

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.