How is argv a string array when it's a char array?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How is argv a string array when it's a char array?



Why doesn't argv[0] print the first character of the filename instead of the whole filename string?


argv[0]



If argv is a pointer to an array of chars, then shouldn't accessing it with [n] result with a char? If its a string (as printf(argv[n]) suggests) then why doesn't argv[0][0] get me the first char of filename (compiles but crashes when started)?


argv


[n]


printf(argv[n]


argv[0][0]


int main(int argc, char **argv)

printf(argv[0][0]);

while (1)
return 0;





Because argv is a pointer to an array of char pointers.
– Weather Vane
Aug 10 at 12:01


argv


char





A char ** can be accessed via two indexing operators like a 2D array (argv[0][0]), but a 2D array is an array of arrays, not an array of pointers. You can find several questions already addressing this here on SO.
– John Bollinger
Aug 10 at 12:06


char **


argv[0][0]





@LukaKostic The compiler doesn't care. That's why undefined behavior exists. You make a contract with yourself about that too. The notation char *argv is simply to indicate to the reader that the intended parameter is indeed an array of pointers, but in the end it's interpreted as char **argv anyway and the compiler really doesn't care.
– Iharob Al Asimi
Aug 10 at 12:16


char *argv


char **argv





@LukaKostic There's a good lesson here: code speaks louder than words. Coding is all about the details, which a written out description misses. In this case, you are conceptually correct that argv[0][0] is the first char of the file name. The problem is that you're not printing that character correctly. Always post code.
– John Kugelman
Aug 10 at 12:22


argv[0][0]





@LukaKostic argv[0][0] by itself doesn't crash. It's the way you used it, that's why the code is so important.
– Iharob Al Asimi
Aug 10 at 12:23


argv[0][0]




2 Answers
2



Your code exhibits undefined behavior because you are passing a char to a functions that expects a pointer.


char



To print a single character you have these options,


fputc(argv[0][0], stdout);


putchar(argv[0][0]); // Effectively the same as above


printf("%cn", argv[0][0]);



you can add more of printf() variants.


printf()



The reason your code crashes, is because printf(argv[0][0]); is undefined behavior since the function will try to dereference a pointer but you passed a single character and the value of such character will be interpreted as a memory address.


printf(argv[0][0]);



You really NEED to enable compiler warnings.





How can it get confused about the address? Isnt &argv[0][0] the address and argv[0][0] the char?
– Luka Kostic
Aug 10 at 12:42





@LukaKostic In the end, char is just an integral type, integers and pointers are interchangeable in c but the behavior of such trade is not always defined. Moreover, if the address was one that is in the range of the memory of your program, it would still be invalid because it's not pointing to a real memory position, or is it? That would not be known beforehand and that's why this is called undefined behavior.
– Iharob Al Asimi
Aug 10 at 12:43


char





Clearly, pointers and integers are not interchangable in C, but they are interconvertible. And some compilers perform such conversions even without a cast, which is an extension (though they are typically willing to emit a warning about that).
– John Bollinger
Aug 10 at 13:10



I am using ubuntu server 14 to execute, and gcc compiler to compile the code and argv[n][n] worked for me. Take a look at this code


gcc


argv[n][n]


// C program to illustrate
// command line arguments
#include<stdio.h>

int main(int argc, char* argv)

int counter;
printf("Program Name Is: %s",argv[0]);
if (argc == 1)
printf("nNo Extra Command Line Argument Passed Other Than Program Name");
if (argc >= 2)

printf("nChecking double array elements: %c",argv[0][0]);

return 0;



Output:


Program Name Is: ./test
Checking double array elements: .



What machine are you using to compile and execute? maybe there is some other error... double check!





I needed a printf("%c",argv[0][0]) instead of just printf(argv[0][0])
– Luka Kostic
Aug 10 at 12:38





Yes exactly, if you need to read a char from a string (array of char) you can do it by %c
– Umair
Aug 10 at 13:14


char


char


%c






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard