0

My question relates to a test I'm developing (I'm an undergraduate student grader) for an assignment in a data structures class. I have to post a minified version of what I'm asking since I don't want to post the test on the internet since it's for an active class in an active university right now. But, to give context, the assignment requires that students set and unset individual bits in an array of integers. Integer 1 is positions 0 - 31, integer 2 is positions 32 - 63 etc. etc. etc. and they write functions to set and unset bits at specific bit positions. Part of my test involves a function like this:

char** convert_bits_to_string(unsigned* flag_holder, unsigned flag_holder_size, char string_flag_holder[5][33]);

int main(int argc, char** argv) {

     // holds the bit string version of flag_holder generated by the students code
     char student_string_flag_holder[5][33];;

     // holds the expected bit string created after round 1 of testing
     char expected_string_flag_holder1[FLAG_HOLDER_SIZE][BIT_STRING_SIZE] = { 
     "11000000000000001000000000000001", "10000000100000000000000000000001", "10000000000000100000000000000000",
        "00000000000000000000000000000000", "00000000000000000000000000000001" };

     // integer array where students will set and unset bits
     unsigned flag_holder[5] = { 0 };
     
     // ROUND 1 of testing
     //...do some set and unsetting of bits

     // convert their flag_holder array to an array with string representations of the bits
     convert_bits_to_string(flag_holder, 5, student_string_flag_holder);

     // now, a string representation of flag_holder is in "student_string_flag_holder" and it can
     // compared to "expected_string_flag_holder" to see if it's correct
     return 0;
}

char** convert_bits_to_string(unsigned* flag_holder, unsigned flag_holder_size, char string_flag_holder[5][33]) {
    // take the bits in flag_holder and store the string representation in string_flag_holder
    // ...
    // ...
    return string_flag_holder;
}

My program works completely fine, it's just that in Linux, I get this warning

main.c:111:9: warning: return from incompatible pointer type [-Wincompatible-pointer-types]
  return string_flag_holder;

I know the warning is because the function returns a char** but the formal parameter of the 2D array it's returning is char [ ][ ]. When I changed the formal parameter to char** like this:

char** convert_bits_to_string(unsigned* flag_holder, unsigned flag_holder_size, char** string_flag_holder)

the program would crash, and obviously changing the return type of the function to char[ ][ ] like this:

char[ ][ ] convert_bits_to_string(unsigned* flag_holder, unsigned flag_holder_size, char string_flag_holder[5][33])

isn't valid.

So, my question just out of curiosity is, is there any way to get rid of this warning ASIDE from just making the function return void OR first changing the formal parameters of the function to char** and secondly using dynamic memory management in main to make my arrays instead of using char[ ][ ]

Again, this question is just out of curiosity coming from someone with some experience in C and just wanting to learn a bit more. I will most likely just make it return void since I don't actually utilize the return value of the function.

Ben
  • 195
  • 7
  • 1
    You shouldn't return a 2D array in C. You should return a pointer to one. It's also a lot more convenient to use a 1D array and emulate the 2D part. – tadman Feb 09 '21 at 04:41
  • 1
    A 2d array is not the same thing as a ptr-to-ptr. [Why can't we use double pointer to represent two dimensional arrays?](https://stackoverflow.com/q/4470950) – 001 Feb 09 '21 at 04:41
  • Okay, thank you tadman and Johnny. – Ben Feb 09 '21 at 04:56

0 Answers0