C++ Passing by pointer and passing by reference - c++

#include<iostream>
using namespace std;
class A{
int *numbers[5];
public:
void assignment(int ** x){
for(int i=0;i<5;i++)
numbers[i]=x[i]; //not changing just the value of *numbers[i] but the pointer numbers[i]
}
void print(){
for(int i=0; i<5;i++)
cout<< *numbers[i]<<endl;
}
};
int main(){
int *x[5];
for(int i; i<5;i++){
x[i]= new int(i);
cout<<*x[i]<<endl;
}
cout<<endl;
A numbers;
numbers.assignment(x);
numbers.print();
return 0;
}
My question is very specific. I want to do the same thing as the code above but instead of passing the argument of function assignment(int **) by pointer to do it by reference. How can I achieve that?

Use:
void assignment(int* (&x)[5]) ...
edit: for the comment "if the length... wasn't standard...", you can use a template:
template<int N> void assignment(int* (&x)[N]) ...
The compiler will automatically deduce N.

Related

couldn't deduce template parameter ‘looptype’

#include<cstdlib>
#include<iostream>
#include<math.h>
#include<string>
using namespace std;
class getAverage{
public:
template<class add>
add computeAverage(add input[], int nosOfElem){
add sum = add();//calling default constructor to initialise it.
for(int index=0;index<=nosOfElem;++index){
sum += input[index];
}
return double(sum)/nosOfElem;
}
template<class looptype>
looptype* returnArray(int sizeOfArray){
looptype* inputArray= (int*)malloc(sizeof(int)*sizeOfArray);
for(int index=0;index<=sizeOfArray;++index){
inputArray[index]=index;//=rand() % sizeOfArray;
}
return inputArray;
}
};
int main(){
int sizeOfArray=2;
int inputArray;
getAverage gA;
int* x= gA.returnArray(sizeOfArray);
for(int index=0;index<=2;++index){
cout<<x[index];
}
cout<<endl;
cout<<gA.computeAverage(x,sizeOfArray);
free(x);
return 0;
}
I want to create a template function through which I can create dynamic arrays of different type(Int,long,string ..etc.). I tried doing it,and realised that the "looptype" never gets the type value. Can someone suggest a way to do this.
Thanks
template<class looptype>
looptype* returnArray(int sizeOfArray){
looptype* inputArray= (int*)malloc(sizeof(int)*sizeOfArray);
for(int index=0;index<=sizeOfArray;++index){
inputArray[index]=index;//=rand() % sizeOfArray;
}
return inputArray;
}
template parameters can only be deduced from the function-template arguments. Here the only argument your function template takes is sizeOfArray which is an int. How does the compiler know what typename looptype is? Since it cannot be deduced, you have to explicitly specify it.
int* x= gA.returnArray<int>(sizeOfArray);
rather than:
int* x= gA.returnArray(sizeOfArray);
BTW, what's the point of have a template parameter looptype when I know it can only be an int as sold by this line of your code:
...
looptype* inputArray= (int*)malloc(sizeof(int)*sizeOfArray);
...
Your use of malloc is scary. For virtually same performance, you are making simple tasks complicated. Prefer std::vector<int> or worse case std::unique_ptr<int[]>.

passing 1d and 2d arrays by reference in c++

