red
red

Reputation: 3

function with pointer return giving error

I'm trying to run a function that returns a pointer, but when I try to store the returned value into my main loop it's giving me an error.

I'm trying to use a pointer to pointer since the return value of the function is a pointer, or is that wrong?

#include <stdio.h>
#include <wiringPi.h>


int **ptr;
int **ptr1;

int* readfunc (int storage[33]){
     
        storage[0] = digitalRead(D0)+48;
        storage[1] = digitalRead(D1)+48;
        storage[2] = digitalRead(D2)+48;
        storage[3] = digitalRead(D3)+48;
        storage[4] = digitalRead(D4)+48;
        storage[5] = digitalRead(D5)+48;
        storage[6] = digitalRead(D6)+48;
        storage[7] = digitalRead(D7)+48;
        
 return storage;
}

int* readfunc2 (int storage2[33]){

        storage2[0] = digitalRead(D0)+48;
        storage2[1] = digitalRead(D1)+48;
        storage2[2] = digitalRead(D2)+48;
        storage2[3] = digitalRead(D3)+48;
        storage2[4] = digitalRead(D4)+48;
        storage2[5] = digitalRead(D5)+48;
        storage2[6] = digitalRead(D6)+48;
        storage2[7] = digitalRead(D7)+48;
   
 return storage2;
}

int main(){

  ptr = &readfunc2;
  ptr1 = &readfunc;

  return 0;
}

The error its giving me is

 assignment to ‘int **’ from incompatible pointer type ‘int * (*)(int *)’ [-Wincompatible-pointer-types]
   44 |   ptr = &readfunc2;


warning: assignment to ‘int **’ from incompatible pointer type ‘int * (*)(int *)’ [-Wincompatible-pointer-types]
   45 |   ptr1 = &readfunc;

Upvotes: 0

Views: 42

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117258

Your declarations of ptr and ptr1 are wrong and you also need to call the functions, not take their address like you now do.

int main() {
    int* ptr;  // the functions return int*, not int**
    int* ptr1;
    
    int storage[33]; // the functions need a parameter

    ptr = readfunc2(storage); // call the functions
    ptr1 = readfunc(storage);
}

Upvotes: 2

Related Questions