Skip to content

Instantly share code, notes, and snippets.

@siv2r
Created December 4, 2021 03:32
Show Gist options
  • Save siv2r/d589ac584b9b8cb7dc8028c78bd775b0 to your computer and use it in GitHub Desktop.
Save siv2r/d589ac584b9b8cb7dc8028c78bd775b0 to your computer and use it in GitHub Desktop.
argv vs 2d char array
#include<stdio.h>
int main(int argc, char** argv){
// argv
int i;
for(i=0; i<argc; i++) {
printf("argv : %s (val), ", argv);
printf("argv : %x (addr)\n", argv);
printf("*argv: %s (val), ", *argv);
printf("*argv: %x (addr)\n", *argv);
printf("argv incremented...\n");
argv++;
if(*(argv) == NULL) {
printf("*(argv) val is null\n");
}
if((argv) == NULL) {
printf("argv val is null\n");
}
}
// 2d char array
char arr[][5] = {"hell", "you"};
int sz = sizeof(arr)/sizeof(arr[0]);
for(i = 0; i < sz; i++) {
printf("arr+%d : %s (val), ", i, arr+i);
printf("arr+%d : %x (addr)\n", i, arr+i);
printf("*(arr+%d): %s (val), ", i, *(arr+i));
printf("*(arr+%d): %x (addr)\n", i, *(arr+i));
}
return 0;
}
This is the output of the program above.
argv : ޿4�� (val), argv : bc349f18 (addr)
*argv: ./a.out (val), *argv: bc34bfde (addr)
argv incremented...
argv : �4�� (val), argv : bc349f20 (addr)
*argv: arg1 (val), *argv: bc34bfe6 (addr)
argv incremented...
*(argv) val is null
arr+0 : hell (val), arr+0 : bc349e0e (addr)
*(arr+0): hell (val), *(arr+0): bc349e0e (addr)
arr+1 : you (val), arr+1 : bc349e13 (addr)
*(arr+1): you (val), *(arr+1): bc349e13 (addr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment