Re: pointer and array
On Sun, 19 Dec 2004 19:39:02 +0000, Joona I Palaste wrote:
[color=blue]
> Leon Brodskiy <ivan@rogers.co m> scribbled the following:[color=green]
>> If we leave char array pointer for a second, can we say that the program
>> will always work correctly?[/color]
>
> No. The C standard allows for an implementation to behave erroneously
> when a pointer value is assigned to a variable of incompatible type.[/color]
A compiler can refuse to compile the program, indeed this is a reasonable
thing for a compiler to do in this case.
[color=blue][color=green]
>> Would it be correct to say that a difference between str and &str is
>> that str is char* (points to one single character) and &str is a
>> pointer to an array(points to array of 10 characters)?[/color]
>
> Yes.
>[color=green]
>> If this is a true then in the
>> program should not be any difference if we use &str or str - both of
>> them will send to the function the address of the first element in the
>> array.[/color][/color]
No, the value of str is a pointer to the first element of the array, &str
is a pointer to the array as a whole. There is no requirement that these
two types of pointer have the same representation or are passed in the
same way in a function call. Often things happen to work, but that is not
the same thing as the source code being correct.
[color=blue]
> That is not true in the general case. At least I think it's not. Maybe
> some of the real C gurus here can answer my original question about
> whether it is undefined behaviour?[/color]
The program contains a constraint violation which means that it isn't a
valid C program. So issues like behaviour (from a C language
perspective) don't apply. OTOH since it isn't a valid C program a C
compiler can do what it likes with it after issuing the required
diagnostic. So from that perspective it is very much like undefined
behaviour.
Lawrence
On Sun, 19 Dec 2004 19:39:02 +0000, Joona I Palaste wrote:
[color=blue]
> Leon Brodskiy <ivan@rogers.co m> scribbled the following:[color=green]
>> If we leave char array pointer for a second, can we say that the program
>> will always work correctly?[/color]
>
> No. The C standard allows for an implementation to behave erroneously
> when a pointer value is assigned to a variable of incompatible type.[/color]
A compiler can refuse to compile the program, indeed this is a reasonable
thing for a compiler to do in this case.
[color=blue][color=green]
>> Would it be correct to say that a difference between str and &str is
>> that str is char* (points to one single character) and &str is a
>> pointer to an array(points to array of 10 characters)?[/color]
>
> Yes.
>[color=green]
>> If this is a true then in the
>> program should not be any difference if we use &str or str - both of
>> them will send to the function the address of the first element in the
>> array.[/color][/color]
No, the value of str is a pointer to the first element of the array, &str
is a pointer to the array as a whole. There is no requirement that these
two types of pointer have the same representation or are passed in the
same way in a function call. Often things happen to work, but that is not
the same thing as the source code being correct.
[color=blue]
> That is not true in the general case. At least I think it's not. Maybe
> some of the real C gurus here can answer my original question about
> whether it is undefined behaviour?[/color]
The program contains a constraint violation which means that it isn't a
valid C program. So issues like behaviour (from a C language
perspective) don't apply. OTOH since it isn't a valid C program a C
compiler can do what it likes with it after issuing the required
diagnostic. So from that perspective it is very much like undefined
behaviour.
Lawrence
Comment