How are addresses being used to find position? [duplicate] - c++

I was reading a bit in Pointer Arithmetic, and I came upon 2 things I couldn't understand neither know it's use
address_expression - address_expression
and also
address_expression > address_expression
Can someone please explain them to me, how do they work and when they are used.
Edit:
What I meant to say is what do they produce if I just take two addresses and subtract them
And If I take two addresses and compare them what is the result or comparing based upon
Edit:
I now understand the result of subtracting addresses, but comparing addresses I still don't get it.
I understand that 1<2, but how is an address greater than another one and what are they compared upon

Several answers here have stated that pointers are numbers. This is not an accurate description of pointers as specified by the C standard.
In large part, you can think of pointers as numbers, and as addresses in memory, provided (a) you understand that pointer subtraction converts the difference from bytes to elements (of the type of the pointers being subtracted), and (b) you understand the limits where this model breaks.
The following uses the 1999 C standard (ISO/IEC 9899, Second edition, 1999-12-01). I expect the following is more detailed than the asker requested, but, given some of the misstatements here, I judge that precise and accurate information should be given.
Per 6.5.6 paragraph 9, you may subtract two pointers that point to elements of the same array or to one past the last element of the array. So, if you have int a[8], b[4];, you may subtract a pointer to a[5] from a pointer to a[2], because a[5] and a[2] are elements in the same array. You may also subtract a pointer to a[5] from a pointer to a[8], because a[8] is one past the last element of the array. (a[8] is not in the array; a[7] is the last element.) You may not subtract a pointer to a[5] from a pointer to b[2], because a[5] is not in the same array as b[2]. Or, more accurately, if you do such a subtraction, the behavior is undefined. Note that it is not merely the result that is unspecified; you cannot expect that you will get some possibly nonsensical number as a result: The behavior is undefined. According to the C standard, this means that the C standard does not say anything about what occurs as a consequence. Your program could give you a reasonable answer, or it could abort, or it could delete files, and all those consequences would be in conformance to the C standard.
If you do an allowed subtraction, then the result is the number of elements from the second pointed-to element to the first pointed-to element. Thus, a[5]-a[2] is 3, and a[2]-a[5] is −3. This is true regardless of what type a is. The C implementation is required to convert the distance from bytes (or whatever units it uses) into elements of the appropriate type. If a is an array of double of eight bytes each, then a[5]-a[2] is 3, for 3 elements. If a is an array of char of one byte each, then a[5]-a[2] is 3, for 3 elements.
Why would pointers ever not be just numbers? On some computers, especially older computers, addressing memory was more complicated. Early computers had small address spaces. When the manufacturers wanted to make bigger addresses spaces, they also wanted to maintain some compatibility with old software. They also had to implement various schemes for addressing memory, due to hardware limitations, and those schemes may have involved moving data between memory and disk or changing special registers in the processor that controlled how addresses were converted to physical memory locations. For pointers to work on machines like that, they have to contain more information than just a simple address. Because of this, the C standard does not just define pointers as addresses and let you do arithmetic on the addresses. Only a reasonable amount of pointer arithmetic is defined, and the C implementation is required to provide the necessary operations to make that arithmetic work, but no more.
Even on modern machines, there can be complications. On Digital’s Alpha processors, a pointer to a function does not contain the address of the function. It is the address of a descriptor of the function. That descriptor contains the address of the function, and it contains some additional information that is necessary to call the function correctly.
With regard to relational operators, such as >, the C standard says, in 6.5.8 paragraph 5, that you may compare the same pointers you may subtract, as described above, and you may also compare pointers to members of an aggregate object (a struct or union). Pointers to members of an array (or its end address) compare in the expected way: Pointers to higher-indexed elements are greater than pointers to lower-indexed elements. Pointers to two members of the same union compare equal. For pointers to two members of a struct, the pointer to the member declared later is greater than the pointer to the member declared earlier.
As long as you stay within the constraints above, then you can think of pointers as numbers which are memory addresses.
Usually, it is easy for a C implementation to provide the behavior required by the C standard. Even if a computer has a compound pointer scheme, such as a base address and offset, usually all elements of an array will use the same base address as each other, and all elements of a struct will use the same base address as each other. So the compiler can simply subtract or compare the offset parts of the pointer to get the desired difference or comparison.
However, if you subtract pointers to different arrays on such a computer, you can get strange results. It is possible for the bit pattern formed by a base address and offset to appear greater (when interpreted as a single integer) than another pointer even though it points to a lower address in memory. This is one reason you must stay within the rules set by the C standard.

Pointer subtraction yields the number of array elements between two pointers of the same type.
For example,
int buf[10] = /* initializer here */;
&buf[10] - &buf[0]; // yields 10, the difference is 10 elements
Pointer comparison. For example, for the > relational operator: the > operation yields 1 if the pointed array element or structure member on the left hand side is after the pointed array element or structure member on the right hand side and it yields 0 otherwise. Remember arrays and structures are ordered sequences.
&buf[10] > &buf[0]; // 1, &buf[10] element is after &buf[0] element

