strange calling convention?

  • Thread starter Friedrich Dominicus
  • Start date
F

Friedrich Dominicus

My first thought was -- strange -- but while lookng at it it has some
merits. The code looks like this


char *foo = "abcdef";

and we have a function which the following signature


void bar(char* some_foo) {

}

Then the code was called like this:

bar(&foo[0]);

I thought, why would anyone write that code instead of
bar(foo);

but after a while I was thinking it's quite good at documentation. the
& suggests that we are talking about some address, this information is
not that obvious while calling the later form.

What do you think?

Regards
Friedrich
 
B

Bill Leary

Friedrich Dominicus said:
My first thought was -- strange -- but while lookng at it it has some
merits. The code looks like this

char *foo = "abcdef";

and we have a function which the following signature

void bar(char* some_foo) {
}

Then the code was called like this:

bar(&foo[0]);

I thought, why would anyone write that code instead of
bar(foo);

but after a while I was thinking it's quite good at documentation. the
& suggests that we are talking about some address, this information is
not that obvious while calling the later form.

What do you think?

I often use the "&foo[0]" form, generally in cases where I want to make it
very clear I'm talking about the address of the start of an array of
something. That is, cases where it's not absolutely clear that "foo" is an
array. So, yeah, it's documentation. On the other hand, I originally
started doing this long ago when I was dealing with a compiler which
sometimes didn't parse the simple "foo" correctly. Under the right (wrong)
circumstances rather than getting the address of "foo," it delivered the
contents of the first element of the "foo" array. I got into the habit of
being explicit in self defense.

- Bill
 
V

vippstar

message

My first thought was -- strange -- but while lookng at it it has some
merits. The code looks like this
char *foo = "abcdef";
and we have a function which the following signature
void bar(char* some_foo) {
}
Then the code was called like this:
bar(&foo[0]);

I thought, why would anyone write that code instead of
bar(foo);
but after a while I was thinking it's quite good at documentation. the
& suggests that we are talking about some address, this information is
not that obvious while calling the later form.
What do you think?
Not bad nor annoying, I still prefer 'foo' because it's smaller to
type, moreover there are modern tools that will, AFAIK, show you the
exact type of the object when you type it, which however does not
apply when you read the code and you don't write it.
Ofcourse, then there's this problem: bar(&foo);
Is foo a scalar/non-pointer or aggregate/pointer?
Because of all this, I always prefer to type as less as possible and
let the programmer use his favorite tools (if any) to assist him.
I often use the "&foo[0]" form, generally in cases where I want to make it
very clear I'm talking about the address of the start of an array of
something. That is, cases where it's not absolutely clear that "foo" is an
array. So, yeah, it's documentation. On the other hand, I originally
started doing this long ago when I was dealing with a compiler which
sometimes didn't parse the simple "foo" correctly. Under the right (wrong)
circumstances rather than getting the address of "foo," it delivered the
contents of the first element of the "foo" array. I got into the habit of
being explicit in self defense.
I think I'm happy I wasn't interested in programming back then. C must
be hell-ish with a buggy compiler.. (or rather, with no standard)
 
R

Richard Bos

I think I'm happy I wasn't interested in programming back then. C must
be hell-ish with a buggy compiler.. (or rather, with no standard)

To be fair, no more so than programming in BASIC when there was no
standard that anyone kept to. Or Pascal, for that matter.

Richard
 
F

Flash Gordon

Friedrich Dominicus wrote, On 01/04/08 08:53:

bar(&foo[0]);

I thought, why would anyone write that code instead of
bar(foo);

but after a while I was thinking it's quite good at documentation. the
& suggests that we are talking about some address, this information is
not that obvious while calling the later form.

What do you think?

Personally I only use the &foo[0] form if *only* the first element will
be operated on rather than potentially the entire array.
 
C

CBFalconer

Richard said:
(e-mail address removed) wrote:
.... snip ...


To be fair, no more so than programming in BASIC when there was
no standard that anyone kept to. Or Pascal, for that matter.

Believe it or not, there were Pascals that met the standard. I was
about to release one in 1981 (for CP/M) when the PC changeover
hit. I was just too busy to do the rework for the PC, so it died.
So we just used it for our own embedded work.
 
R

Richard Bos

CBFalconer said:
Believe it or not, there were Pascals that met the standard.

Oh, certainly. But in most cases, you at least had (have?) to take the
ones which didn't into account.

Richard
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top