When I make an array of integer pointers, I tried this.
int *arr = new int*[10];
This did not work but the following worked.
int **arr = new int*[10];
Why do we need double pointer?? And when I deference it, I had to do the following.
cout<<arr[0];
Why we do not need * in front of arr??
thanks!
new int*[10] allocates an array of ten pointers, and it yields a pointer to the first element of that array. The element type is itself a pointer, that's why you end up having a pointer to a pointer (to int), which is int**. And obviously int** isn't convertible to int*, so you have to declare arr with the appropriate type.
You are not just "making an array of integer pointers": you are dynamically allocating them.
Just like when you dynamically allocate an array of integers you get a single pointer through which to access them:
int* ptr = new int[5];
when you dynamically allocate an array of pointers-to-integer you get a single pointer through which to access those, too; since your element type is int*, adding the extra * gives you int**:
int** ptr = new int*[5];
As for dereferencing, I'm not quite sure what you're asking but that's just how the [] operator works; it adds n to a pointer then dereferences it:
int* ptr = new int[5];
*(ptr+1) = 42; // identical
ptr[1] = 42; // to this
If you forget dynamic allocation and just make a nice array, it's all much simpler:
int* array[5];
std::cout << array[0];
This statement is an expression for an 1D array of int
int* arr = new int [10]; // pointer to array of 10 int
This statement is an expression for a 2D array of int
int** arr = new int* [10]; // pointer to 10 pointers to arrays of 10 int
To populate the 1D array, you need to do this...
for (int i = 0; i < 10; i++) {
arr[i] = val; // where val can be any integer
}
To populate the 2D array, you need to do this...
int** arr2 = new int*[10];
for (int i = 0; i < 10; i++) {
arr2[i] = new int[10];
for (int j = 0; j < 10; j++) {
arr2[i][j] = val; // where val can be any integer
}
}
The * symbol between the variable type and the variable name is syntax for pointer. It changes type from int to pointer of int.
Related
int **v = new int*[n];
I'm confused as to what this does? could someone please explain?
This allocates an array of n pointers to int. A pointer to the first element in this array of pointers is stored in v. It is a double pointer, such that accessing an element via v[i] returns a stored pointer from the array.
it's not correctly complete:
int **v = new int*[n];
we'd think this means to allocates dynamically array of integer array on memory, so the dimension must suit it
statically equivalent
const size_t n=5;
int *v[n] = {} ;
// or
int v[][n] ={ {1,2,3,4,5}, {6,7,8}, {9,8,7,6,5} }; //2 dimensional array
as max size of first dimension is inferred automatically to be 3 but
I guess IMHO, this is not yet given,
int **v = new int*[n];
so it'd be specified as
int **v = new int*[n * 3];
What's the meaning of the code below?
int **matrix = new int*[n]
What's the difference here between matrix and int*[n]?
It is an array of 'n' pointers, for which memory can be allocated and initialized in loop.
If n is 3, it is an array of 3 elements and each is pointer to int, can point to set of array of integer values like below.
matrix[0] -> Ox001 points to array of int [ 1 2 3 4]
matrix[1] -> Ox017 [ 5 6 7 8]
matrix[2] -> Ox024 [ 9 10 11 12]
Sample code like this
int **m = new int*[3];
for(auto i=0; i < 3; i++)
{
m[i] = new int[3];
for(auto j=0; j < 3; j++)
m[i][j] = 0;
}
for(auto i=0; i < 3; i++)
{
m[i] = new int[3];
for(auto j=0; j < 3; j++)
cout << m[i][j];
cout << "\n";
}
for instance there is cricket team and you need
Since you have Cricket* team;, this indicates you have one of two possible
situations:
1) a pointer to a single CricketPlayer (or any derived) type
2) a pointer to an array of CricketPlayer (but not derived) types.
What you want is a pointer to an array of CricketPlayer or derived types. So you
need the **.
You'll also need to allocate each team member individually and assign them to the array:
// 5 players on this team
CricketPlayer** team = new CricketPlayer*[5];
// first one is a bowler
team[0] = new Bowler();
// second one is a hitter
team[1] = new Hitter();
// etc
// then to deallocate memory
delete team[0];
delete team[1];
delete[] team;
In your query,
It can be understood as
int *matrix[]=new int*[n];
SO there are n pointers pointing to n places.
Because
int *foo;
foo=new int[5];
will also create 5 consecutive places but it is the same pointer.
In our case it is array of pointers
You need to notice some thing as follows:
int *p means that p is a pointer that points to an int variable or points to an array of int variables.
int* *p means that p is a pointer that points to an int* variable or points to an array of int* variables.
new int[5] is an array of 5 int variables.
new int*[5] is an array of 5 int* variables.
In this case, matrix is the second type so it can point to an array of int* variables so the statement:int **matrix = new int*[n] is legal
It means that you have declared a double pointer(of type int) called matrix and allocated an array of n int* pointers. Now you can use matrix[i] as an int* pointer (0 <= i < n). Later you might want to allocate memory to individual pointers as well like matrix[i] = new int[size];(size is an int or more appropriately size_t)
matrix is an object of type int **. This type is a pointer to pointer to int.
On the other hand, int * is a type, a pointer to int.
The expression:
new int*[n]
creates n adjacent objects of type int * somewhere in memory and returns a pointer to the first of them. In other words, it allocates and constructs n pointers to int; and returns a pointer to the first. Since the first (and each of them) are int *, the pointer pointing to the first is, in turn, of type int **.
It would be absolutely clear if you remember the operator associativity and precedence.
Going by that int *[n] will be interpreted by compiler : as array of " n pointer to integers".(as [ ] has greater precedence than *, so for easy understanding, compiler interprets it that way.).
Now comming to other side int ** matrix is pointer to pointer.
When you created an array of pointer,you basically have the address of the first location ,here first location stores an pointer to integer.
So,when you are storing the address of the first address of such array you will need pointer to a pointer to point the whole array.
//code for passing matrix as pointer and returning as a pointer
int ** update(int ** mat)
{
mat[0][1]=3;
return mat;
}
int main()
{ int k=4;
int **mat = new int*[k];
for(auto i=0; i < k; i++)
{
mat[i] = new int[k];
for(int j=0;j<k;j++)
{
if(i==j)
mat[i][j]=1;
else
mat[i][j]=0;
}
}
mat=update(mat);
for(auto i=0; i < k; i++)
{
for(auto j=0; j < k; j++)
cout << mat[i][j];
cout << "\n";
}
return 0;
}
So I have this code in here I've try to create a 2-D array with different sizes,
first I declare 3 pointers then I assign different arrays to them and hope it works and it did well, but there is the problem , in the second code the compiler gives an error ( a value of type int* cannot be assigned to an entity type int ) so it means they are no longer pointers I think , but why is that , what am I missing here ? what is the biggest difference in these two codes other than one of them is declared in stack and other is on the heap
int main()
{
int* arr[3];
arr[0] = new int[5];
arr[1] = new int[2];
arr[2] = new int[6];
delete[] arr[0];
delete[] arr[1];
delete[] arr[2];
}
//2nd Code
int main()
{
int* arr = new int[3];
arr[0] = new int[5];
arr[1] = new int[2];
arr[2] = new int[6];
}
and sorry for my baddd English
In the first program there is declared an array of pointers
int* arr[3];
In the second program there is allocated an array of integers
int* arr = new int[3];
So for example the expression
arr[0]
has type int.
If you want to allocate an array of pointers you should write
int ** arr = new int *[3];
^^ ^
In this case the expression
arr[0]
has type int * and you may write
arr[0] = new int[5];
So the lecture example has the following code
int **a;
a = new int*[10];
for (int i = 0; i < 10; ++i){
a[i] = new int[5];
for (int j = 0; j < 5; ++j){
a[i][j] = i*10+5;
}
}
And I have a few questions about the above code (and multidimensional arrays on the heap in general):
Do we have to initialize every element in the array as shown? Or is it suffice to cut the code after the a[i] = new int [5]?
I know with arrays allocated on the stack, it is illegal to use a variable as the stack size as
cin >> n;
int a[n];
but is it legal for heap-allocated arrays? As in, a = new int[n]?
Why is it a double pointer pointing to this array? Usually for 1D arrays, we used a single pointer as int *a = new int[10]? If I wanted the value of the array element itself, do I deference twice as **a? Or do I still do *a?
So say I have a 2D array of objects of some Class. How would I access the member field var of the ith, jth element? What if I want to invoke the member function of the object in the ith, jth element?
You don't have to initialize every element in the array. If you don't, the contents of the array will be undefined. Alternatively, you can zero-initialize with new int[5]().
a = new int[n] works on the heap because there's actually a run-time call to mark new memory for use as the data a is pointing to. It can't work on the stack because the compiler needs to know how big the stack frame is for any particular function call, so the value must be computable at compile time.
You need a double pointer because a is a pointer to a pointer to an integer. It points to an array of arrays, each of which needs to be dereferenced when accessing the value. You would typically use a[i][j] to access a specific element, which effectively double-deferences.
If the number of columns in the matrix is known at compile time, you can just allocate a matrix:
int nrow = 10; // number of rows assigned at run time
int (*a)[5]; // pointer to array of 5 ints
a = new int[nrow][5]; // allocate matrix
for (int i = 0; i < nrow; ++i) // initialize matrix
for (int j = 0; j < 5; ++j)
a[i][j] = i*nrow+j;
// ...
delete[] a;
I have a dynamic matrix in a class created by allocating memory in this way:
int **m; //this in the member head pointer
void allocate_mem(int ***ptr, unsigned r, unsigned c){
*ptr = new int *[r];
(*ptr)[0] = new int[r*c];
for(unsigned i = 1; i < r; i++)
(*ptr)[i] = (*ptr)[0] + i*c;
}
how can I call the pointers to the rows? I mean, m is the pointer to the array of pointers, *m is the pointer to the first row, but I don't know how to call the pointers to the other row
*m is the pointer to the row with index 0 indeed, but *m is equivalent to m[0]. So for other indexes use m[index]