Subtracting two pointer addresses returns the number of elements of that type.
So if you have an array of integers and two pointers into it, subtracting those pointers will return the number of int values between, not the number of bytes. Same with char types. So you need to be careful with this, especially if you are working with a byte buffer or wide characters, that your expression is calculating the right value. If you need byte-based buffer offsets for something that does not use a single byte for storage (int, short, etc) you need to cast your pointers to char* first.

The first expression subtracts one pointer from another. As a simple example of why this might be useful, consider a C string. The string is in contiguous memory, so if you had the address of the first character of the string, and the address of the last character, you could find the length of the string by doing:
int strLength = (last_char_address - first_char_address) + 1;
Such pointer arithmetic is type aware, meaning that the result of the arithmetic represents the number of elements - of the specific type - between two pointers. In the above example using char, the difference is the number of characters. This works similarly for e.g. pointers to two structs.
Similarly, your second expression is simply comparing pointers and the result will be 1 or 0. As a very simple example, the address of element 5 of an array is always > the address of element 4: &string[4] > &string[5] is true.

An analogy I like to use when explaining pointer arithmetic — both how it works, and its limitations — is to think about street addresses.
Suppose there are a bunch of houses on same-sized lots on Elm Street, with all the lots, say, 50 feet wide. Suppose I want to know how far it is from #12 Elm Street to #46 Elm Street, and suppose I want to know this distance as a number of houses, not a distance in feet. Well, obviously, I can just subtract 12 from 46, and get an answer of 34 houses. (Actually, of course, it's a little more complicated than that, because there are probably houses on both sides of the street, but let's ignore that issue for now.)
And suppose over on 10th Avenue there are a bunch of industrial buildings on bigger lots, all 100 feet wide. I can still subtract street numbers, and I'll get distances in number of buildings (not feet).
And this is analogous to pointer subtraction in C, where you get differences that are scaled by the size of the pointed-to objects. You do not get answers as raw bytes (analogous to feet in the street address analogy).
But the other thing the street address analogy helps us understand is why we can't use pointer arithmetic to work with pointers into different arrays. Suppose I want to know how far it is from #12 Elm Street to #30 10th Avenue. Subtracting the addresses doesn't work! It's meaningless. You can't meaningfully subtract or compare addresses on different streets, just as you can't meaningfully subtract or compare pointers into different arrays.

