Inconsistent

E

Emmanuel Delahaye

Flash Gordon a écrit :
The problems I see with the term address are:
After all, in summary, C pointers are slightly more abstract than
machine addresses.
I think it would be better if the term "address" was not used, expect

May be the good wording should be 'reference' ?

"A reference is a (unique ?) value that identifies the memory location
of an object."

"NULL is a reference constant meaning invalid reference."

"A pointer is a variable that holds a reference to an object or NULL."
or:
"A pointer is a variable that holds the reference of an object or NULL."

"When a pointer is typed, the dereference operator (*) allows a read of
write acces to the referenced object."

etc. I'm not going to rewrite the C-standard, but I think that this kind
of wording could be used in C-books and tutorials for consistency.
 
F

Flash Gordon

pemo said:
Flash Gordon said:
jimbo wrote:
and it seems as though again that C is incosistent, i.e., you
don't have to declare testFunction's parameter as char *. Surely, the
compiler should know that char [] someFunction actually means char *
someFunction??
If both were valid they would mean different things. One would mean
returning an array, which C does not allow, whilst the other does mean
returning a pointer to char.

"If both were valid they would mean different things"

Hmmmm

Suerly, there is an inconsistency here with char ?[]

OK, I'll admit the standard is inconsistent and that it *could* define s
function returning char [] as being the same one returning char *, i.e. make
char []func(void);
and
char *func(void);
the same.

My opinion is that this would only add to the confusion. A reason for
not allowing it is because:

char *pointless_func(char derf[])
{
return derf;
}

int main(void)
{
char fred[20] = "char array";
char another_array[20];
char *ptr;
ptr = pointless_func(fred); /* this is legal */
another_array = pointless_func(fred); /* this is illegal */
}

The first is an example where usage looks like definition, because the
definition looks like it takes an array, and that is probably why "char
derf[]" is allowed. The second would be usage looking like definition if
char []pointless_func(char derf[])
{
return derf;
}
was allowed, but the usage would still not be allowed.

So a parameter of char[] means you can pass a char array to the function.
For a return type of char[] to mean you could assign it to an array (the
obvious usage) it would mean it would require different semantics to a
return type of char*.
So my opinion is that it would just add further to the confusion.
 
F

Flash Gordon

Emmanuel said:
Flash Gordon a écrit :



May be the good wording should be 'reference' ?

<snip examples>

The problem with that is that I believe that other languages have used
the term "reference" to mean something else, so you would confuse people
coming from such a language to C or going from C to such a language.

I really don't see the problem with referring to a function returning a
pointer, or if you want to avoid confusion with pointer variables then
talking about 'pointer values'. IMHO it keeps it clearly tied to the
pointer type, and I consider that to be a good thing.

As I say, I'm not going to object to others using the word address,
after all the standard refers to, "the result points to the lowest
addressed byte of the object." when talking about converting to a char
pointer and you have the "address operator" etc.
 
K

Keith Thompson

buda said:
Keith Thompson said:
The indexing operator x[y], takes two operands, a pointer and an
integer (*not* an array and an integer). Typically, the pointer
operand is going to be an array name, which will be implicitly
converted to a pointer before the indexing operator sees its value.
x[y] is by definition equivalent to *(x+y). (Because addition is
commutative, x[y] can be written as [y]x; this odd little quirk is in
no way useful.) ^^^^

ITYM "x[y] can be written as y[x]". For example, "123"[1] is equivalent to
1["123"], but [1]"123" is a syntax error.

Yes, that's what I meant. Thanks for the correction.
 
K

Keith Thompson

Emmanuel Delahaye said:
Flash Gordon a écrit :



May be the good wording should be 'reference' ?

"A reference is a (unique ?) value that identifies the memory location
of an object."
[snip]

C++ has things it calls references, and they're not pointers or
addresses.
 
K

Keith Thompson

Emmanuel Delahaye said:
Flash Gordon a écrit :

There is no 'null pointer'. There is a 'null pointer constant' also
known as NULL.

Sorry, but there are at least two major errors there.

There most certainly is a null pointer. It's defined in C99
6.3.2.3p3; Flash Gordon quoted the definition in another response.
(The term "null pointer" is in italics, so that's *the* definition of
the term.)

A null pointer constant is not "also known as" NULL. A null pointer
constant is a syntactic construct (also defined in 6.3.2.3p3), which
occurs only in source code. NULL is a macro that expands to some
specific implementation-defined null pointer constant. A null pointer
is a value that exists at run time. (It can result from things other
than a null pointer constant, which means the standard's definition of
"null pointer" is incomplete.)
 
O

Old Wolf

Flash said:
OK, I'll admit the standard is inconsistent and that it *could*
define s function returning char [] as being the same one
returning char *, i.e. make
char []func(void);
and
char *func(void);
the same.

That's not how array declarations work in C:
char x[9]; /* correct */
char[9] x; /* wrong */

The syntax for a function returning an array of char would be:
char func(void) [];

(but the C standard explicitly says that returning an array is
illegal).
pemo said:
Suerly, there is an inconsistency here with char ?[]

Two wrongs don't make a right -- it would have been
better if the [] syntax in function parameters were
never permitted.
 
T

Tim Rentsch

Flash Gordon said:
The problems I see with the term address are:
1) If you talk about returning an address you either imply that a null
pointer constant is an address leading to confusion about what you
can do with it, or continually say "address or null pointer" (or
something similar.
2) You are likely to confuse people about what they cad do. IMHO that
since address is a term used when talking about the hardware where no
type is involved (it is just memory), where as pointers are not.

After all, in summary, C pointers are slightly more abstract than
machine addresses.

A pointer holding a null pointer value still holds an address. That
the address might not refer to any actual memory location, or might
not be usable to access memory (because of memory protection, etc),
doesn't make it not an address. There are machine architectures with
each of these properties.

I realize that the comments above just represent a point of view and
can't be called "right" or "wrong" (at least, not by reasonable
persons :). However, that view is one I was used to even before
learning C, and it simplifies the discussion and understanding of C,
so I think it's worth promulgating. Pointer values in C correspond
pretty well to the notion of "address", considered in a broad context
of various machine architectures. IMO, anyway.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top