Reverse Pointer function issue - c++

I'm having an issue with the pointer return function. The error, "calling object type 'int* ' is not a function or a function pointer" reverseArray = reverseArray(array,size);.I am not sure why it's giving me this error, this is not my solution I'm using this solution as a guidance to help me solve the problem. Since I've sat for now 2 hours trying to solve it and I got no where with it, so I decide to look it up and get an idea on how to approach the problem. And break down their solution by using a debugger to see why their solution works. I know it's a bad thing to do because I'm not learning how to solve problems on my own.
#include <iostream>
int* reverseArray(int [], int );
int main()
{ const int size =5;
int array[size] = {1,2,3,4,5};
int* reverseArray;
for(int i =0; i < size;i++)
{
std::cout << array[i];
}
reverseArray = reverseArray(array,size);
for(int i =0; i <size;i++)
{
std::cout <<array[i];
}
return 0;
}
int* reverseArray(int array [],int size)
{
int* newArray;
newArray = new int[size];
int j = 0;
for(int k =size-1;k>=0;k--)
{
newArray[j] = array[k];
j++;
}
return newArray;
}

The error is self explanatory.
"calling object type 'int* ' is not a function or a function pointer". In you main() function, you named the array you want to pass as a parameter to your reverseArray() function with the same name as your function (reverseArray). The compiler get confused within that scope, because of this and thinks you're calling a variable as a function.
See below:
#include <iostream>
int* reverseArray(int [], int );
int main()
{ const int size =5;
int array[size] = {1,2,3,4,5};
int* reverseArray; // Change this name to something else
for(int i =0; i < size;i++)
{
std::cout << array[i];
}
reverseArray = reverseArray(array,size);
for(int i =0; i <size;i++)
{
std::cout <<array[i];
}
return 0;
}
Hope it helps :)

Related

Use an array in main() in another function in c++

Hello I have a task to make a function that fills an array, made in main(), and then use the array in another function, where I should make some calculations with it. I already have some code, could you please assist me how to do it? Thank you in advance
#include <iostream>;
using namespace std;
int* modArray(int* a, int size) {
for (int i = 0; i < size ; i++) {
a[i] = i;
}
return a;
}
int modAddition() {} // This is the function, where I want to use, the "a" array
int main() {
int size = 7;
int* a = new int[size];
a = modArray(a, size);
system("pause");
return 0;
}
Your modArray works, and the return steatment is not needed in this case. You can just do the same in your modAddition. Rembemer that if they don't have a return, they will be void methods.

Referencing Elements of Class Arrays using C++

I am trying to switch the elements of a class array using pointers. It is not outputting what I want. I tried using pointers in the function, but it's not allowed. It's also not allowed to call the function onto the class object without using a pointer, since I declared the class object using a double pointer. I am not using this method simply to solve a small problem, but just to practice using this method for more difficult problems.
Here is my code:
#include <iostream>
#include <algorithm>
using namespace std;
class thing{
public:
int index;
int value;
thing();
private: int number;
};
thing::thing()
{
number = 0;
}
void arrange(thing array[]){
for(int i=0; i<19; ++i){
if(array[i].value<array[i+1].value){
swap(array[i], array[i+1]);
arrange(array);
}
}
}
int main(){
thing** things = new thing*[20];
for (int i=0; i < 20; ++i)
{
things[i] = new thing(); // default constructor
things[i]->index = i;
things[i]->value=rand() % 100;
}
cout << "The random array is: " << endl;
for(int i=0;i<20;++i){
cout << things[i]->value << endl;
}
arrange(*things);
cout << "The arranged array is: " << endl;
for (int i=0; i < 20; ++i)
{
cout << things[i]->value << endl;
}
return 0;
}
When you call arrange(*things), you're just passing the first element of things to the function, not the array. It should be array(things). Then the arrange function should be written to use pointers:
void arrange(thing* array[]){
for(int i=0; i<19; ++i){
if(array[i]->value<array[i+1]->value){
swap(array[i], array[i+1]);
arrange(array);
}
}
}
Here you create an array of pointers to thing:
thing** things = new thing*[20];
Here you dereference it and get a pointer to thing which is stored at thing[0]:
arrange(*things);
But this function declaration
void arrange(thing array[])
treats this pointer as an array of thing, so that *things points to it first element, which is absolutely not what it really is.
You should change your arrange() function to use correct type:
void arrange(thing* array[]){
for(int i=0; i<19; ++i){
if(array[i]->value<array[i+1]->value){
swap(array[i], array[i+1]);
arrange(array);
}
}
}
And call it as:
arrange(things);
Regarding using vectors, you don't need to use any pointers at all.
std::vector<thing> things(20);
for (int i=0; i < things.size(); ++i)
{
things[i].index = i;
things[i].value=rand() % 100;
}
arrange(things);
void arrange(std::vector<thing>& array){
for(int i=0; i + 1 < things.size(); ++i){
if(array[i].value<array[i+1].value){
swap(array[i], array[i+1]);
arrange(array);
}
}
}

c++ two errors while compiling

