pointers to pointers question

C

Chad

How come something like:

#include <stdio.h>

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

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

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
 
S

santosh

Chad said:
How come something like:
#include <stdio.h>
int main(int argc, char **argv) {
char **arg;
int i;
for ( i = 0; argv != NULL; ++i )
{
puts(argv);
}
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.
 
S

santosh

Chad said:
How come something like:

#include <stdio.h>

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

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

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

Chad

santosh said:
Chad said:
How come something like:

#include <stdio.h>

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

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

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

zhousqy

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

Default User

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C pipe 1
Command Line Arguments 0
Help with pointers 1
More on pointers to pointers. 6
Pointers 16
Print with command-line arguments 0
Adding adressing of IPv6 to program 1
Fibonacci 0

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top