You are forgetting that a string (char *
) is accessed via it's starting address, not an absolute value... What value would you store for a string? How much memory should the generic 'string' type use, and thus how long can a 'string' be?
A string in C consists of a number of printable characters, terminated by a nul (\0
). These are stored in memory.
The char *argv[]
parameter is specifying that there is an array of char *
elements. As we've established above, a char *
is a string (or an array of char
elements).
The char *argv[]
parameter can just as legally be declared char **argv
.
For example, you can print the absolute values of each character of a string, along with their address in memory:
#include <stdio.h>
int main(void) {
char *my_string = "Hello World";
int i;
for (i = 0; my_string[i] != '\0'; i++) {
printf("%c - 0x%02hhX - %p\n", my_string[i], my_string[i], &(my_string[i]));
}
return 0;
}
Here, we are dereferencing the pointer to get a single character by using my_string[i]
.
Output:
H - 0x48 - 0x400614
e - 0x65 - 0x400615
l - 0x6C - 0x400616
l - 0x6C - 0x400617
o - 0x6F - 0x400618
- 0x20 - 0x400619
W - 0x57 - 0x40061a
o - 0x6F - 0x40061b
r - 0x72 - 0x40061c
l - 0x6C - 0x40061d
d - 0x64 - 0x40061e
You could equally implement this program like so (note the dereferences):
#include <stdio.h>
int main(void) {
char *my_string = "Hello World";
char *s;
/* s = my_string
* is equivelant to:
* s = &(my_string[0]) */
for (s = my_string; *s != '\0'; s++) {
printf("%c - 0x%02hhX - %p\n", *s, *s, s);
}
return 0;
}