I am new to c++ and have a couple questions regarding passing arrays by reference to functions (so that the arrays are modified by the function). I realize there are similar questions that have been asked already, but there are a few points that I think were not covered in those previous questions (at least from what I saw). From what I have gathered so far, one can pass an array by reference by doing the following:
#include<iostream>
using namespace std;
void modify_array(int* a);
int main()
{
int array[10];
modify_array(&array[0]);
for(int i=0;i<10;i++)
{
cout<<array[i]<<endl;
}
}
void modify_array(int* a)
{
int i;
for(i=0;i<10;i++)
{
*(a+i)=i;
}
}
This makes sense to me but if I change the function to:
void modify_array(int* a)
{
int i;
for(i=0;i<10;i++)
{
a[i]=i; //line changed
}
}
This also works. Is there a difference? Or is the second just a short cut? Also in the case of passing 2d arrays I would have guessed that the following code would work:
#include<iostream>
using namespace std;
void modify_array(int* a);
int main()
{
int array[10][10];
modify_array(&array[0][0]);
}
void modify_array(int* a)
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
a[i][j]=i*j;
}
}
}
But this doesn't. From what I have seen in other related questions, you would do something like:
void modify_array(int (*a)[10])
{
int i,j;
//a[i][j]= blah blah blah;
}
or,
void modify_array(int (&a)[10][10])
{
int i,j;
//a[i][j]= blah blah blah;
}
What is the difference between these latter two function definitions? What do experienced c++ programmers recommend using: the (*a)[10][10] notation or the (&a)[10][10] notation?
Writing *(a+i)=i; or a[i]=i; are equivalent. The first is seen as an offset applied to the pointer to the array and assigning the value to the pointee, while the second is assigning the value to the element of the array.
However, when passing a pointer to a function modify_array(int* a), it cannot deduce that the pointee is a 2D array, and does not know what size to offset to address the other lines of the array, with a[i][j]=i*j;. For the compiler, it can only access the first dimension of the array.
The proper way to do what you need is this
#include<iostream>
using namespace std;
void modify_array(int (&a)[10][10]);
int main()
{
int array[10][10];
modify_array(array);
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
cout<<array[i][j]<<endl;
}
}
}
void modify_array(int (&a)[10][10])
{
int i;
for(i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
a[i][j]=i*j;
}
}
}
Live example
The function is expecting an int array of 10x10, passed by reference.

error in passing 2-d array to function

