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.

6
  • 6
    const int * elements[] is not an array of integers. Its an array of pointers to integer.. Commented Oct 21, 2014 at 17:37
  • 4
    @haris Actually, it is not an array at all. It is adjusted to pointer to pointer to int. Commented Oct 21, 2014 at 17:38
  • 2
    @juanchopalnza only when used as a function parameter. But that's really arguing symantix. Commented Oct 21, 2014 at 17:40
  • 2
    @aruisdante It is a function parameter. And it is an important distinction. Commented Oct 21, 2014 at 17:42
  • 1
    @aruisdante Not trying to be rude, but I think you meant "semantics", unless there's an SO inside joke I'm not in on. Commented Oct 21, 2014 at 17:53

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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. :)
Right now I'm getting the error "expected 'const int **' but argument is of type 'int **'". Do you have any suggestions?
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[]).
@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) 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).
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.
|
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".

Comments

-4

Have you tried to convert it implicitly?

(const int**)variable;

done ;)

2 Comments

i know you cannot comment.. but think and ponder over your answer before posting..
The mistake? Just try out what happens when you do return **(const int**)entries;...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.