Could anyone tell me why the second and the last receive compile-time errors?
void test(){
int array[10]{};
int (*i) [10] = &array; // works
int (*j) [10] = array; // does not work
int *k = array; // works
int *l = new int[10](); // works
int (*m) [10] = new int[10](); // does not work
exit(0);
}
I'm not sure why the second does not work without the ampersand since array and &array refer to the same address. For the last, I think it's because it can't tell we're dynamically allocating an array specifically, but I'd like this confirmed.
I'm not sure why the second does not work without the ampersand since array and &array refer to the same address.
int (*j) [10] = array;
array decays to int* not int (*)[10] in several contexts. So even though the value of array and &array is same the types are different. The type of array before decaying is int [10] which decays to int* while the type of &array is int (*)[10].
So the error is telling you that we cannot initialize a int(*)[10](type on the left hand side) with an int*(type on the right hand side after decay).
error: cannot convert ‘int*’ to ‘int (*)[10]’ in initialization
Similarly in the last case int (*m) [10] = new int[10]() the type on the right hand side is int* which cannot be used to intialize the type on the left hand side(int (*)[10]).
Related
When i tried to access array elements using pointer I got this error i don't understand how can i access with the help of pointer.
Error: cannot convert ‘int (*)[5]’ to ‘int*’ for argument ‘1’ to ‘void addd(int*, int)
Any guidance on the matter would be greatly appreciated!
#include <iostream>
using namespace std;
void addd(int *ptr,int length)
{
cout<<"The values in the array are: ";
for(int i = 0; i < length; i++) {
cout<< *ptr <<" ";
ptr++;
}
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
addd(&arr,5);
//int *ptr = &arr[0];
return 0;
}
Your function receives a pointer to int, so you should just pass the address of the first element.
You can do that:
addd(&arr[0],5);
or simply:
addd(arr,5);
You're doing it wrong. &arr means int (*)[5], but you are using int *ptr. So, you've to use like below:
addd(arr, 5); // this is correct
// addd(&arr, 5) // this is wrong
Here, arr is passed as int *
When you pass an array to a function, for example:
addd(arr,5);
then the array is implicitly converted to a pointer to its first element, an int*. Instead of relying on the implicit conversion, you can make it explicit:
addd( &arr[0],5);
// ^- first element
// ^------ address-of first element
However, arrays are not pointers and taking the address of the array yields a pointer to an array:
using pointer_to_array = int (*)[5];
pointer_to_array p = &arr;
arrays and pointers are pretty much inter-related to each other. An array by default points to the address of the first element, that is the element at index 0. So, working with arrays and pointers are pretty much the same thing( not every time ). So, you do not need to pass on the array as
addd(&arr,5);
you should simply do just
addd(arr,5);
Also , i will say,
The statement
addd(&arr[0],5); does the job, but not a good way to proceed.
So I know that a pointer is:
int *someNumber;
but I have run accross some code with:
int* pOutputData = nullptr;
byte* pOutputDataByte = nullptr;
What does int* mean?
Is that some kind of array of int? Is it a pointer to the type?
int * and int* are the same thing. The whitespace doesn't change anything.
int * can be used either as a pointer to int or as a pointer to the first element of an array of ints. How a variable is used is context-dependent. Judging by the variable names, I'd guess that someNumber points to a single int and pOutputData points to an array.
Both are same
White space does not mean anything in c
int * num ;
int *num;
int* num;
All the above statements are same!
We use int* as default; mainly in data structure, to avoid confusion.
Always read pointer variables from right to left
int *p means p is a pointer to an int
char *p means p is a pointer to a char
const int *p means p is a pointer to an integer constant
int * const p means p is constant pointer to an int
I am having problems understanding how to assign to the Test array as shown below:
int (*&Test)[10] = Parray; Test is a reference to a pointer to an array of ten ints.
The error I get is as follows:
error: incompatible types in assignment of 'int*' to 'int [10]'|.
I have done my research without understanding this completely. I'm reading C++ Primer 5th edition.
int main() {
int arr[10];
int n = 5;
int *ptr1 = &n;
int arr2[10];
int *ptrs[10]; // ptrs is an array of ten pointers
// Parray points to an array of ten ints
int (*Parray)[10] = &arr;
// arrRef refers to an array of ten ints
int (&arrRef)[10] = arr2;
// Test is a reference to a pointer to an array of ten ints.
int (*&Test)[10] = Parray;
// How can I assign to Test[0..1..2..etc]?
// This is what I am trying to do:
Test[0] = ptr1; // Error here
return 0;
}
How can I assign to Test[0] etc.?
Use the followi ng expression statement
Test[0][0] = *ptr1;
The type of expression Test[0] is int [10]. So Test[0][0] will have type int and *ptr1 has type int Of course ptr1 shall have a valid value that may be dereferenced.
It should be:
(*Test)[0] = 3;
(*Test)[1] = 5;
etc. Alternatively you can write Test[0][0] = 3; Test[0][1] = 5; however I think that is less clear.
Test is a reference to the same type as Parray. Dereferencing this gives an array of 10 int, which you can then use array syntax on.
I came across this question:
In the declaration below , p is a pointer to an array of 5 int
pointers.
int *(*p)[5];
which of the following statements can be used to allocate memory for
the first dimension in order to make p an array of 3 arrays of 5
pointers to type int ?
A. p = new int [3][5]*;
B. p = new int (*)[3][5];
C. p = new int [3]*[5];
D. p = new int *[3][5];
E. p = new int (* [3] ) [5];
What is the answer ?
I am not sure I understand the question. Normally I would create a pointer to an array of 5 int as such int* p[5]; I am curious as to why they did it as int *(*p)[5];
Also what does the question want ? Is it asking to initialize (allocate memory) to the first 3 int pointers ? I would appreciate it if someone could explain this to me
F:
using IPA5 = int*[5];
IPA5 * p = new IPA5[3];
Each element p[0], p[1], p[2] is just a plain, typed array of int*. There's nothing dynamic going on beyond the initial dynamic allocation, where 3 is allowed to be a dynamic quantity.
Then p[0][i] for i in [0, 5) is an int *, which you can use in whatever way you like (which includes making it point to the first element of yet anohter dynamic array).
What you would write as:
int* p[5];
is a five element array of pointers to int.
What this declares:
int *(*p)[5];
is a pointer to a five element array of pointers to int, i.e. a pointer to the type of thing you just wrote.
In other words; you could do:
int * a[5];
int * (*p)[5] = &a;
You can mentally read this incrementally as follows:
(*p) // p is a pointer
(*p)[5] // p is a pointer to an array of size 5
int * (*p)[5] // p is a pointer to an array of size 5 of type pointer to int
You need the parentheses around *p, because otherwise:
int ** p[5];
would declare a 5 element array of type int **, or pointer to pointer to int, which is a different thing entirely.
The question is basically asking you to dynamically allocate memory equivalent to three of what a is above, so answer "D" is the correct one.
The answer is
D. p = new int *[3][5];
all the others are syntactically wrong
to realize the difference between
int * p [5];
int * (*p) [5];
consider this example
int *(*p)[5];
int pp[5];
pp[0][0] = new int [5]; //LHS is int , RHS is int ,, compilation error
p[0][0] = new int [5]; //this works because p[0][0] is a pointer not an int
try thinking about each dimension as adding you additional *
back to the question
int *(*p)[5] is giving you 3 * (***p)
so you can assign
p = int *[3][5]
because it has 3 * as well
int* p[5] has type array of size 5 of int*. It decays to int**, so p + 1 will point to the second element of that array.
int *(*p)[5] has type pointer to array of size 5 of int*. You can think of it as decayed two-dimensional array int* [][5]. So p + 1 will point to the second element of the first dimension of that array, that is to the next byte after 5 pointers to int.
Which leads us to the conclusion that the right answer is D.
(This is not to mention that other answers just don't compile regardless of type of p)
What is the answer ?
D
Normally I would create a pointer to an array of 5 int as such int* p[5]; I am curious as to why they did it as int *(*p)[5];
It is not "normally" because int* p[5] is not a pointer to an array of 5 int, it is an array of 5 pointers to int.
Also what does the question want ? Is it asking to initialize (allocate memory) to the first 3 int pointers ?
It's not clear. There is no way "to make p an array of 3 arrays of 5 pointers to type int", to begin with.
I don't quite understand where the error is here:
int *parr[22]; // Array of int* pointers
parr[0] = ptr1;
parr[1] = ptr2;
//...
int *(*pparr)[22]; // A pointer to a int* array[22]
pparr = parr; // ERROR
the error tells me error C2440: '=' : cannot convert from 'int *[22]' to 'int *(*)[22]'
how come that the types are not equal? The name of the array should be equal to a reference to the first element of the array, something like
parr => &parr[0]
so the line seems right to me
As pparr is A pointer to a int* array[22] so you need to write
pparr = &parr;
You need to store address in the pointer and not the pointer itself.
It is same like when you have
int a=3;
int *b;
b=&a;
You are storing address of a in b, similarly you need to store address of parr in pparr
EDIT: To clarify OP's comment
You can't assign the address of the first element, but the address of the pointer that is pointing to first element.(therefore pparr = &parr;)
An int*[22] can decay to an int**, but you cannot assign an int** to an int*(*)[22].
int *(*pparr)[22]; //This one is an array of function-pointers returning an int pointer.
int **pptr; //Points to an array of pointer
So you can write
pptr = parr;