Pointers can often be thought of as just numbers that represents the memory address, like 0x0A31FCF20 (or 2736770848 in decimal), or 0xCAFEDEAD (sometimes systems use this to indicate an error, I don't remember the details.)
Pointer comparison is often used in sorting arrays of pointers. Sorted arrays of pointers are helpful when you need to check if a pointer is in a list of pointers; if the list is sorted, you don't have to look through every element of the list to figure out if the pointer is in that list. You need to use comparisons to sort a list.
Pointer arithmetic is often used when you have a pointer to a chunk of data, and you need to access something that is not at the beginning of the chunk of data. For example:
const char *string = "hello world!"
const char *substring = string+6;
std::cout << string << "\n";
std::cout << substring << std::endl;
This would output:
hello world!
world!
Here we got the string after the first 6 characters of "hello world!", or "world!". Keep in mind that you should use std::string where its available, instead, if possible. A concept very similar to pointer arithmetic is random access iterators.
Subtracting pointers can help you find the distance between those two pointers. If you have a pointer to the first element of an array, and a pointer to one element past the last element of the array, subtracting these two pointers helps you find the size of the array.
Another case where you might treat pointers as integers is in an optimized version of a linked list, called an XOR linked list. You can find more details about it here. I can expand on this if you'd like; let me know in the comments.

You can treat an address like an int in many ways. The only difference is that that int is representing the number of sizes in that address. For example, if int * p happens to have the value of, say, 234 (from some safe instruction of for example p = new int[12];), it represents the address 234. If we do p += 1;, it's just adding, in terms of int-size. Now p is (assuming 4-byte int for this example) 238, aka p[1]. In fact p[x] is equivalent to *(p+x). You can compare and such just like an int. In some contexts this is useful, for example in the given example p[0] now refers to what was p[1]. This avoids having to do something like p = &p[1] which dereferences unnecessarily.

Related

Shifting values when calling global function in CUDA/C [duplicate]

I was reading a bit in Pointer Arithmetic, and I came upon 2 things I couldn't understand neither know it's use
address_expression - address_expression
and also
address_expression > address_expression
Can someone please explain them to me, how do they work and when they are used.
Edit:
What I meant to say is what do they produce if I just take two addresses and subtract them
And If I take two addresses and compare them what is the result or comparing based upon
Edit:
I now understand the result of subtracting addresses, but comparing addresses I still don't get it.
I understand that 1<2, but how is an address greater than another one and what are they compared upon
Several answers here have stated that pointers are numbers. This is not an accurate description of pointers as specified by the C standard.
In large part, you can think of pointers as numbers, and as addresses in memory, provided (a) you understand that pointer subtraction converts the difference from bytes to elements (of the type of the pointers being subtracted), and (b) you understand the limits where this model breaks.
The following uses the 1999 C standard (ISO/IEC 9899, Second edition, 1999-12-01). I expect the following is more detailed than the asker requested, but, given some of the misstatements here, I judge that precise and accurate information should be given.
Per 6.5.6 paragraph 9, you may subtract two pointers that point to elements of the same array or to one past the last element of the array. So, if you have int a[8], b[4];, you may subtract a pointer to a[5] from a pointer to a[2], because a[5] and a[2] are elements in the same array. You may also subtract a pointer to a[5] from a pointer to a[8], because a[8] is one past the last element of the array. (a[8] is not in the array; a[7] is the last element.) You may not subtract a pointer to a[5] from a pointer to b[2], because a[5] is not in the same array as b[2]. Or, more accurately, if you do such a subtraction, the behavior is undefined. Note that it is not merely the result that is unspecified; you cannot expect that you will get some possibly nonsensical number as a result: The behavior is undefined. According to the C standard, this means that the C standard does not say anything about what occurs as a consequence. Your program could give you a reasonable answer, or it could abort, or it could delete files, and all those consequences would be in conformance to the C standard.
If you do an allowed subtraction, then the result is the number of elements from the second pointed-to element to the first pointed-to element. Thus, a[5]-a[2] is 3, and a[2]-a[5] is −3. This is true regardless of what type a is. The C implementation is required to convert the distance from bytes (or whatever units it uses) into elements of the appropriate type. If a is an array of double of eight bytes each, then a[5]-a[2] is 3, for 3 elements. If a is an array of char of one byte each, then a[5]-a[2] is 3, for 3 elements.
Why would pointers ever not be just numbers? On some computers, especially older computers, addressing memory was more complicated. Early computers had small address spaces. When the manufacturers wanted to make bigger addresses spaces, they also wanted to maintain some compatibility with old software. They also had to implement various schemes for addressing memory, due to hardware limitations, and those schemes may have involved moving data between memory and disk or changing special registers in the processor that controlled how addresses were converted to physical memory locations. For pointers to work on machines like that, they have to contain more information than just a simple address. Because of this, the C standard does not just define pointers as addresses and let you do arithmetic on the addresses. Only a reasonable amount of pointer arithmetic is defined, and the C implementation is required to provide the necessary operations to make that arithmetic work, but no more.
Even on modern machines, there can be complications. On Digital’s Alpha processors, a pointer to a function does not contain the address of the function. It is the address of a descriptor of the function. That descriptor contains the address of the function, and it contains some additional information that is necessary to call the function correctly.
With regard to relational operators, such as >, the C standard says, in 6.5.8 paragraph 5, that you may compare the same pointers you may subtract, as described above, and you may also compare pointers to members of an aggregate object (a struct or union). Pointers to members of an array (or its end address) compare in the expected way: Pointers to higher-indexed elements are greater than pointers to lower-indexed elements. Pointers to two members of the same union compare equal. For pointers to two members of a struct, the pointer to the member declared later is greater than the pointer to the member declared earlier.
As long as you stay within the constraints above, then you can think of pointers as numbers which are memory addresses.
Usually, it is easy for a C implementation to provide the behavior required by the C standard. Even if a computer has a compound pointer scheme, such as a base address and offset, usually all elements of an array will use the same base address as each other, and all elements of a struct will use the same base address as each other. So the compiler can simply subtract or compare the offset parts of the pointer to get the desired difference or comparison.
However, if you subtract pointers to different arrays on such a computer, you can get strange results. It is possible for the bit pattern formed by a base address and offset to appear greater (when interpreted as a single integer) than another pointer even though it points to a lower address in memory. This is one reason you must stay within the rules set by the C standard.
Pointer subtraction yields the number of array elements between two pointers of the same type.
For example,
int buf[10] = /* initializer here */;
&buf[10] - &buf[0]; // yields 10, the difference is 10 elements
Pointer comparison. For example, for the > relational operator: the > operation yields 1 if the pointed array element or structure member on the left hand side is after the pointed array element or structure member on the right hand side and it yields 0 otherwise. Remember arrays and structures are ordered sequences.
&buf[10] > &buf[0]; // 1, &buf[10] element is after &buf[0] element
Subtracting two pointer addresses returns the number of elements of that type.
So if you have an array of integers and two pointers into it, subtracting those pointers will return the number of int values between, not the number of bytes. Same with char types. So you need to be careful with this, especially if you are working with a byte buffer or wide characters, that your expression is calculating the right value. If you need byte-based buffer offsets for something that does not use a single byte for storage (int, short, etc) you need to cast your pointers to char* first.
The first expression subtracts one pointer from another. As a simple example of why this might be useful, consider a C string. The string is in contiguous memory, so if you had the address of the first character of the string, and the address of the last character, you could find the length of the string by doing:
int strLength = (last_char_address - first_char_address) + 1;
Such pointer arithmetic is type aware, meaning that the result of the arithmetic represents the number of elements - of the specific type - between two pointers. In the above example using char, the difference is the number of characters. This works similarly for e.g. pointers to two structs.
Similarly, your second expression is simply comparing pointers and the result will be 1 or 0. As a very simple example, the address of element 5 of an array is always > the address of element 4: &string[4] > &string[5] is true.
An analogy I like to use when explaining pointer arithmetic — both how it works, and its limitations — is to think about street addresses.
Suppose there are a bunch of houses on same-sized lots on Elm Street, with all the lots, say, 50 feet wide. Suppose I want to know how far it is from #12 Elm Street to #46 Elm Street, and suppose I want to know this distance as a number of houses, not a distance in feet. Well, obviously, I can just subtract 12 from 46, and get an answer of 34 houses. (Actually, of course, it's a little more complicated than that, because there are probably houses on both sides of the street, but let's ignore that issue for now.)
And suppose over on 10th Avenue there are a bunch of industrial buildings on bigger lots, all 100 feet wide. I can still subtract street numbers, and I'll get distances in number of buildings (not feet).
And this is analogous to pointer subtraction in C, where you get differences that are scaled by the size of the pointed-to objects. You do not get answers as raw bytes (analogous to feet in the street address analogy).
But the other thing the street address analogy helps us understand is why we can't use pointer arithmetic to work with pointers into different arrays. Suppose I want to know how far it is from #12 Elm Street to #30 10th Avenue. Subtracting the addresses doesn't work! It's meaningless. You can't meaningfully subtract or compare addresses on different streets, just as you can't meaningfully subtract or compare pointers into different arrays.
Pointers can often be thought of as just numbers that represents the memory address, like 0x0A31FCF20 (or 2736770848 in decimal), or 0xCAFEDEAD (sometimes systems use this to indicate an error, I don't remember the details.)
Pointer comparison is often used in sorting arrays of pointers. Sorted arrays of pointers are helpful when you need to check if a pointer is in a list of pointers; if the list is sorted, you don't have to look through every element of the list to figure out if the pointer is in that list. You need to use comparisons to sort a list.
Pointer arithmetic is often used when you have a pointer to a chunk of data, and you need to access something that is not at the beginning of the chunk of data. For example:
const char *string = "hello world!"
const char *substring = string+6;
std::cout << string << "\n";
std::cout << substring << std::endl;
This would output:
hello world!
world!
Here we got the string after the first 6 characters of "hello world!", or "world!". Keep in mind that you should use std::string where its available, instead, if possible. A concept very similar to pointer arithmetic is random access iterators.
Subtracting pointers can help you find the distance between those two pointers. If you have a pointer to the first element of an array, and a pointer to one element past the last element of the array, subtracting these two pointers helps you find the size of the array.
Another case where you might treat pointers as integers is in an optimized version of a linked list, called an XOR linked list. You can find more details about it here. I can expand on this if you'd like; let me know in the comments.
You can treat an address like an int in many ways. The only difference is that that int is representing the number of sizes in that address. For example, if int * p happens to have the value of, say, 234 (from some safe instruction of for example p = new int[12];), it represents the address 234. If we do p += 1;, it's just adding, in terms of int-size. Now p is (assuming 4-byte int for this example) 238, aka p[1]. In fact p[x] is equivalent to *(p+x). You can compare and such just like an int. In some contexts this is useful, for example in the given example p[0] now refers to what was p[1]. This avoids having to do something like p = &p[1] which dereferences unnecessarily.

How come 0x787619ce51b4 - 0x787619ce51b0 = 1? Shouldn't it be 4? [duplicate]

I was reading a bit in Pointer Arithmetic, and I came upon 2 things I couldn't understand neither know it's use
address_expression - address_expression
and also
address_expression > address_expression
Can someone please explain them to me, how do they work and when they are used.
Edit:
What I meant to say is what do they produce if I just take two addresses and subtract them
And If I take two addresses and compare them what is the result or comparing based upon
Edit:
I now understand the result of subtracting addresses, but comparing addresses I still don't get it.
I understand that 1<2, but how is an address greater than another one and what are they compared upon
Several answers here have stated that pointers are numbers. This is not an accurate description of pointers as specified by the C standard.
In large part, you can think of pointers as numbers, and as addresses in memory, provided (a) you understand that pointer subtraction converts the difference from bytes to elements (of the type of the pointers being subtracted), and (b) you understand the limits where this model breaks.
The following uses the 1999 C standard (ISO/IEC 9899, Second edition, 1999-12-01). I expect the following is more detailed than the asker requested, but, given some of the misstatements here, I judge that precise and accurate information should be given.
Per 6.5.6 paragraph 9, you may subtract two pointers that point to elements of the same array or to one past the last element of the array. So, if you have int a[8], b[4];, you may subtract a pointer to a[5] from a pointer to a[2], because a[5] and a[2] are elements in the same array. You may also subtract a pointer to a[5] from a pointer to a[8], because a[8] is one past the last element of the array. (a[8] is not in the array; a[7] is the last element.) You may not subtract a pointer to a[5] from a pointer to b[2], because a[5] is not in the same array as b[2]. Or, more accurately, if you do such a subtraction, the behavior is undefined. Note that it is not merely the result that is unspecified; you cannot expect that you will get some possibly nonsensical number as a result: The behavior is undefined. According to the C standard, this means that the C standard does not say anything about what occurs as a consequence. Your program could give you a reasonable answer, or it could abort, or it could delete files, and all those consequences would be in conformance to the C standard.
If you do an allowed subtraction, then the result is the number of elements from the second pointed-to element to the first pointed-to element. Thus, a[5]-a[2] is 3, and a[2]-a[5] is −3. This is true regardless of what type a is. The C implementation is required to convert the distance from bytes (or whatever units it uses) into elements of the appropriate type. If a is an array of double of eight bytes each, then a[5]-a[2] is 3, for 3 elements. If a is an array of char of one byte each, then a[5]-a[2] is 3, for 3 elements.
Why would pointers ever not be just numbers? On some computers, especially older computers, addressing memory was more complicated. Early computers had small address spaces. When the manufacturers wanted to make bigger addresses spaces, they also wanted to maintain some compatibility with old software. They also had to implement various schemes for addressing memory, due to hardware limitations, and those schemes may have involved moving data between memory and disk or changing special registers in the processor that controlled how addresses were converted to physical memory locations. For pointers to work on machines like that, they have to contain more information than just a simple address. Because of this, the C standard does not just define pointers as addresses and let you do arithmetic on the addresses. Only a reasonable amount of pointer arithmetic is defined, and the C implementation is required to provide the necessary operations to make that arithmetic work, but no more.
Even on modern machines, there can be complications. On Digital’s Alpha processors, a pointer to a function does not contain the address of the function. It is the address of a descriptor of the function. That descriptor contains the address of the function, and it contains some additional information that is necessary to call the function correctly.
With regard to relational operators, such as >, the C standard says, in 6.5.8 paragraph 5, that you may compare the same pointers you may subtract, as described above, and you may also compare pointers to members of an aggregate object (a struct or union). Pointers to members of an array (or its end address) compare in the expected way: Pointers to higher-indexed elements are greater than pointers to lower-indexed elements. Pointers to two members of the same union compare equal. For pointers to two members of a struct, the pointer to the member declared later is greater than the pointer to the member declared earlier.
As long as you stay within the constraints above, then you can think of pointers as numbers which are memory addresses.
Usually, it is easy for a C implementation to provide the behavior required by the C standard. Even if a computer has a compound pointer scheme, such as a base address and offset, usually all elements of an array will use the same base address as each other, and all elements of a struct will use the same base address as each other. So the compiler can simply subtract or compare the offset parts of the pointer to get the desired difference or comparison.
However, if you subtract pointers to different arrays on such a computer, you can get strange results. It is possible for the bit pattern formed by a base address and offset to appear greater (when interpreted as a single integer) than another pointer even though it points to a lower address in memory. This is one reason you must stay within the rules set by the C standard.
Pointer subtraction yields the number of array elements between two pointers of the same type.
For example,
int buf[10] = /* initializer here */;
&buf[10] - &buf[0]; // yields 10, the difference is 10 elements
Pointer comparison. For example, for the > relational operator: the > operation yields 1 if the pointed array element or structure member on the left hand side is after the pointed array element or structure member on the right hand side and it yields 0 otherwise. Remember arrays and structures are ordered sequences.
&buf[10] > &buf[0]; // 1, &buf[10] element is after &buf[0] element
Subtracting two pointer addresses returns the number of elements of that type.
So if you have an array of integers and two pointers into it, subtracting those pointers will return the number of int values between, not the number of bytes. Same with char types. So you need to be careful with this, especially if you are working with a byte buffer or wide characters, that your expression is calculating the right value. If you need byte-based buffer offsets for something that does not use a single byte for storage (int, short, etc) you need to cast your pointers to char* first.
The first expression subtracts one pointer from another. As a simple example of why this might be useful, consider a C string. The string is in contiguous memory, so if you had the address of the first character of the string, and the address of the last character, you could find the length of the string by doing:
int strLength = (last_char_address - first_char_address) + 1;
Such pointer arithmetic is type aware, meaning that the result of the arithmetic represents the number of elements - of the specific type - between two pointers. In the above example using char, the difference is the number of characters. This works similarly for e.g. pointers to two structs.
Similarly, your second expression is simply comparing pointers and the result will be 1 or 0. As a very simple example, the address of element 5 of an array is always > the address of element 4: &string[4] > &string[5] is true.
An analogy I like to use when explaining pointer arithmetic — both how it works, and its limitations — is to think about street addresses.
Suppose there are a bunch of houses on same-sized lots on Elm Street, with all the lots, say, 50 feet wide. Suppose I want to know how far it is from #12 Elm Street to #46 Elm Street, and suppose I want to know this distance as a number of houses, not a distance in feet. Well, obviously, I can just subtract 12 from 46, and get an answer of 34 houses. (Actually, of course, it's a little more complicated than that, because there are probably houses on both sides of the street, but let's ignore that issue for now.)
And suppose over on 10th Avenue there are a bunch of industrial buildings on bigger lots, all 100 feet wide. I can still subtract street numbers, and I'll get distances in number of buildings (not feet).
And this is analogous to pointer subtraction in C, where you get differences that are scaled by the size of the pointed-to objects. You do not get answers as raw bytes (analogous to feet in the street address analogy).
But the other thing the street address analogy helps us understand is why we can't use pointer arithmetic to work with pointers into different arrays. Suppose I want to know how far it is from #12 Elm Street to #30 10th Avenue. Subtracting the addresses doesn't work! It's meaningless. You can't meaningfully subtract or compare addresses on different streets, just as you can't meaningfully subtract or compare pointers into different arrays.
Pointers can often be thought of as just numbers that represents the memory address, like 0x0A31FCF20 (or 2736770848 in decimal), or 0xCAFEDEAD (sometimes systems use this to indicate an error, I don't remember the details.)
Pointer comparison is often used in sorting arrays of pointers. Sorted arrays of pointers are helpful when you need to check if a pointer is in a list of pointers; if the list is sorted, you don't have to look through every element of the list to figure out if the pointer is in that list. You need to use comparisons to sort a list.
Pointer arithmetic is often used when you have a pointer to a chunk of data, and you need to access something that is not at the beginning of the chunk of data. For example:
const char *string = "hello world!"
const char *substring = string+6;
std::cout << string << "\n";
std::cout << substring << std::endl;
This would output:
hello world!
world!
Here we got the string after the first 6 characters of "hello world!", or "world!". Keep in mind that you should use std::string where its available, instead, if possible. A concept very similar to pointer arithmetic is random access iterators.
Subtracting pointers can help you find the distance between those two pointers. If you have a pointer to the first element of an array, and a pointer to one element past the last element of the array, subtracting these two pointers helps you find the size of the array.
Another case where you might treat pointers as integers is in an optimized version of a linked list, called an XOR linked list. You can find more details about it here. I can expand on this if you'd like; let me know in the comments.
You can treat an address like an int in many ways. The only difference is that that int is representing the number of sizes in that address. For example, if int * p happens to have the value of, say, 234 (from some safe instruction of for example p = new int[12];), it represents the address 234. If we do p += 1;, it's just adding, in terms of int-size. Now p is (assuming 4-byte int for this example) 238, aka p[1]. In fact p[x] is equivalent to *(p+x). You can compare and such just like an int. In some contexts this is useful, for example in the given example p[0] now refers to what was p[1]. This avoids having to do something like p = &p[1] which dereferences unnecessarily.

How to delete specific elements from an array in c++

I dont know the numbers which are stored in the array[multidimensional].As I get these numbers from the sensor.I just know that If the same number is repeated more than 5 times, that number should be deleted.
please help.
How to delete specific elements from an array
Depends on what do you mean by "delete". An array of x numbers always has exactly x numbers. An integer can't have a state that represents a "deleted" number, unless you decide that a specific value signifies such state. A typical choice would be -1 if only positive values are used otherwise. A floating point number could be set to NaN, but considering the "repeated 5 times" requirement, remember that equality comparison of floating point numbers is not trivial.
Or, you could maintain a duplicate array of bools which signifies whether the number in corresponding index has been deleted.
Another approach would be to augment your array with a pointer to the last "used" number (or rather, point to the one after the last used number). This allows you to represent a smaller (dynamic)array than fits into the whole array. The size of such dynamic array would be the distance between address of the first number and the pointer and the size may change up to x. The numbers beyond the pointer would then be considered deleted. You must take care not to access the deleted numbers thinking they would contain valid data. If you want to delete a number in middle of the array, simply copy all numbers after it one index to the left and decrement the pointer. If you don't want to implement this yourself (and you shouldn't want to), you may want to use std::vector instead since this is pretty much what vector does under the hood.

Difference between <type*[n]> and <type(*)[n]> in C++

I wanted to create a queue to store two dimensional arrays of chars and I thought that declaring it in the following way would work:
queue<char*[7]> states;
However, it turned out that the right way was:
queue<char(*)[7]> states;
And I can't really understand what do the round brackets change? I guess it has something to do with precedence, but nothing more specific.
char*[7] is an array of seven pointers to char, char(*)[7] is a pointer to an array of seven chars. Often it's used to allocate dynamically contiguous multidimensional arrays (see here).
The C++ FAQ about arrays may give you some insight about these subtleties.
An easy way to remember the meaning of char*[7] is that that's the form of the second argument to main.
I.e. it means an array of pointers.
Then char(*)[7] is easiest to analyze by introducing a name, like char(*p)[7]. Since C declarations were designed to mimic use of the declared things, this means that you can dereference p, and index the result, then yielding a char. I.e. p is a pointer to an array of char.
char*[7] is an array of pointer to char.
char(*)[7] is a pointer referencing an array of char.

C/C++: Pointer Arithmetic

I was reading a bit in Pointer Arithmetic, and I came upon 2 things I couldn't understand neither know it's use
address_expression - address_expression
and also
address_expression > address_expression
Can someone please explain them to me, how do they work and when they are used.
Edit:
What I meant to say is what do they produce if I just take two addresses and subtract them
And If I take two addresses and compare them what is the result or comparing based upon
Edit:
I now understand the result of subtracting addresses, but comparing addresses I still don't get it.
I understand that 1<2, but how is an address greater than another one and what are they compared upon
Several answers here have stated that pointers are numbers. This is not an accurate description of pointers as specified by the C standard.
In large part, you can think of pointers as numbers, and as addresses in memory, provided (a) you understand that pointer subtraction converts the difference from bytes to elements (of the type of the pointers being subtracted), and (b) you understand the limits where this model breaks.
The following uses the 1999 C standard (ISO/IEC 9899, Second edition, 1999-12-01). I expect the following is more detailed than the asker requested, but, given some of the misstatements here, I judge that precise and accurate information should be given.
Per 6.5.6 paragraph 9, you may subtract two pointers that point to elements of the same array or to one past the last element of the array. So, if you have int a[8], b[4];, you may subtract a pointer to a[5] from a pointer to a[2], because a[5] and a[2] are elements in the same array. You may also subtract a pointer to a[5] from a pointer to a[8], because a[8] is one past the last element of the array. (a[8] is not in the array; a[7] is the last element.) You may not subtract a pointer to a[5] from a pointer to b[2], because a[5] is not in the same array as b[2]. Or, more accurately, if you do such a subtraction, the behavior is undefined. Note that it is not merely the result that is unspecified; you cannot expect that you will get some possibly nonsensical number as a result: The behavior is undefined. According to the C standard, this means that the C standard does not say anything about what occurs as a consequence. Your program could give you a reasonable answer, or it could abort, or it could delete files, and all those consequences would be in conformance to the C standard.
If you do an allowed subtraction, then the result is the number of elements from the second pointed-to element to the first pointed-to element. Thus, a[5]-a[2] is 3, and a[2]-a[5] is −3. This is true regardless of what type a is. The C implementation is required to convert the distance from bytes (or whatever units it uses) into elements of the appropriate type. If a is an array of double of eight bytes each, then a[5]-a[2] is 3, for 3 elements. If a is an array of char of one byte each, then a[5]-a[2] is 3, for 3 elements.
Why would pointers ever not be just numbers? On some computers, especially older computers, addressing memory was more complicated. Early computers had small address spaces. When the manufacturers wanted to make bigger addresses spaces, they also wanted to maintain some compatibility with old software. They also had to implement various schemes for addressing memory, due to hardware limitations, and those schemes may have involved moving data between memory and disk or changing special registers in the processor that controlled how addresses were converted to physical memory locations. For pointers to work on machines like that, they have to contain more information than just a simple address. Because of this, the C standard does not just define pointers as addresses and let you do arithmetic on the addresses. Only a reasonable amount of pointer arithmetic is defined, and the C implementation is required to provide the necessary operations to make that arithmetic work, but no more.
Even on modern machines, there can be complications. On Digital’s Alpha processors, a pointer to a function does not contain the address of the function. It is the address of a descriptor of the function. That descriptor contains the address of the function, and it contains some additional information that is necessary to call the function correctly.
With regard to relational operators, such as >, the C standard says, in 6.5.8 paragraph 5, that you may compare the same pointers you may subtract, as described above, and you may also compare pointers to members of an aggregate object (a struct or union). Pointers to members of an array (or its end address) compare in the expected way: Pointers to higher-indexed elements are greater than pointers to lower-indexed elements. Pointers to two members of the same union compare equal. For pointers to two members of a struct, the pointer to the member declared later is greater than the pointer to the member declared earlier.
As long as you stay within the constraints above, then you can think of pointers as numbers which are memory addresses.
Usually, it is easy for a C implementation to provide the behavior required by the C standard. Even if a computer has a compound pointer scheme, such as a base address and offset, usually all elements of an array will use the same base address as each other, and all elements of a struct will use the same base address as each other. So the compiler can simply subtract or compare the offset parts of the pointer to get the desired difference or comparison.
However, if you subtract pointers to different arrays on such a computer, you can get strange results. It is possible for the bit pattern formed by a base address and offset to appear greater (when interpreted as a single integer) than another pointer even though it points to a lower address in memory. This is one reason you must stay within the rules set by the C standard.
Pointer subtraction yields the number of array elements between two pointers of the same type.
For example,
int buf[10] = /* initializer here */;
&buf[10] - &buf[0]; // yields 10, the difference is 10 elements
Pointer comparison. For example, for the > relational operator: the > operation yields 1 if the pointed array element or structure member on the left hand side is after the pointed array element or structure member on the right hand side and it yields 0 otherwise. Remember arrays and structures are ordered sequences.
&buf[10] > &buf[0]; // 1, &buf[10] element is after &buf[0] element
Subtracting two pointer addresses returns the number of elements of that type.
So if you have an array of integers and two pointers into it, subtracting those pointers will return the number of int values between, not the number of bytes. Same with char types. So you need to be careful with this, especially if you are working with a byte buffer or wide characters, that your expression is calculating the right value. If you need byte-based buffer offsets for something that does not use a single byte for storage (int, short, etc) you need to cast your pointers to char* first.
The first expression subtracts one pointer from another. As a simple example of why this might be useful, consider a C string. The string is in contiguous memory, so if you had the address of the first character of the string, and the address of the last character, you could find the length of the string by doing:
int strLength = (last_char_address - first_char_address) + 1;
Such pointer arithmetic is type aware, meaning that the result of the arithmetic represents the number of elements - of the specific type - between two pointers. In the above example using char, the difference is the number of characters. This works similarly for e.g. pointers to two structs.
Similarly, your second expression is simply comparing pointers and the result will be 1 or 0. As a very simple example, the address of element 5 of an array is always > the address of element 4: &string[4] > &string[5] is true.
An analogy I like to use when explaining pointer arithmetic — both how it works, and its limitations — is to think about street addresses.
Suppose there are a bunch of houses on same-sized lots on Elm Street, with all the lots, say, 50 feet wide. Suppose I want to know how far it is from #12 Elm Street to #46 Elm Street, and suppose I want to know this distance as a number of houses, not a distance in feet. Well, obviously, I can just subtract 12 from 46, and get an answer of 34 houses. (Actually, of course, it's a little more complicated than that, because there are probably houses on both sides of the street, but let's ignore that issue for now.)
And suppose over on 10th Avenue there are a bunch of industrial buildings on bigger lots, all 100 feet wide. I can still subtract street numbers, and I'll get distances in number of buildings (not feet).
And this is analogous to pointer subtraction in C, where you get differences that are scaled by the size of the pointed-to objects. You do not get answers as raw bytes (analogous to feet in the street address analogy).
But the other thing the street address analogy helps us understand is why we can't use pointer arithmetic to work with pointers into different arrays. Suppose I want to know how far it is from #12 Elm Street to #30 10th Avenue. Subtracting the addresses doesn't work! It's meaningless. You can't meaningfully subtract or compare addresses on different streets, just as you can't meaningfully subtract or compare pointers into different arrays.
Pointers can often be thought of as just numbers that represents the memory address, like 0x0A31FCF20 (or 2736770848 in decimal), or 0xCAFEDEAD (sometimes systems use this to indicate an error, I don't remember the details.)
Pointer comparison is often used in sorting arrays of pointers. Sorted arrays of pointers are helpful when you need to check if a pointer is in a list of pointers; if the list is sorted, you don't have to look through every element of the list to figure out if the pointer is in that list. You need to use comparisons to sort a list.
Pointer arithmetic is often used when you have a pointer to a chunk of data, and you need to access something that is not at the beginning of the chunk of data. For example:
const char *string = "hello world!"
const char *substring = string+6;
std::cout << string << "\n";
std::cout << substring << std::endl;
This would output:
hello world!
world!
Here we got the string after the first 6 characters of "hello world!", or "world!". Keep in mind that you should use std::string where its available, instead, if possible. A concept very similar to pointer arithmetic is random access iterators.
Subtracting pointers can help you find the distance between those two pointers. If you have a pointer to the first element of an array, and a pointer to one element past the last element of the array, subtracting these two pointers helps you find the size of the array.
Another case where you might treat pointers as integers is in an optimized version of a linked list, called an XOR linked list. You can find more details about it here. I can expand on this if you'd like; let me know in the comments.
You can treat an address like an int in many ways. The only difference is that that int is representing the number of sizes in that address. For example, if int * p happens to have the value of, say, 234 (from some safe instruction of for example p = new int[12];), it represents the address 234. If we do p += 1;, it's just adding, in terms of int-size. Now p is (assuming 4-byte int for this example) 238, aka p[1]. In fact p[x] is equivalent to *(p+x). You can compare and such just like an int. In some contexts this is useful, for example in the given example p[0] now refers to what was p[1]. This avoids having to do something like p = &p[1] which dereferences unnecessarily.