The program is just for passing complete 2-d array to function.I am able to run the problem by hook or by crook but i didnt understood.I have written a program which i should have written threotically and which i have written to make it working(in comments)
can anyone please explain me this issue??
#include<iostream>
#include<conio.h>
void print(bool *a);
using namespace std;
int main()
{
bool arr[3][3]={1,1,1,1,0,0,0,1,1};
print(arr[0]);//**This IS working but why we need subscript 0 here only print(arr) should work?..**
getch();
return 0;
}
void print(bool *a)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<*(a+i*3+j)<<"|";//**cant we use cout<<a[i][j] here??In 1 d array it is working fine**
}
cout<<"--";
}
}
void print(bool *a)
should be
void print(bool a[][3])
the compiler needs to know the size of second dimension in order to compute offset for addressing.
void print(bool a[][3], int rowSize)
{
for(int i=0;i<rowSize;i++)
{
for(int j=0;j<3;j++)
{
cout<<a[i][j]<<"|";
}
cout<<"--";
}
In C++, you should prefer using vector<vector <bool> > over 2D dynamic array arr.
Use:
void print(bool a[][3])
which is the correct prototype if you want to call print(arr);
Then you can use a[i][j] to access array elements in the print function body.
arr is an array of array 3 of bool and when passed to print function call the arr expression is converted to a pointer to an array 3 of bool.

Initialize empty array in class

I've just tried:
class Test
{
public:
int iArray[][];
}
...is this not possible? Do I have to set a constant value?
like:
class Test
{
public:
const int iArray[5][4];
}
I want to define [x][y] later, just have the placements there. Else it wouldn't be "dynamic" and I don't want to use a vector because I want to be able to access the values by "X" and "Y".
I think better way to achieve this is to use pointers. You can do like this.
#include <cstdlib>
#include <iostream>
using namespace std;
class PointerTest {
private:
int** array;
int x, y;
public :
void setValue(int row, int col,int value);
int getValue(int row, int col);
PointerTest(int row, int col);
~PointerTest() {
for(int i=0;i<x;i++) {
delete array[y];
}
}
};
PointerTest::PointerTest(int row, int col) {
x=row, y=col;
for(int i=0;i<row;i++) {
*array=new int[col];
}
}
void PointerTest::setValue(int row, int col, int value) {
*(array[row])=value;
}
int PointerTest::getValue(int row, int col) {
return *(array[row]);
}
int main(int argc, char *argv[])
{
PointerTest* t=new PointerTest(4,5);
t->setValue(0,0,464);
cout<<"The value in array: "<<t->getValue(0,0)<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
What about
tempalte <int N1, int N2> class Test
{
public:
int iArray[N1][N2];
};
?
What about putting a std::vector in a vector?
std::vector< std::vector< const int > > iArray;
There aren't many reason to use "plain" arrays in C++.
If you want to decide int iArray[][]; size later then you can use vector< vector<int> > iArray;.
The other way is to use nested new[], which would be little complex.
No this is not possible. But you can have a pointer in your class like
int **ptr;
and then in the constructor or where ever allocate the memory for your array with
ptr = (int **)malloc( the size you want );
or with the "new[]"-operator in C++.
but if you are using C++ .. the best way is to use:
std::vector< std::vector< int >> array;
class Test
{
public:
Test()
{
iArray = new int*[5];
for(int i = 0; i < 5; i++)
iArray[i] = new int[4];
}
~Test()
{
for(int i = 0; i < 5; i++)
delete[] iArray[i];
delete[] iArray;
}
int** iArray;
};
Will allow you to allocate a 2d int array at runtime (in this example it is a 5x4), but in all honestly I would use vectors as pointed out by some other posters, you don't need to worry about freeing the memory afterwards like you do with the use of new.

C++ passing an array pointer as a function argument

I'm trying to use pointers of arrays to use as arguments for a function which generates an array.
void generateArray(int *a[], int *si){
srand(time(0));
for (int j=0;j<*si;j++)
*a[j]=(0+rand()%9);
} //end generateArray;
int main() {
const int size=5;
int a[size];
generateArray(&a, &size);
return 0;
} //end main
But when I compile this this message appears:
cannot convert `int (*)[5]' to `int**' for argument `1' to `void generateArray(int**, int*)'
You're over-complicating it - it just needs to be:
void generateArray(int *a, int si)
{
for (int j = 0; j < si; j++)
a[j] = rand() % 9;
}
int main()
{
const int size=5;
int a[size];
generateArray(a, size);
return 0;
}
When you pass an array as a parameter to a function it decays to a pointer to the first element of the array. So there is normally never a need to pass a pointer to an array.
int *a[], when used as a function parameter (but not in normal declarations), is a pointer to a pointer, not a pointer to an array (in normal declarations, it is an array of pointers). A pointer to an array looks like this:
int (*aptr)[N]
Where N is a particular positive integer (not a variable).
If you make your function a template, you can do it and you don't even need to pass the size of the array (because it is automatically deduced):
template<size_t SZ>
void generateArray(int (*aptr)[SZ])
{
for (size_t i=0; i<SZ; ++i)
(*aptr)[i] = rand() % 9;
}
int main()
{
int a[5];
generateArray(&a);
}
You could also take a reference:
template<size_t SZ>
void generateArray(int (&arr)[SZ])
{
for (size_t i=0; i<SZ; ++i)
arr[i] = rand() % 9;
}
int main()
{
int a[5];
generateArray(a);
}
You do not need to take a pointer to the array in order to pass it to an array-generating function, because arrays already decay to pointers when you pass them to functions. Simply make the parameter int a[], and use it as a regular array inside the function, the changes will be made to the array that you have passed in.
void generateArray(int a[], int si) {
srand(time(0));
for (int j=0;j<*si;j++)
a[j]=(0+rand()%9);
}
int main(){
const int size=5;
int a[size];
generateArray(a, size);
return 0;
}
As a side note, you do not need to pass the size by pointer, because you are not changing it inside the function. Moreover, it is not a good idea to pass a pointer to constant to a parameter that expects a pointer to non-constant.
I'm guessing this will help.
When passed as functions arguments, arrays act the same way as pointers. So you don't need to reference them. Simply type:
int x[]
or
int x[a]
. Both ways will work. I guess its the same thing Konrad Rudolf was saying, figured as much.
This is another method . Passing array as a pointer to the function
void generateArray(int *array, int size) {
srand(time(0));
for (int j=0;j<size;j++)
array[j]=(0+rand()%9);
}
int main(){
const int size=5;
int a[size];
generateArray(a, size);
return 0;
}