0

I'm having problem using pointers and arrays in C, the task is simple i have a function that converts digits into a int array then send back a pointer to this array. At this point i need to assign this pointer into an array of pointers.

What i get is a bunch of number in sequence, so i suppose those are adresses of pointers, problem is i need to see the value stored in those arrays not the adresses...

Program compile without warnings or error.

//-----Function to convert single digits-----
int* convertToBits(int digitToConvert) 
{
    int binaryDigit[8];
    int k;
    for (int i = 7; i >= 0; i--) {
        k = digitToConvert >> i;

        if (k & 1) {
            binaryDigit[i] = 1;
            //printf("1");
        }
        else {
            binaryDigit[i] = 0;
            //printf("0");
        }
    }

    return binaryDigit;
}

//-----Function to store single digits-----
void setupDigit(int temperature) 
{
    int* displayDigit[4];
    if (temperature >= 0) 
    {
      for (int i = 0; i < 4; i++) {
        //displayDigit[i] = convertToBits(takeSingledDigit(temperature));
          int* pointTo = convertToBits(takeSingledDigit(temperature));
          displayDigit[i] = pointTo;
          for (int j = 0; j < 8; j++) {

              printf("%d", *(displayDigit[i]) + j);

          }
        printf("\n");
        temperature /= 10;
      }
    }
    else 
    {
        for (int i = 0; i < 3; i++) {
            //displayDigit[i] = convertToBits(takeSingledDigit(temperature));
            int* pointTo = convertToBits(takeSingledDigit(temperature));
            displayDigit[i] = pointTo;
            for (int j = 0; j < 8; j++) {

                printf("%d", *(displayDigit[i]) + j);

            }
            printf("\n");
            temperature /= 10;
        }
        displayDigit[3] = minusChar;
    }
  • 1
    What happens to `int binaryDigit[8];` when the function returns? (hint: poof!) You can't return a pointer to a locally declared array. On return the function stack is destroyed (released for reused). Your locally declared array lived in that function stack. The pointer you return -- now points to an invalid address -- poof! Either provide `binaryDigit` as a parameter to your function, or allocate with `mailloc` and return the pointer. – David C. Rankin Feb 16 '20 at 11:38
  • 1
    `return binaryDigit` gives undefined behavior (the address returned is not valid outside the function. There may be other issues, but that one really stands out. – John3136 Feb 16 '20 at 11:40
  • first: thanks!. what if i define the binarDigit as a global variable? – Simone Nardone Feb 16 '20 at 11:59
  • If you want to store the returned pointer in a container for ongoing usage, (an array of pointers, say), you should allocate the storage with malloc() and return that pointer. Don't go near a global unless there is no alternative. Document what your function does so that users understand that the storage allocated may need to be freed at some point. – Martin James Feb 16 '20 at 12:56
  • Oh.. and don't be tempted to use a static array either. Like with a global, you will likely end up in the brown stuff. If you malloc the space, your function will work and be recursion/thread safe. Functions that work correctly, no matter where/how they are called from in user space, leave a nice, warm fuzzy feeling:) The only 'better' way is to pass in the array as an argument, resulting in a reentrant function that will work correctly even if called by an interrupt handler:) – Martin James Feb 16 '20 at 13:12

0 Answers0