0

I'm trying to store 4 arrays in an array, and for some reason, the last value keeps overriding the previous three values. For example, if 123; 456; 789; 987 were inputted into the code below, ipadr[0] - ipadr[4] would all only store 123. I've tested to make sure that numConvert() is working, and within numConvert, 4 different arrays of ints are returned, but only numConvert(d) is the ipadr array (stored 4 times).

Also, is my syntax / code correct in order for this function to return ipadr as an array of arrays (int**)? Why do you need to make it static int* when initializing the array?

I'm new to C and this has been really frustrating me. Any help would be incredibly appreciated. Thank you in advance!!

   int** ipConvert(int a, int b, int c, int d){
        static int* ipadr[4];
        ipadr[0]=numConvert(a);
        ipadr[1]=numConvert(b);
        ipadr[2]=numConvert(c);
        ipadr[3]=numConvert(d);
        return ipadr;
    }

numConvert code:

     int* numConvert(int dec) {
        static int hold[8];
        ...
        return hold;
    }

3 Answers3

2

The hold array is indeed overwritten after each call to numConvert, because it is a static area of 8 int.

Better use dynamic allocation with malloc, and have that function allocate 8 ints.

int* numConvert(int dec) {
    int *hold = (int *)malloc(sizeof(int) * 8); // C++ need cast!
    int x = dec;
    for(int y=7;y>=0;y--){
        if(x%2==0 || x==0){
            hold[y]=0;
        }
        else{
            hold[y]=1;
        }
        x=x/2;
    }
    printf("\ntest loop: ");
    for(int i=0;i<8;i++){
      printf("%i", hold[i]);
    }
    printf("\n");

    return hold;
}

then the returned values will have to be freed, after you don't need them anymore.

As for static, this answer explains it in details.

Déjà vu
  • 28,223
  • 6
  • 72
  • 100
1

Every time you write something in hold then return it, you basically overwrite the previous value. There is single instance of hold not multiple as you may be thinking.

static keyword you have used without knowing the full functionality. It is not the same as automatic storage duration.

int* numConvert(int dec) {
    int *hold = malloc(sizeof *hold *8);
    if( hold == NULL){
       fprintf(stderr,"Error in malloc");
       exit(1);
    }
    int x = dec;
    ....
    ....
    return hold;
}

Free the dynamically allocated memory when you are done working with it.

What have we done here?

Dynamically allocated memory has lifetime beyond the scope of the function. So just like the static one it stays alive. But with contrary to the static variable each time a new chunk of memory is allocated. And for that we are not overwriting anything, which was the primary problem in your case.

One thing more..

hold is of automatic storage duration meaning every time we exit the function the hold is deallocated and then again when we call it we get to have a local variable named hold. So in each we get different instance. And that's why we return the address contained in hold. This memory whose address hold contains has lifetime beyond the scope of the function. So it stays unlike the local variable hold and we store this address in other function.

Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
0

Declare your hold variable as instance variable rather then static variable like

 int* numConvert(int dec) {
    int hold[8];