I'm a beginner in c++ and I'm getting two errors in my code and I don't know how to fix them...
the first one
illegal indirection
and the second one is
'=' left operand must be a I-value. (in the line: ((ArrayPtr +i)+j)=rand()%55+1 )
Does anyone have an idea how to fix them? That's my code:
#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>
using namespace std;
const int AS = 6;
void FillingRandomly(int (*)[AS]);
void printing(int (*)[AS]);
int c;
int main()
{
int funny = 0;
int timpa = 0;
int counter = 0;
int Array[AS][AS];
srand(time(0));
FillingRandomly(Array);
cout << "The unsorted array is" << endl << endl;
printing(Array);
cout << "The sorted array is" << endl << endl;
printing(Array);
system("PAUSE");
return 0;
}
void FillingRandomly(int *ArrayPtr)
{
for(int i=0;i<AS;i++)
{
for (int j=0;j<AS;j++)
{
*(*(ArrayPtr +i)+j)=rand()%55+1;
}
}
}
void printing(int *Array)
{
for(int i=0;i<AS;i++)
{
for (int j=0;j<AS*AS;j++)
{
int counter = 0;
cout<<((Array[i] +j))<<setw(5);
if ((Array[i] +j)%AS == 0)
cout << endl << endl;
}
}
}
void forsorting(int *Brray, int funny)
{
int dice = 0;
int super = 0;
int space=0;
//Sorting Array[][] which is treated like Array[]
{
for (int pass = 0; pass < AS - 1; pass++) {
for (int k = 0; k < AS - 1; k++) {
int temp;
if(*(Brray+k)==*(Brray+k+1))
{
temp=*(Brray+k);
*(Brray+k)=*(Brray+k+1);
*(Brray+k+1)=temp;
}
}
}
}
}
By
*(*(ArrayPtr +i)+j)=rand()%55+1;
it seems you want
ArrayPtr[i][j] = (rand() % 55) + 1;
You can try something along the line of
int const offset = AS * i + j;
int const elem = (rand() % 55) + 1;
*(ArrayPtr + offset) = elem;
Your function signature is:
void FillingRandomly(int *ArrayPtr)
where you are telling to compiler that you are passing a simple pointer, but in the line:
*(*(ArrayPtr +i)+j)=rand()%55+1;
you are doing a double derreference, which is illegal and causing the compiler to complain
COMPLEMENT
I was seeing the comments in the other answer and, as what I need to write is bigger than the reserved commentary space, I decided to complement my own answer.
You defined Array as:
int Array[AS][AS];
Indeed, what you are doing is a promise to compiler that you will use Array as defined, but the compiler doesn't believe in you too much, so that any time you use Array the compiler will make sure that it is being used as declared.
The problem arises when you declare your FillingRandomly function. Here you are broking your promise and are trying to use Array by declaring a differente type. Note how you declare your function:
void FillingRandomly(int *ArrayPtr)
Due the fact that c++ supports function overloading, the compiler doesn't warn you until it initiate the linking phase, when it is unable to find a function whose signature is:
void FillingRandomly(int ArrayPtr[][AS])
note that both are different.
Once you are a beginner, the best way to keep your programs correctly is to keep your promise immutable. Bellow I show you a piece of your own code, correcting those issues for FillingRandomly function (you have to correct it for the others functions too):
const int AS = 6;
void FillingRandomly(int [][AS]); // Note that I've changed your prototype here
....
void FillingRandomly(int ArrayPtr[][AS]) // Keep your function signature the same as your prototype signature
{
for(int i=0;i<AS;i++)
{
for (int j=0;j<AS;j++)
{
ArrayPtr[i][j]=rand()%55+1; // Note how ArrayPtr is being used exactly as your promised early
}
}
}

C++: Segamentation fault when reading array in function passed as argument

I get a segmentation fault when reading the second element of h array inside the g function. Strangely, when debugging can I actually watch the array content. I think that besides this curious thing that shows that the data is there, I have done something wrong. Thanks in advance.
#include <iostream>
using namespace std;
void function(void function_passed(double* [], int), int n);
void g(double* [] ,int n_g);
int main()
{
function(g,5);
return 0;
}
void g(double* h[], int n_g)
{
for (int i = 0; i < n_g; i++)
cout << i << " "<< *h[i] << endl;
}
void function(void function_passed(double* [], int ), int n)
{
double * h = new double[n];
for (int i=0;i<n;i++)
h[i] = i + 10;
function_passed(&h,n);
delete[] h;
}
void func(void g(double* [],int n ), int n)
{
double * h = new double[n];
for (int i=0;i<n;i++)
h[i] = i;
g(&h,n);
delete[] h;
}
Operator precedence has bitten you. Inside g:
*h[i] is parsed as *(h[i]) but what you want is (*h)[i].
*h[i] is okay for the first iteration, but in the second one (and all subsequent) you're dereferencing an invalid pointer h+i.
On the second thought, you're actually invoking undefined behavior - pointer arithmetic is valid only between pointers that point to the same array.

dynamic allocation of int array in c++ and assigning value

I am new to c++ and i was trying to do folloing two things without help of std::vector( This was done earlier)
Define an array of integer and the size of array is not known to me.
Pass this array into another function and output all the values stored in array.
int _tmain()
{
int* a = NULL;
int n;
std::cin >> n;
a = new int[n];
for (int i=0; i<n; i++) {
a[i] = 0;
}
testFunction(a,n);
delete [] a;
a = NULL;
}
void testFunction( int x[], int n)
{
for(int i =0;i<n;++n)
{
std::cout<<x[i];
}
}
But i can see that its not allocating memory of 10 bytes and all the time a single memory is filled up with 0.
Can anyone please help me if i am lacking something ? Or is there any alternative way for this apart from vector.
Thanks in Advance
I modified with one thing as i realized that i put ++n instead of i
int _tmain()
{
int* a = NULL;
int n;
std::cin >> n;
a = new int[n];
for (int i=0; i<n; i++) {
a[i] = i;
}
testFunction(a,n);
delete [] a;
a = NULL;
}
void testFunction( int x[], int n)
{
for(int i =0;i<n;++i)
{
std::cout<<x[i];
}
}
I’m not sure I understand all yours problems but the typo
for(int i =0;i<n;++n) in testFunction led to a very long loop.
Write:
for(int i =0;i<n;++i)
this print yours n "0"