Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   pointers to pointers question (http://www.velocityreviews.com/forums/t441992-pointers-to-pointers-question.html)

Chad 03-26-2006 09:24 PM

pointers to pointers question
 
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 03-26-2006 09:33 PM

Re: pointers to pointers question
 
Chad wrote:
> 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 &


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 03-26-2006 09:52 PM

Re: pointers to pointers question
 
Chad wrote:
> 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 &


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 03-27-2006 01:57 AM

Re: pointers to pointers question
 

santosh wrote:
> Chad wrote:
> > 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 &

>
> 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.


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 03-27-2006 06:40 AM

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 03-27-2006 07:44 AM

Re: pointers to pointers question
 
zhousqy@gmail.com wrote:

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


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.


All times are GMT. The time now is 02:10 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.