int arr[][3] = {{1,3},{2,3},{6,7}};
cout << arr[0] << endl; // W/O col' somehow give you the address, not the element.
cout << arr[0][0] << endl;
I am curious as to why arr[0] prints the address, not the element.
It seems like somehow it's ignoring its dereference operator.
I'd be appreciated if you could help me in terms of a pointer or in depth.
Thank you.
Since arr is a two-dimensional array, arr[0] will give you an entire row (more precisely, the address of that row).
More precisely, since arr is of type int[][3] arr[0] is of type int[3], which decays to int *. Hence, cout << arr[0] << endl; prints a pointer.
array[0] holds the address of the array[0][0...n-1] values so only cout << arr[0]; prints the address since arr[0] is the pointer to the values in arr[0][0..n-1]
If you want specific value of an two dimensional array then you need to give both x and y index like
array[0][0] ... array[0][n-1]
Related
I have come across a piece of example code that uses pointers and a simple subtraction to calculate the number of items in an array using C++.
I have run the code and it works but when I do the math on paper I get a different answer.
There explanation does not really show why this works and I was hoping someone could explain this too me.
#include <iostream>
using namespace std;
int main() {
int array[10] = {0, 9, 1, 8, 2, 7, 3, 6, 4, 5};
int stretch = *(&array + 1) - array;
cout << "Array is consists of: " << stretch << " numbers" << endl;
cout << "Hence, Length of Array is: " << stretch;
return 0;
}
From: https://www.educba.com/c-plus-plus-length-of-array/
When I run the code I get the number 10.
When I print the results of *(&array + 1) and array by
cout << *(&array+1) << endl; cout << array << endl;
I get of course two hex address's.
When I subtract these hex numbers I get 1C or 28???
Is it possible that C++ does not actually give the hex results or their translation to decimal but rather sees these numbers as addresses and therefore only returns the number of address slots remaining?
That is the closest I can come to an explanation if some one with more knowledge than I could explain this I would be very grateful.
Let's take one step back and take it step-by-step to see if it will help. Continuing from my comment, the problem you are having difficulty with is one of type.
Let's take the array iteself:
int array[10] = {0, 9, 1, 8, 2, 7, 3, 6, 4, 5};
On access, an array is converted to a pointer to the first element in the array (e.g. the address of the first element) subject to caveats not relevant here. So when you say array, you have type int *, a pointer to the first element in array.
Now what happens when I take the address of the array? (&array in)
int stretch = *(&array + 1) - array;
When you take the address of the array, the result is the same address as array, but has type int (*)[10] (a pointer-to-array-of int[10]). When you add 1 to that pointer (recall type controls pointer arithmetic), you get the address for the pointer to the next array of int[10] in memory after array -- which will be 10 int after the first element of array.
So *(&array + 1) gives you the address to the next array of int[10] after array, and then dereference is only needed for type compatibility. When you dereference an int (*)[10] you are left with int[10] -- which on access gives you the address of the first element of that array (one after the original)
Think through the types and let me know if you have further questions.
You forgot a small detail of how pointer addition or subtraction works. Let's start with a simple example.
int *p;
This is pointing to some integer. If, with your C++ compiler, ints are four bytes long:
++p;
This does not increment the actual pointer value by 1, but by 4. The pointer is now pointing to the next int. If you look at the actual pointer value, in hexadecimal, it will increase by 4, not 1.
Pointer subtraction works the same way:
int *a;
int *b;
// ...
size_t c=b-a;
If the difference in the hexadecimal values of a and b is 12, the result of this subtraction will not be 12, but 3.
When I subtract these hex numbers I get 1C or 28 ???
There must've been a mistake with your subtraction. Your result should be 0x28, or 40 (most likely you asked your debugger or compiler to do the subtraction, you got the result in hexadecimal and assumed that it was decimal instead). That would be the ten ints you were looking for.
I will try it with 5 items
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int array[] {1,2,3,4,5};
int items= sizeof(array)/sizeof(array[0]);
cout << items << endl;
int items2 = *(&array +1) - array;
cout << items2 << endl;
cout << array << endl;
cout << *(&array +1) << endl;
return 0;
}
root#localhost:~/Source/c++# g++ arraySize.cpp
root#localhost:~/Source/c++# ./a.out
5
5
0x7fe2ec2800
0x7fe2ec2814
using https://www.gigacalculator.com/calculators/hexadecimal-calculator.php to subtract the numbers from each other
I get
14 hex
20 decimal.
that fits with the 4 bytes to an integer.
thanx guys :)
this is an edit done on the 12th of december melbourne time ::
I have still had questions on this topic and something did not fit right with me about the entire route to counting array items via this code.
I found something I think is interesting and again would love to know why ( I shall try to explain it as best I can my self anyway)
*(&array + 1) is the question.
lets have a look at it.
as arrays are at there very nature in c and c++ only pointers to the first element in the array how can this work.
I shall use a small set of cout calls to see if I can find out whats happening.
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
int array[] {1,2,3,4,5,6,7,8,9,10};
int size {0};
size = *(&array + 1) - array;
cout << "size = *(&array + 1) - array = " << size << endl;
cout << "*(&array + 1) = " << *(&array + 1) << endl;
cout << "(&array + 1) = " << (&array + 1) << endl;
cout << "(array + 1) = " << (array + 1) << endl;
cout << "&array = " << &array << endl;
cout << "array = " << array << endl;
cout << "*(&array) = " << *(&array) << endl;
cout << "*(array) = " << *(array) << endl;
cout << "*array = " << *array << endl;
return 0;
}
again this is off proot in my phone so still under root with no systemd.
root#localhost:~/Source/c++# g++ arrayPointerSize.cpp
root#localhost:~/Source/c++# ./a.out
size = *(&array + 1) - array = 10
*(&array + 1) = 0x7ff6a51798
(&array + 1) = 0x7ff6a51798
(array + 1) = 0x7ff6a51774
&array = 0x7ff6a51770
array = 0x7ff6a51770
*(&array) = 0x7ff6a51770
*(array) = 1
*array = 1
we see that as a pointer array can be called with * too derefernce the pointer and give the variable held in position [0] in the array.
when calling &array or reference too array we get the return of the address at the first position in the array or [0].
when calling just array we also get the address for the first position in the array or [0].
when calling *array the * is working as it does for pointers and it is dereferencing the arrays first position [0] to give the variable.
now things get a little interesting.
*(array) also dereferences the array as is seen by its value being given as 1 in this instance.
yet *(&array) does not dereference the array and returns the address to the first position in the array.
in this instance memory address 0x7ff6a51770 is the first spot in the array array = 0x7ff6a51770
and &array (reference to the pointer of the position in memory that is the first spot in the array) gives the same address 0x7ff6a51770.
it is also of note in this instance to remind us of the fact that *(&array) is also returning the first possition in the array and *(array) is not
so we can not dereference a pointer too a position in memory as its variable is the position in memory.
if array and &array give the same answer as array is a pointer too the memory position in the first spot in our array and a reference to
this pointer.
why the different answer for (array + 1) and (&array + 1).
we get the memory address 0x7ff6a51774 for (array + 1) which is in line with an integer taking four bytes on my linux or
the addition of four bytes in memory past the first spot in the array (second array spot) but (&array + 1) gives a different answer.
if we follow the bytes and the code we see that (&array + 1) actually gives us the memory address four bytes after the end of the array.
so pointer too memory address add one gives the amount of bytes the variable type is past the memory address for the start of the array
and the reference to the pointer too the memory address add one gives the address the amount of bytes the variable type is after the last ?? spot in the array.
how then can array and &array return the same answer if (array + 1) and (&array + 1) do not.
it seems to me that the & reference when working with arrays overloads the + operator when doing arithmatic.
this would explain the difference in answers as straight &array has no operator too overload so returns the same answer as calling for
straight array
this small peice of code also shows that the use of pointers using *(&array + 1) is a very bad way to show a way to find array size with
pointers as really arrays are pointers and *(&array + 1) and (&array + 1) give the same result.
the heavy work was really being done by the reference operator &.
I may still be missing something here as I have used cout directly with the different experssions and being a stream it may
be limited in its ability to take advantage of what the reference operator is really doing when working with arrays.
I am still learning this language but I shall for sure keep this in mind as I dive deaper into c++.
I believe other than a few other trials with variables that the true answer will be found when reading the source for GCC g++.
I am not ready for that yet.
I was learning how to find the length of an array and I'm baffled by this solution. I tried to find an explanation online but there seems to be none.
int arr[5] = {5, 8, 1, 3, 6};
int len = *(&arr + 1) - arr;
cout << "The length of the array is: " << len;
The memory address of the array is the same as the memory address of the first element, and when you add to or subtract from a pointer, it is done by the size of the type it points to, so:
arr refers to int, and &arr refers to int[5].
&arr+1 increments the memory address in the size of five integers.
If you do (&arr+1)-arr you get a compile error, because they are different types.
If you do (&arr+1)-&arr you get 1, because the offset of the memory address is the same as one size of int[5].
Therefore, when you do *(&arr+1), you get the same memory address but pointing to int and not int[5]. Now you wont get a compile error, because both pointers point to int and you get the offset of the memory address in terms of int size, and not int[5].
Memory addresses and types are quite difficult to explain sometimes, I hope I made it clear. Here you have some code you can run to see some of the concepts mentioned:
int arr[5] = {5, 8, 1, 3, 6};
int len = *(&arr + 1) - arr;
cout << "arr: " << arr << endl;
cout << "arr + 1: " << arr+1 << endl;
cout << "&arr: " << &arr << endl;
cout << "&arr + 1: " << &arr+1 << endl;
cout << "*(&arr + 1): " << *(&arr+1) << endl;
// cout << "&arr + 1 - arr: " << &arr+1-arr << endl;
// error: invalid operands of types ‘int (*)[5]’ and ‘int [5]’ to binary ‘operator-’
cout << "The length of the array is: " << len;
The type of the array arr is int[5], the type of &arr is int(*)[5]. (&arr + 1) increases the array address on sizeof(int[5]) as it's done by the rules of the pointer arithmetic, i.e. computes the address after the array. *(&arr + 1) is int[5], an array right after arr, where arr[5] would place. The both arguments of the substractions decay to int*. The substraction of pointers to int gives 5.
This may be considered as undefined behavior, since substraction of pointers belonging to different object storages is not defined. Also results of expressions with pointers addressing unallocated memory (like the (&arr + 1)) are undefined.
First, the traditional way to get the size of an array is sizeof a/sizeof *a. C++11 adds std::extent<decltype(a)>::value. There is of course no way to get an array’s size from just a pointer to it, as in
void f(int x[]) {/* no size here */}
Since an array is not a suitable operand to -, the array-to-pointer conversion occurs for the right-hand operand, producing an int*. Both operands must be of this type for the subtraction to result in a number of ints. &arr is of course a pointer to the array (of type int(*)[5], which conveys the size), and so therefore is &arr+1. Adding a * (or, equivalently, writing (&arr)[1]) produces an lvalue that supposedly refers to “the next array after arr”, which itself decays to a pointer that works with -.
However, as the indexing form indicates, this involves referring to an array that does not exist and is thus undefined behavior.
I have the following code:
#include <iostream>
using namespace std;
int main()
{
int g[] = {9,8};
int (*j)[2] = &g;
cout << "*(j):" << *(j) << endl;
cout << "j:" << j << endl;
cout << "&j:" << &j << endl;
cout << "&(*j)" << &(*j) << endl;
cout << "*(*j):" << *(*j) << endl;
return 0;
}
which ouputs:
*(j):0x7fff5ab37c7c
j:0x7fff5ab37c7c
&j:0x7fff5ab37c70
&(*j)0x7fff5ab37c7c
*(*j):9
I think that j is a pointer to an array of two integer.
And &g is the address of the whole array.
Then j store the address of the whole array.
And so I use the *(j), it will dereference the first element in the array.
But the result said that *(j) store the array address the same value as j.
I cannot figure out how this happened.
I think that j is a pointer to an array of two integer.
And &g is the address of the whole array.
That is correct.
And so I use the *(j), it will dereference the first element in the array.
This is not. *j gives you the array itself. When you insert it to cout, it decays to a pointer again (this time to a pointer to its first element, type int*) and its value is printed.
It's in effect the same as if you wrote cout << g.
"I think that j is a pointer to an array"
Yes, it is. And that's also the reason why *j output the same address as outputting g would do. In this case an array decays into the pointer and that's why even outputting j yields the same result.
The fact that the same address is outputted might make you think that j and *j are the same pointers, however they are not. Their type is different (a fact that actually matters):
int g[] = {9,8}; // int[]
int (*j)[2] = &g; // int (*)[2]
so using *j becomes equivalent to using g directly, just like *(*j) becomes equivalent to *g.
And &(*j) is nothing but j, which is initialized with an address of an array (an address taken from decayed g, i.e. an address of the first element of this array).
So why j and *j outputs the same address and *(*j) outputs the value of first element?
Because of the type of j being int (*)[2]. A simple example:
int g[] = {9,8};
int (*j)[2] = &g; // j points to the first element as well
cout << *((int*) j);
outputs 9.
This question already has answers here:
C pointer to array/array of pointers disambiguation
(13 answers)
Closed 8 years ago.
I have read the pointer to array c++
and have some experiment on it.
#include <iostream>
using namespace std;
int main()
{
int g[] = {9,8};
int (*j)[2] = &g;
int *i = g;
int n = (*j)[0];
int m = *(i + 0);
cout << "n:" << n << endl;
cout <<"m:" << m << endl;
cout << "=============" << endl;
cout << "*(j):" << *(j) << endl;//9's address ???
cout << "j:" << j << endl;//9's address ???
cout << "&j:" << &j << endl;//what it is???
cout << "&(*j)" << &(*j) << endl;//9's address ???
cout << "=============" << endl;
cout << "*(i):" << *(i) << endl;//get the value pointered by i
cout << "i:" << i << endl;//i's value store g's address
cout << "&i:" << &i << endl; //i's address
cout << "&(*i):" << &(*i) << endl;//9's address
cout << "=============" << endl;
cout << "*(g):" << *(g) << endl;//get the value pointered by g
cout << "g:" << g << endl;//g's value store 9's address
cout << "&g:" << &g << endl;//9's address???
cout << "&(*g):" << &(*g) << endl;//9's address
return 0;
}
The result is :
n:9
m:9
=============
*(j):0x7fff56076bbc
j:0x7fff56076bbc
&j:0x7fff56076bb0
&(*j)0x7fff56076bbc
=============
*(i):9
i:0x7fff56076bbc
&i:0x7fff56076ba8
&(*i):0x7fff56076bbc
=============
*(g):9
g:0x7fff56076bbc
&g:0x7fff56076bbc
&(*g):0x7fff56076bbc
[Finished in 0.3s]
What I want to ask is:
1.Why
int (*j)[2] = &g;
int *i = g;
if I change either one &g to g, or g to &g, it will give a compile error, what is the difference?
(I think that the array name is a pointer, so why cannot I use int (*j)[2] = g and what is the meaning of &g, it is a pointer already, why get address again?)
2.
Since the &g and g, their address are the same, why cannot be exchanged?
3.
Why the &g's value is 9's address? Is it store its own address?
4.
Can you explain the j variable part?
I cannot understand why j stores the 9's address and *(j) gets the 9's address, &(*j) gets the 9's address too?
I am very confused about it now. What I totally understand is about the i variable part,
g variable part is confused about the &g, and the j varialbe part is all confused.
The int (*j)[2] = &g; part is confused too.
You can have a look at this post to understand why g and &g hold the same address:
How array name has its address as well as its first element in it?
The compiler complains when you change either of g to &g because there is a type mismatch. This means that semantically, g and &g have different meanings, even though they may refer to the same thing: g, when used in an expression, decays into pointer to int (int *), but &g is of type pointer to array of 2 ints. These are not compatible types; you can't assign a pointer to an array of ints to something that is a pointer to int (and the same happens the other way around).
Your claim that g is already a pointer is wrong: g is an array. When used in an expression, it decays into a pointer to the array's first element. A pointer is just something that holds an address; an array is an address (the address of its first element). With &g, you get a pointer to an array, you don't get pointer to pointer because arrays do not decay into pointers when the referencing operator is used. Think of it like this: if an array is an address, then applying the "address of" operator - & - yields this same address, which is, by convention, the address of the first element. When you don't apply the & operator, in an expression, the array decays into a pointer to the first element, and then again - it points to the first element, the same address as &g.
g is the variable (in this case array), &g is the address of the variable (in this case the address of the beginning of the array. You might understand that, but the compiler does not, and thus it's giving you an error.
They probably could be exchanged, if you were willing to do type-casting, but that's not a really good idea in this case.
Because g is the array of integers, and the address of the first (0-th) element of that array just happens to be the address of the whole array. Doesn't it make sense?
j in your case is a pointer to the array, of course it is also a pointer to the address with the 0-th element of that array (by definition)
(*j)[2] is pointer to array of 2 integers. In other words it is (almost) equivalent with pointer to pointer to ints. The line int (*j)[2] = g; tries to assign int* to (int**) which is a type mismatch. The line int *i = &g; tries to assign int** to int* which is again a type mismatch.
(I think that the array name is a pointer, so why cannot I use int (*j)[2] = g and what is the meaning of &g, it is a pointer already, why get address again?)
The address of what? In its type checking C++ distinguishes address of variables of different types, so even though int* and int** are both addresses (pointers) they point to different types.
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int (*parr)[10] = &arr;
//prints address of arr and the value 1
cout << parr << " " << *parr[0];
//what is this doing?
parr++;
//prints (what looks like the address of arr[1]) and some long number -8589329222
cout << parr << " " << *parr[0];
I thought parr++ would increment the address that parr is pointing to so that *parr[0] is now the address of *parr[1]. Where am I wrong?
You're assuming parr++ increments by one word. It doesn't. It increments by the size of *parr, which in thise case is an int[10], so it's incrementing by the size of 10 integers (probably 40 bytes).
You only need a pointer to the start of the array.
int* parr = arr; // points to the 0 element
parr++; // poInts to the first element, 1.