(again :))
Hello everyone.
I'll ask this even at risk of being accused of not researching adequately.
My question (before longer reasoning) is: How does declaring (or defining,
whatever) a variable **var make it an array of pointers?
I realize that 'char **var' is a pointer to a pointer of type char (I hope).
And I realize that with var[], var is actually a memory address (or at
least as it is represented by C, IIRC (an internal copy which is a fixed
pointer)) pointing (permanently) to the first element of an array. And I
realize that *var[] is an array of pointers where each pointer can point to
the beginning of a string (or whatever). But then there is **var. How
does that then become an array of pointers?
Hmmm. It's coming to me. Wait. So we declare 'char **var'. *var
is/contains a memory address of size char, which could point to the
beginning of a string (**var). (?) *var+1 would be the next char memory
address, which could point to a string (*(*var+1)) (moving char bytes
through memory (1)). That's my hangup. How is the **argv structure
formed? Are the arguments added, then memory allocated for that many, then
dividing them up across the argv variable? Because I've learned to accept
that **argv points to a string, *(*argv+1) points to the next, etc. (Are
the () necessary? Am I even right?) But how does it get that way? I feel
like I'm almost to a satori experience with this aspect of pointers (which
would be nice :)), but there's something holding me back (my mind maybe?).
I think I just need to get a grasp of the mechanics behind the creation of
argv. (Don't ask; whenever I'm studying pointers I get stuck on these
issues and I can't stop thinking about how, so I become unable to wrap my
head around it)
Where does the program store the arguments before putting them in argv? Is
there a buffer it puts each argument in, then copies it into argv? It's
driving me crazy. (Similar to how passing a pointer to printf with %s
(char *str = "Confused";prin tf("%s", str);) is the same as a string. How
does it (the compiler, program, ??) know? I then figured when it receives
the memory address, expecting a string, it dereferences the pointer,
traversing it until it gets a '\0'? Close?) Although it actually just hit
me that if I were to pass a normal string variable (char str[6] = "idiot")
as 'printf("hello, %s", str)' then str is actually a pointer to the first
element of str[6]. Ahhhh... :)
I realize that perhaps the argv example is implementation specific and not
topical. Perhaps you could imagine a similar situation, i.e. passing a
**var in a function that is in fact an array of pointers. Is the **var
construction often used without being an array of pointers? Also, why is
it technically more accurate to define argv as **argv and not *argv[]?
(according to a book I have, Linux Programming by Example).
Please excuse the rambling. I know I'm not being very clear. There's a
reason for that; hence the post :). Thanks for any help or guidance, and
patience.
-jab3
Hello everyone.
I'll ask this even at risk of being accused of not researching adequately.
My question (before longer reasoning) is: How does declaring (or defining,
whatever) a variable **var make it an array of pointers?
I realize that 'char **var' is a pointer to a pointer of type char (I hope).
And I realize that with var[], var is actually a memory address (or at
least as it is represented by C, IIRC (an internal copy which is a fixed
pointer)) pointing (permanently) to the first element of an array. And I
realize that *var[] is an array of pointers where each pointer can point to
the beginning of a string (or whatever). But then there is **var. How
does that then become an array of pointers?
Hmmm. It's coming to me. Wait. So we declare 'char **var'. *var
is/contains a memory address of size char, which could point to the
beginning of a string (**var). (?) *var+1 would be the next char memory
address, which could point to a string (*(*var+1)) (moving char bytes
through memory (1)). That's my hangup. How is the **argv structure
formed? Are the arguments added, then memory allocated for that many, then
dividing them up across the argv variable? Because I've learned to accept
that **argv points to a string, *(*argv+1) points to the next, etc. (Are
the () necessary? Am I even right?) But how does it get that way? I feel
like I'm almost to a satori experience with this aspect of pointers (which
would be nice :)), but there's something holding me back (my mind maybe?).
I think I just need to get a grasp of the mechanics behind the creation of
argv. (Don't ask; whenever I'm studying pointers I get stuck on these
issues and I can't stop thinking about how, so I become unable to wrap my
head around it)
Where does the program store the arguments before putting them in argv? Is
there a buffer it puts each argument in, then copies it into argv? It's
driving me crazy. (Similar to how passing a pointer to printf with %s
(char *str = "Confused";prin tf("%s", str);) is the same as a string. How
does it (the compiler, program, ??) know? I then figured when it receives
the memory address, expecting a string, it dereferences the pointer,
traversing it until it gets a '\0'? Close?) Although it actually just hit
me that if I were to pass a normal string variable (char str[6] = "idiot")
as 'printf("hello, %s", str)' then str is actually a pointer to the first
element of str[6]. Ahhhh... :)
I realize that perhaps the argv example is implementation specific and not
topical. Perhaps you could imagine a similar situation, i.e. passing a
**var in a function that is in fact an array of pointers. Is the **var
construction often used without being an array of pointers? Also, why is
it technically more accurate to define argv as **argv and not *argv[]?
(according to a book I have, Linux Programming by Example).
Please excuse the rambling. I know I'm not being very clear. There's a
reason for that; hence the post :). Thanks for any help or guidance, and
patience.
-jab3
Comment