2

For this assignment, my professor gave us the following function header:

void thisFunc(Node** root, const int * elements[], const int count)

Presumably, this is correct and I cannot change this.

Const int *elements is an array of int values taken with scanf; I declared mine with

int entries[atoi(argv[1])-1];

and successfully populated it with

   for(int a=0; a < atoi(argv[1]); a++) {
      scanf("%i", &entries[a]);
   }

but I'm struggling with calling thisFunc.

   thisFunc(&bst, entries, atoi(argv[1]));

which throws the obvious error:

note: expected 'const int **' but argument is of type 'int *'

It's expecting a constant array of pointers to pointers of ints, if I'm right? What should I be doing with my entries array that will make it a valid parameter?

I've tried passing entries in by reference (&entries), but I'm a little lost.

HB-
  • 635
  • 2
  • 8
  • 21

3 Answers3

8

The signature implies that you are going to pass a pointer to a pointer, which in turn suggests dynamic allocation (rather than a variable-length array):

// Read the length once, and store it for future reference.
// Calling atoi on the same string multiple times is inefficient.
int len = atoi(argv[1]);
int *entries = malloc(sizeof(int) * len);
... // Populate the data in the same way, but use len instead of atoi(argv[1])
...
// Now you can take address of entries
thisFunc(&bst, &entries, len);
...
// Do not forget to free memory allocated with malloc once you are done using it
free(entries);

Note: with this said, I am nearly certain that your professor has made a small mistake in declaring thisFunc, and that it should have been declared like this:

void thisFunc(Node** root, const int elements[], const int count)

The reason I think that this should be the correct signature is that there needs to be an intent behind making a variable a pointer, and such intent is clearly missing from making elements a const pointer-to-pointer. Since elements is const, the signature tells me that thisFunc is not going to modify the data behind elements. At the same time, by using a pointer the signature tells me that it is going to modify elements itself, which does not look like what the function is going to do, because elements are read elsewhere.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Of course! I should've realized. I'm new to C and used to Java, so I'm still getting used to the options that come with being able to manage memory. Thank you very much. :) – HB- Oct 21 '14 at 17:52
  • Right now I'm getting the error "expected 'const int **' but argument is of type 'int **'". Do you have any suggestions? – HB- Oct 21 '14 at 17:57
  • This works if the function needs a pointer to a pointer to an array of `int`... but not if it needs an array of pointers to `int`. The function header could mean either one, but to me the array brackets suggest the latter (for the former I'd expect `const int **elements` rather than `const int *elements[]`). – Dmitri Oct 21 '14 at 17:58
  • 2
    @Rohawk I am almost certain that your professor has made a mistake: he put an extra asterisk in front of `elements`. You can make it run with `const` double-pointer using a cast ([demo](http://ideone.com/bznvPN)) but it is useless: you are not supposed to modify entries anyway, and you need an extra dereference inside `myFunction`. Without an extra asterisk it's a lot more natural, and there is no need to cast ([another demo](http://ideone.com/vZNPGF)). – Sergey Kalinichenko Oct 21 '14 at 18:17
  • It seems to work if inside main I create "const int * nums = entries;", call the function with &nums as the second param, then in thisFunc create "const int * test = *elements;" and refer to values as "test[#]". I have absolutely no doubt that there's a better way to handle this issue, so I'm still extremely open to suggestions. I'll try the one you just posted, thanks again. – HB- Oct 21 '14 at 18:17
  • @dasblinkenlight: Indeed, the function has a prototype like this: `void thisFunc(Node** root, const int elements[], const int count)`.Someone did asked a [similiar question](http://stackoverflow.com/q/26395841/1057230) regarding the same set up. Since, I didn't knew, that it is a part of an assignment, I tried to answer that, to explain, that in a best way. But looking at this question again, it seems like, it is actually some college/university assignment – nIcE cOw Oct 22 '14 at 04:33
1

If you want to modify the values in array entries[] using your mentioned function then change your call to: thisfunc(&bst, &entries, atoi(argv[1]));

The problem is you are passing "entries" which is an array but your function is expecting a pointer to int array[]. So pass the adrress of entries which is "&entries".

rahul tyagi
  • 643
  • 1
  • 7
  • 20
-4

Have you tried to convert it implicitly?

(const int**)variable;

done ;)

MRXI
  • 109
  • 1
  • 3
  • 11