473,800 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer as argument and parameter

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.

(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...w hich is more than likely.

Thanks in advance.



Nov 29 '07 #1
5 1665
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 helpful 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 helps.

--
Er*********@sun .com
Nov 29 '07 #2
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

Nov 30 '07 #3
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
es*****@ieee-dot-org.invalid
Nov 30 '07 #4
mdh
On Nov 29, 2:56 pm, Eric Sosman <Eric.Sos...@su n.comwrote:
>
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 helps.

Yes indeed it helps 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.

Nov 30 '07 #5
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

Nov 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1859
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in include files: typedef Context *Context_ptr; typedef ObjOut<Context> Context_out;
110
9969
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
34
440
by: wilson | last post by:
Hi All, I am a novice at C and just have learned pointer for a short period. Today one error had occured while executing my program below. And I cannot find the error out since it's checked "OK" by Dev C++. Here is:(I want to exchange the value of "a" and "b" with function "swap") void swap(int *pa, int *pb) {
9
5301
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer from integer of different size. Now I thought that when it was a void I could pass anything? Thing is it works when I use an int, but in this case I wanted to use a char. It wouldnt be hard to work around it, but it annoys me because I've heard...
204
13140
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
18
1618
by: hzmonte | last post by:
typedef int t_compare_func(const void *, const void *); struct node *tree_search(struct node *root, const void *keyy, t_compare_func *comp) { struct node *cur_item; int result; if (root == NULL) return NULL; cur_item = root; while (cur_item != NULL) {
2
8291
by: David Rose | last post by:
I have a DLL (not .NET) that takes a function pointer argument and calls that function with an integer argument. The DLL is being called from C#. So far, it is partially working, but the integer argument is getting corrupted (MessageBoxes in the dll and C# fire). I do not understand why the int parameter is getting corrupted calling the callback function (Testing() in C#). ( probably I just don't understand
8
2237
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
29
3656
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
20
2186
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then suddenly I can't use sizeof(array) / sizeof(array) anymore within that function ? Help - What point am I missing ?
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10035
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.