pointers as parameters

M

mdh

Hi all,
I have gone through the FAQ and done searches in the Comp.Lang and if
I have missed it please let me have the ref.

(Generated in part from page 119)

Given

char a[10];
char *b[8];

int foo(char *s, char *t[]);

Now, if in main, I do this:

int i;
i=foo(a, b);

it is my understanding that what is passed to "foo's", parameters, is

1) a pointer to a[0] of type "ptr to char".
2) a pointer to b[0] of type "ptr to ptr to char"

My question is this. If this is indeed correct, then from the
standpoint of the function foo, what is the difference in the two
pointers? From the programmer's standpoint, the difference is more
obvious. In the first case, *ptr yields a character, and in the second
place, *ptr yields a pointer to character. I am not sure if I am
framing this correctly.
Perhaps, if I ask this another way. Not knowing anything about how
the pointers were generated, if you were to be handed each pointer at
the level of the function, could you tell the difference. If you can
then my question is somewhat answered, if not then how does the ?
compiler know what to do with each pointer.
If the question is somewhat confusing, it is because I am not quite
sure what it is that I am missing.

Thanks in advance.
 
U

user923005

Hi all,
I have gone through the FAQ and done searches in the Comp.Lang and if
I have missed it please let me have the ref.

(Generated in part from page 119)

Given

char a[10];
char *b[8];

int foo(char *s, char *t[]);

Now, if in main, I do this:

int i;
i=foo(a, b);

it is my understanding that what is passed to "foo's", parameters, is

1) a pointer to a[0] of type "ptr to char".
2) a pointer to b[0] of type "ptr to ptr to char"

My question is this. If this is indeed correct, then from the
standpoint of the function foo, what is the difference in the two
pointers?

One holds the address of a character. The other holds the address of
an address.
From the programmer's standpoint, the difference is more
obvious. In the first case, *ptr yields a character, and in the second
place, *ptr yields a pointer to character. I am not sure if I am
framing this correctly.

Close enough.
Perhaps, if I ask this another way. Not knowing anything about how
the pointers were generated, if you were to be handed each pointer at
the level of the function, could you tell the difference.

Sure, if you are given the pointer type (and the type is not void *).
If you are handed a void pointer, then no.
If you can
then my question is somewhat answered, if not then how does the ?
compiler know what to do with each pointer.

It is the programmer who does things with the pointers, for the most
part. The compiler does exactly what you tell it to (whether it makes
sense or not). It may bark at you if you do something stupid, or not
-- depending on exactly what the thing was.
If the question is somewhat confusing, it is because I am not quite
sure what it is that I am missing.

Neither am I sure what you are missing.
 
M

mdh


char a[10];
char *b[8];
int foo(char *s, char *t[]);
Now, if in main, I do this:
int i;
i=foo(a, b);
it is my understanding that what is passed to "foo's", parameters, is
1) a pointer to a[0] of type "ptr to char".
2) a pointer to b[0] of type "ptr to ptr to char"
My question is this. If this is indeed correct, then from the
standpoint of the function foo, what is the difference in the two
pointers?

Ok...will let me then ask this. If there is no difference in the
"actual" pointers of different types that are passed to the function,
then is is correct to say that the reason one declares the pointer's
type as, a "pointer to char" or "pointer to pointer to char" or "ptr
to void" etc, is that during the compilation of the program, the
compiler is able to check the code and prevent errors at run time?
 
M

Mark McIntyre

mdh said:
Ok...will let me then ask this. If there is no difference in the
"actual" pointers of different types that are passed to the function,

Note that there _may_ in fact be a difference. Pointer representations
need not all be indistinguishable.
then is is correct to say that the reason one declares the pointer's
type as, a "pointer to char" or "pointer to pointer to char" or "ptr
to void" etc, is that during the compilation of the program, the
compiler is able to check the code and prevent errors at run time?

Yes.
 
M

mdh

mdh wrote:

If there is no difference in the

Note that there _may_ in fact be a difference. Pointer representations
need not all be indistinguishable.


Yes.

thanks Mark...a little lightbulb has just lit up for me in C.
 
C

CBFalconer

Mark said:
Note that there _may_ in fact be a difference. Pointer
representations need not all be indistinguishable.


Yes.

In addition, since the compiler knows the type, it can correctly
interpret such values as "sizeof *ptr" and also detect type
conflicts in such things as "*ptr1 = *ptr2".
 
K

Keith Thompson

CBFalconer said:
In addition, since the compiler knows the type, it can correctly
interpret such values as "sizeof *ptr" and also detect type
conflicts in such things as "*ptr1 = *ptr2".

And there's nothing special about pointer types in this regard; the
same thing happens with other types as well. For example, suppose int
and float are both 32 bits. There's no real distinction on the
machine level between an int object and a float object; they might
both be stored in exactly the same way. But the compiler knows the
type of each object, so that when you write "x + y" in your program,
it generates an add-integer or an add-float instruction as
appropriate. If you're writing in assembly language, you can just as
easily apply integer or floating-point operations to any chunk of
memory of the right size; in the machine code generated by a compiler,
you won't see floating-point operations on integer objects unless you
do some really ugly pointer-casting.
 

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

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top