Connecting Tech Pros Worldwide Help | Site Map

pointers to pointers question

Chad
Guest
 
Posts: n/a
#1: Mar 26 '06
How come something like:

#include <stdio.h>

int main(int argc, char **argv)
{
char **arg;
int i;

for ( i = 0; argv[i] != NULL; ++i )
{
puts(argv[i]);
}

for ( arg = argv; *arg != NULL; ++arg )
{
puts(*arg);
}
return 0;
}

doesn't require a explicit & before argv in the for loop, but something
like

#include <stdio.h>
#include <stdlib.h>

int main(void) {
char *ptr = "Garbage";

char **pptr;
pptr = &ptr;
printf("The value is: % s\n", *pptr);
}

does require an explict &

Chad

santosh
Guest
 
Posts: n/a
#2: Mar 26 '06

re: pointers to pointers question


Chad wrote:[color=blue]
> How come something like:
> #include <stdio.h>
> int main(int argc, char **argv) {
> char **arg;
> int i;
> for ( i = 0; argv[i] != NULL; ++i )
> {
> puts(argv[i]);
> }
> for ( arg = argv; *arg != NULL; ++arg )
> {
> puts(*arg);
> }
> return 0;
> }
>
> doesn't require a explicit & before argv in the for loop, but something
> like
> #include <stdio.h>
> #include <stdlib.h>
> int main(void) {
> char *ptr = "Garbage";
> char **pptr;
> pptr = &ptr;
> printf("The value is: % s\n", *pptr);
> }
>
> does require an explict &[/color]

Well, in the first case puts() expects a pointer to a null terminated
array of char and the expression supplies it, while in the second
example, you'll need the *address* of the pointer, not it's value, to
initialise pptr and hence & operator must be used.

santosh
Guest
 
Posts: n/a
#3: Mar 26 '06

re: pointers to pointers question


Chad wrote:[color=blue]
> How come something like:
>
> #include <stdio.h>
>
> int main(int argc, char **argv)
> {
> char **arg;
> int i;
>
> for ( i = 0; argv[i] != NULL; ++i )
> {
> puts(argv[i]);
> }
>
> for ( arg = argv; *arg != NULL; ++arg )
> {
> puts(*arg);
> }
> return 0;
> }
>
> doesn't require a explicit & before argv in the for loop, but something
> like
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void) {
> char *ptr = "Garbage";
>
> char **pptr;
> pptr = &ptr;
> printf("The value is: % s\n", *pptr);
> }
>
> does require an explict &[/color]

In C arr[n] is converted by the compiler to 'arr + n' and the resulting
address is deferenced. In the second example, you want the address of
the pointer itself. If you had written:
pptr = *ptr;
the compiler would warn about casting an int to pointer type but would
go ahead and compile. However, during runtime the value stored at the
address pointed to by ptr, (which in this case is the code for 'G'),
would be loaded into pptr after any necessary conversion. When you
deference pptr, your program will invoke undefined behaviour and will
most likely will crash or be terminated.
Chad
Guest
 
Posts: n/a
#4: Mar 27 '06

re: pointers to pointers question



santosh wrote:[color=blue]
> Chad wrote:[color=green]
> > How come something like:
> >
> > #include <stdio.h>
> >
> > int main(int argc, char **argv)
> > {
> > char **arg;
> > int i;
> >
> > for ( i = 0; argv[i] != NULL; ++i )
> > {
> > puts(argv[i]);
> > }
> >
> > for ( arg = argv; *arg != NULL; ++arg )
> > {
> > puts(*arg);
> > }
> > return 0;
> > }
> >
> > doesn't require a explicit & before argv in the for loop, but something
> > like
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > int main(void) {
> > char *ptr = "Garbage";
> >
> > char **pptr;
> > pptr = &ptr;
> > printf("The value is: % s\n", *pptr);
> > }
> >
> > does require an explict &[/color]
>
> In C arr[n] is converted by the compiler to 'arr + n' and the resulting
> address is deferenced. In the second example, you want the address of
> the pointer itself. If you had written:
> pptr = *ptr;
> the compiler would warn about casting an int to pointer type but would
> go ahead and compile. However, during runtime the value stored at the
> address pointed to by ptr, (which in this case is the code for 'G'),
> would be loaded into pptr after any necessary conversion. When you
> deference pptr, your program will invoke undefined behaviour and will
> most likely will crash or be terminated.[/color]

Hmmm..... fascinating. Normally it would have taken me a good 3 days to
make heads or tails out of the responses. I think this has to be the
first time I've actually been able to understand a response in less
than 5 hours.

zhousqy@gmail.com
Guest
 
Posts: n/a
#5: Mar 27 '06

re: pointers to pointers question


the type of pptr is "char **", but the type of "ptr" is "char *", so
the & before "ptr" is needed.

Default User
Guest
 
Posts: n/a
#6: Mar 27 '06

re: pointers to pointers question


zhousqy@gmail.com wrote:
[color=blue]
> the type of pptr is "char **", but the type of "ptr" is "char *", so
> the & before "ptr" is needed.[/color]

That's nice. Too bad it's impossible to tell why you're saying this.
See below for valuable information.



Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Closed Thread