PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > pointer as argument and parameter
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
pointer as argument and parameter

Réponse
 
LinkBack Outils de la discussion
Vieux 29/11/2007, 22h13   #1
mdh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut pointer as argument and parameter

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.

(Question generated in part by p119).


Given

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

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

Now, if in main, I do this:

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

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

1) a pointer to [ index 0] of type "pointer-to-char" (for a)
2) a pointer to index 0 of type "pointer to pointer to char" ( for
b)

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, I can 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.

Or...I am making this far too complicated...which is more than likely.

Thanks in advance.







  Réponse avec citation
Vieux 29/11/2007, 22h56   #2
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer as argument and parameter

mdh wrote On 11/29/07 17:13,:
> 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.
>
> (Question generated in part by p119).
>
>
> Given
>
> char a[10];
> char *b[8];
>
> int foo(char *s, char *[]);
>
> Now, if in main, I do this:
>
> int i;
> i=foo(a, b);
>
> it is my understanding that what is passed to "foo", as parameters,
> is
>
> 1) a pointer to [ index 0] of type "pointer-to-char" (for a)
> 2) a pointer to index 0 of type "pointer to pointer to char" ( for
> b)


Right. You may find it ful to imagine the call
as having been written `foo(&a[0], &b[0])', which means
exactly the same thing as `foo(a, b)'.

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


They point to different places (the beginning of `a'
and the beginning of `b'). Also, they point to different
kinds of things: one points to a `char' and one to a `char*'.
If you find "pointer to pointer" a confusing idea, just
think of it as "pointer to a Thing, where the Thing happens
to be a pointer to `char'".

> 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, I can 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.


Every pointer (in fact, every C expression) has a type,
and that type is determined at compile time and remains
fixed throughout the life of the program. An expression
that produces a `double' always produces a `double' and
never an `int' or a `struct midget'.

The type of a pointer is determined when you declare
it: If you tell the compiler that `p' is a `double*', the
compiler remembers what it was told. The compiler can
also reason about the types of expressions involving `p':
it knows that `*p' is a `double', that `p + 1' is a
`double*', that `&p' is a `double**', and so on. It works
this all out by starting from what you told it and applying
various rules.

If you tell the compiler that `b' is an array of `char*',
the same sort of thing goes on, except that the choice of
which rules to apply is a little different. Most of the
time, when you mention the name of an array you get a pointer
to the array's initial element -- hence the equivalence of
`b' and `&b[0]' in the function call. Since `b' is an
array of `char*', `b[0]' is one of those `char*' elements,
and `&b[0]' is therefore a `char**'.

When you write the function foo, part of your task is
to declare the types of the arguments it expects -- and when
you wrote `int foo(char *s, char *[]);' you communicated
this information to the compiler. The compiler remembers,
and knows that it must always provide one `char*' and one
`char**' as arguments when it calls foo. If you provide
argument expressions that aren't of these types (or can't
be converted to them automatically), the compiler complains.

Inside foo itself, the types of the function parameters
are whatever you said they were. Again, you tell the compiler
something, and it's the compiler's job to remember.

I hope this s.

--
Eric.Sosman@sun.com
  Réponse avec citation
Vieux 30/11/2007, 00h51   #3
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer as argument and parameter

Eric Sosman wrote:
>

.... snip ...
>
> If you tell the compiler that `b' is an array of `char*',
> the same sort of thing goes on, except that the choice of
> which rules to apply is a little different. Most of the
> time, when you mention the name of an array you get a pointer
> to the array's initial element -- hence the equivalence of
> `b' and `&b[0]' in the function call. Since `b' is an
> array of `char*', `b[0]' is one of those `char*' elements,
> and `&b[0]' is therefore a `char**'.


I disagree. b[0] _is_ a char. It happens to be of the same type
as what is pointed at by a char* pointer. However *b is not a
pointer to a char. Thus "&b[0]" is not a char**.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 30/11/2007, 02h28   #4
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer as argument and parameter

CBFalconer wrote:
> Eric Sosman wrote:
> ... snip ...
>> If you tell the compiler that `b' is an array of `char*',
>> the same sort of thing goes on, except that the choice of
>> which rules to apply is a little different. Most of the
>> time, when you mention the name of an array you get a pointer
>> to the array's initial element -- hence the equivalence of
>> `b' and `&b[0]' in the function call. Since `b' is an
>> array of `char*', `b[0]' is one of those `char*' elements,
>> and `&b[0]' is therefore a `char**'.

>
> I disagree. b[0] _is_ a char. It happens to be of the same type
> as what is pointed at by a char* pointer. However *b is not a
> pointer to a char. Thus "&b[0]" is not a char**.


Chuck, you blew it. If `b' is an array of `char*', as in

char *b[42];

.... then `b[0]' is clearly a `char*', not a `char'. If you
don't believe me, ask your compiler:

char c = b[0]; /* diagnostic required */

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 30/11/2007, 03h06   #5
mdh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer as argument and parameter

On Nov 29, 2:56 pm, Eric Sosman <Eric.Sos...@sun.com> wrote:

>
> Inside foo itself, the types of the function parameters
> are whatever you said they were. Again, you tell the compiler
> something, and it's the compiler's job to remember.
>
> I hope this s.
>



Yes indeed it s a lot. (Please excuse the double version of the
question...I thought I had successfully removed the first).
What you say makes a lot of things much clearer, and places the role
of the compiler in a new light for me.Thank you.

  Réponse avec citation
Vieux 30/11/2007, 04h59   #6
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pointer as argument and parameter

Eric Sosman wrote:
> CBFalconer wrote:
>> Eric Sosman wrote:
>> ... snip ...
>>> If you tell the compiler that `b' is an array of `char*',
>>> the same sort of thing goes on, except that the choice of
>>> which rules to apply is a little different. Most of the
>>> time, when you mention the name of an array you get a pointer
>>> to the array's initial element -- hence the equivalence of
>>> `b' and `&b[0]' in the function call. Since `b' is an
>>> array of `char*', `b[0]' is one of those `char*' elements,
>>> and `&b[0]' is therefore a `char**'.

>>
>> I disagree. b[0] _is_ a char. It happens to be of the same type
>> as what is pointed at by a char* pointer. However *b is not a
>> pointer to a char. Thus "&b[0]" is not a char**.

>
> Chuck, you blew it. If `b' is an array of `char*', as in
>
> char *b[42];
>
> ... then `b[0]' is clearly a `char*', not a `char'. If you
> don't believe me, ask your compiler:
>
> char c = b[0]; /* diagnostic required */


But you didn't define "char *b[42]'". Or so I thought, on
rereading. I thought I saw "char b[42];". I'm blowing a lot of
these things recently.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.


--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h11.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14583 seconds with 14 queries