0

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;
2
  • 2
    You really need to go back to the fundamentals before you start writing code. This snippet is wrong from so many aspects. Commented Oct 21, 2022 at 6:34
  • 1
    ptr = &readfunc2; This is not how you call functions. You are taking the address of a function instead of calling it. If you want to call it, try readfunc2(arg); but you need to provide the argument declared in parameter list and the variable where you want to store the pointer, has wrong type. Commented Oct 21, 2022 at 6:36

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.