473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

pointers as parameters

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.



Nov 29 '07 #1
6 1170
On Nov 29, 2:21 pm, mdh <m...@comcast.netwrote:
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.
Nov 29 '07 #2
mdh
On Nov 29, 3:29 pm, user923005 <dcor...@connx.comwrote:
On Nov 29, 2:21 pm, mdh <m...@comcast.netwrote:
>
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?
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?
Nov 29 '07 #3
mdh wrote:
>
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.
Nov 30 '07 #4
mdh
On Nov 29, 4:21 pm, Mark McIntyre <markmcint...@spamcop.netwrote:
mdh wrote:

If there is no difference in the
"actual" pointers of different types ....snip

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

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

Nov 30 '07 #5
Mark McIntyre wrote:
mdh wrote:
>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.
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".

--
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
CBFalconer <cb********@yahoo.comwrites:
Mark McIntyre wrote:
>mdh wrote:
>>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.

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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 30 '07 #7

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

Similar topics

42
by: x-pander | last post by:
Is is guaranteed, that a pointer to any object in C points exactly at the lowest addressed byte of this object? Specifcally is it possible for any platform/os/compiler combination that: (char...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
13
by: munni | last post by:
hi i want to write a program on function pointers where i have not written any programs using function pointers till now. i want to take an array of 26 functions which r to be pointed by this...
5
by: Dinesh Kumar | last post by:
Hi all I am using VB.NET for a Connector dll in Delphi client and some webservice . can you tell me how to handle pointers in Vb.net which are passed by delphi client as parameters in function...
9
by: Sidhu | last post by:
Hai, I like 2 know more abot pointers. Can you please tell me the fields in which pointers are widely used. Yours Sidhu
5
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
19
by: MQ | last post by:
Can someone tell me where I should use pointers and where I should use references? In his book, Stroustrup says that you should use pointers for passing arguments that are to be modified, not...
33
by: pateldm15 | last post by:
How do I sort an string array using pointers
2
by: Rahul | last post by:
Hi, I planning to replace the switch-case with an array of function pointers, the index for the array would be the case integers. Each case block would be implemented as a seperate function. It...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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...

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.