C – pointer variable is just a “symbol” with some operators?

Edit: The original word choice is confusing. The term “symbolic” is much better than the original term (“mysterious”).

In About In the discussion of my previous C problem, I was told that the pointer is

>”a simple value type much like an integer”
>not “mystical”
>”The Bit pattern (object representation) contains the value (value representation) (§3.9/4) for trivially copyable types, which a pointer is.”

This sounds great! If there is no symbol and the pointer is its representation, then I can do the following. Can I?

#include 
#include

int main() {
int a[1] = {0 }, *pa1 = &a[0] + 1, b = 1, *pb = &b;
if (memcmp (&pa1, &pb, sizeof pa1) == 0) {
printf ("pa1 == pb ");
*pa1 = 2;
}
else {
printf ("pa1 != pb ");
pa1 = &a[0]; // ensure well defined behaviour in printf
}
printf ("b = %d *pa1 = %d ", b, *pa1);
return 0;
}

This is a C and C problem.

Use GNU GCC v4.8.3 for Compile and Execute C Online test: gcc- O2 -Wall gives

pa1 == pb 
b = 1 *pa1 = 2

Use GNU GCC v4.8.3 for Compile and Execute C++ Online test: g -O2-Wall

pa1 == pb 
b = 1 *pa1 = 2

Therefore, the modification of b in (& a)[1] to GCC in C and C fails.

Of course, I I want to give an answer based on the standard quotation.

Edit: In response to the criticism about UB on& a 1, now a is an array of 1 element.

Related: Dereferencing an out of bound pointer that contains the address of an object (array of array)

Additional note: I think the word “mystery” was originally used by Tony Delroy here. I borrowed it wrong.

C is considered a language, where pointers and integers are very closely related, and the exact relationship depends on the target platform The relationship between pointers and integers makes this language very suitable for low-level or system programming. For the purposes of the following discussion, I will refer to this language as “low-level C” [LLC].

< p>The C Standards Committee has written a description in a different language. This relationship is not expressly prohibited, but it is not recognized in any useful way. This relationship is useful even if the implementation is the goal and the application domain generates code. I will This language is called “High Level C Only” [HLOC].

In the era of writing standards, most of the things that claim to be implemented in C deal with the dialect of LLC. The most useful compiler handles one A dialect that defines useful semantics in more cases than HLOC, but not as much as LLC. Pointers behave more like integers or abstract mysterious entities, depending on which precise dialect is used. If the system is in progress Programming, then it is reasonable to think of C as treating pointers and integers as closely related, because the LLC dialect suitable for this purpose does this, and the HLOC dialect that does not do so is not suitable for this purpose. However, when doing high-end When calculating numbers, people use HLOC dialects more often. These dialects cannot recognize this relationship.

The real problem, and the source of such controversy, is that LLC and HLOC are becoming more and more different, but both are Called C.

Edit: The original word choice is confusing. The term “symbolic” is much better than the original term (“mysterious”).

In the discussion about my previous C problem, I was told that the pointer is

>”a simple value type much like an integer”
>not “mystical”< br>>”The Bit pattern (object representation) contains the value (value representation) (§3.9/4) for trivially copyable types, which a pointer is.”

This sounds great! If there is no symbol and the pointer is its representation, then I can do the following. Can I?

#include 
#include

int main() {
int a[1] = {0 }, *pa1 = &a[0] + 1, b = 1, *pb = &b;
if (memcmp (&pa1, &pb, sizeof pa1) == 0) {
printf ("pa1 == pb ");
*pa1 = 2;
}
else {
printf ("pa1 != pb ");
pa1 = &a[0]; // ensure well defined behaviour in printf
}
printf ("b = %d *pa1 = %d ", b, *pa1);
return 0;
}

This is a C and C problem.

Use GNU GCC v4.8.3 for Compile and Execute C Online test: gcc- O2 -Wall gives

pa1 == pb 
b = 1 *pa1 = 2

Use GNU GCC v4.8.3 for Compile and Execute C++ Online test: g -O2-Wall

pa1 == pb 
b = 1 *pa1 = 2

Therefore, the modification of b in (& a)[1] to GCC in C and C fails.

Of course, I I want to give an answer based on the standard quotation.

Edit: In response to the criticism about UB on& a 1, now a is an array of 1 element.

Related: Dereferencing an out of bound pointer that contains the address of an object (array of array)

Additional note: I think the word “mystery” was originally used by Tony Delroy here. I borrowed it wrong.

C is considered a language, in which pointers and integers are very closely related, and the exact relationship depends on the target platform. The relationship between pointers and integers makes the language very Suitable for low-level or system programming. For the purpose of the following discussion, I will call this language “low-level C” [LLC].

The C Standards Committee has written a different language Description, this relationship is not expressly prohibited, but it is not recognized in any useful way. This relationship is useful even if the realization is the goal and the application domain generates code. I call this language “high-level C only” [HLOC].

In the era of writing standards, most of the things that claim to be implemented in C dealt with the dialect of LLC. The most useful compiler deals with a dialect, which is used in more situations than HLOC The following defines useful semantics, but there are not as many as LLC. Pointers behave more like integers or abstract mysterious entities, depending on which precise dialect is used. If you are doing system programming, then think of C as treating pointers and integers as It is reasonable to be closely related because LLC dialects suitable for this purpose do this, and HLOC dialects that do not do so are not suitable for this purpose. However, when doing high-end numerical operations, people use HLOC dialects more often, These dialects cannot identify this relationship.

The real problem, and the source of such controversy, is that LLC and HLOC are becoming more and more different, but both are called C.

Leave a Comment

Your email address will not be published.