3

I am new to C and C++. I have defined a static function which has a pointer 'ptr'. How can I access the pointer outside the function?

#include <iostream>

using namespace std;

static void accessArr(uint8_t arr[]);

int main()
{
    uint8_t arr[] = {1,2,3,4,5};

    accessArr(arr);
    
    cout << *ptr <<endl;
    return 0;
}

void accessArr(uint8_t arr[])
{
    uint8_t *ptr = arr;
}

I am getting the below error for the above code. Please help in solving the error.

main.cpp:12:14: error: ‘ptr’ was not declared in this scope

     cout << *ptr <<endl;
              ^~~
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Beginner
  • 73
  • 3
  • `cout << arr[0] < – Lukas-T Mar 23 '21 at 19:28
  • Local variables inside functions are local variables no matter what. Their scope and life-time follows the same rules both for statis and non-staticfunctions. – Some programmer dude Mar 23 '21 at 19:29
  • @churill No, I want to print the first element of 'arr' array using pointer 'ptr' – Beginner Mar 23 '21 at 19:29
  • You cannot do that because `ptr` is a local variable of `accessArr`. Workaround is calling some function from `accessArr`, passing a pointer or reference to `ptr`. – MikeCAT Mar 23 '21 at 19:29
  • 1
    That's not how C++ works. Function variables are always only accessible from inside the function. – NathanOliver Mar 23 '21 at 19:30
  • is it not possible as the pointer is assigned in the static function? – Beginner Mar 23 '21 at 19:31
  • @Beginner And _why_ do you want to do this? Just use `arr[0]`. You can't access local variable from other functions. Maybe we can find a solution if you tell us what exactly you want to achieve and why. I think you maybe are trying to solve the wrong problem. – Lukas-T Mar 23 '21 at 19:32
  • @Beginner This is a very well asked question for a beginner. Just so you know, even if the function is static, extern or inline it doesn't change the problem you had. The solution you accepted will solve it no matter the linkage of the function. – Guillaume Racicot Mar 23 '21 at 20:15
  • @GuillaumeRacicot getting your point, thanks for the explanation :) – Beginner Mar 23 '21 at 20:43

2 Answers2

3

The name ptr is not declared and visible in main. Just declare the function as returning a pointer like for example

static uint8_t * accessArr(uint8_t arr[])
{
    return arr;
}

And in main you can write

cout << *accessArr( arr ) <<endl;

Or

uint8_t *ptr = accessArr( arr );

cout << *ptr <<endl;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I think that's the most useless function I've ever seen, but it correctly solves the problem. +1 – Lukas-T Mar 23 '21 at 19:31
  • Even if it looks trivial, I think such answers are really needed for beginners, as many will fail to understand where to begin. You could even add a section about out parameters or globals, even if not recommended so that the beginners knows what's possible and what to avoid. – Guillaume Racicot Mar 23 '21 at 20:12
0

arr refers to the adress of firts element? If you need the arr address just write &arr

Sebastian
  • 27
  • 11
  • I think I know what you're tying to get at, but `arr` refers to the whole array, not just the first element. `arr` will [decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) to a pointer to the first element, but `arr` will be and remain the array. – user4581301 Mar 23 '21 at 19:36