-2

So this is what my function declaration looks like:

void mdump(char* startAdrs, char* endAdrs);

In my code, here's how I'm calling it:

char f = 'f';
char d[200*sizeof(int)];
mdump(&d, &f);

The error I'm getting is:

invalid arguments 'Candidates are: void mdump(char*, char*)'

Why am I not able to call the mdump function properly?

  • You might want `mdump(d, &f);`. – songyuanyao Apr 03 '18 at 03:53
  • @songyuanyao Why? I need to pass memory addresses for both the character f and character array d. So why can't i do it the way I have done it? – T0ny50pr4n0 Apr 03 '18 at 03:54
  • ..why the downvote what makes this an invalid question – T0ny50pr4n0 Apr 03 '18 at 03:55
  • 1
    `&d` will return the pointer to array, which is different with pointer to `char`. – songyuanyao Apr 03 '18 at 03:55
  • @songyuanyao How can I make it so that I can pass memory addresses for both into the function? – T0ny50pr4n0 Apr 03 '18 at 03:56
  • Downvotes likely caused by lack of research. – user4581301 Apr 03 '18 at 05:28
  • @user4581301 Downvotes likely from old people assuming anyone who dare use C++ must read entire books and work through all the exercises...vs. seeing it as "just another language out there", trying something, and if it doesn't work thinking it's okay to ask. I'd hate to be held to that standard if I were trying to solve a problem with some random just-written-yesterday JavaScript thing. I understand C++ has an "older" aspect some think *more deserving respect*, but the downvotes are rude, and not going to grow the culture. Countervoted. – HostileFork says dont trust SE Apr 03 '18 at 11:53
  • What is it that you are trying to do? If those are really "start" and "end" pointers and you're trying to "explore" individual local scope variables in memory then you're going into wild UB territory – Lightness Races in Orbit Apr 03 '18 at 15:19
  • 1
    @HostileFork: Your name is apt today. Downvote reasons are clearly enumerated in the tooltip text, and have nothing to do with "old people", "daring to use C++", holding C++ to a higher standard than other languages, being "rude" or "not growing the culture". Also it is not your job to "undo" people's right to vote. Thanks and have a nice day. – Lightness Races in Orbit Apr 03 '18 at 15:20
  • @LightnessRacesinOrbit As per usual: I'm not actually talking to you...I'm actually talking to the OP...so they realize *not everyone here is like you*. Weren't you supposed to be out buying `goreadabook.com`, where every answer to a question that can be found in a book...or somewhere on the web...is downvoted and answered with "go read a book"? I think a site whose policy only allowed really original questions that interested you would be great, except for the part where you're allowed to comment on it. – HostileFork says dont trust SE Apr 03 '18 at 23:36
  • @user4581301 are you kidding me? then what is the point of having this site if those new to programming can't get help when stuck? I made a real effort and hit a roadblock. If my question bores you, then move along instead of trying to hinder my learning and progress. – T0ny50pr4n0 Apr 04 '18 at 08:57
  • Not kidding you. You've nailed the point of stack overflow, but your question is a variant of *How do I pass an array to a function?* This question has been asked and answered thousands of times and dooms the question to a bit of a pounding. But how do you get from "invalid arguments 'Candidates are: void mdump(char*, char*)'" to "How do I pass an array to a function?" That's the challenge here. The error message is from Eclipse's CODAN tool. It's pretty terse. It's good at letting you know you probably have an error without compiling, but in this case you really wanted the compiler. – user4581301 Apr 04 '18 at 15:17
  • The compiler would have spat out something along the lines of `cannot convert ‘char(8) [800]’ to ‘char*’ for argument ‘1’ to ‘void mdump(char*, char*)’` which tells you which argument is wrong, 1, and why, it's not a pointer to `char`. When you don't understand an error message, try to get another one. Anyway, once you know the first argument is wrong, that leads to a websearch for "How do I pass an array to a function?" and a couple billion hits on Google. The useful ones will mention stuff like "Arrays Decay to Pointers". – user4581301 Apr 04 '18 at 15:18

3 Answers3

3

Use of

mdump(&d, &f);

is a compiler error since &d evaluates to a char (*)[200*sizeof(int)]. That cannot be converted to a char*.

You may use

mdump(d, &f);

to get past the compiler error. However, judging by the names of the arguments of mdump, the second argument needs to be d+sizeof(d).

mdump(d, d+sizeof(d));
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You should not pass the address of d because d already acts as a pointer pointing to the first element.

so you should do this:

mdump(d, &f);
Lorence Hernandez
  • 1,189
  • 12
  • 23
-1

d is an array. so just writing d itself indicates the address of the first element in the array. By using &d, you are sending pointer to the pointer to a char. But you need to send pointer to a char. So the write way to call mydump is:

mdump(d, &f);
Tuhin Paul
  • 558
  • 4
  • 11
  • _"you are sending pointer to the pointer to a char"_ Not true. – Lightness Races in Orbit Apr 03 '18 at 15:21
  • hmm, i used layman's language probably. how if you replace 'pointer' with 'reference'? – Tuhin Paul Apr 03 '18 at 15:49
  • If you see a problem, state the instead of doing a drama. This is the error if &d is used: [[ error: cannot convert ‘char (*)[800]’ to ‘char*’ for argument ‘1’ to ‘void mdump(char*, char*)’ ]] In the erroneous code, &d is the address of d, which is the address of the array or the first element in the array. And as the above error says, it's failing to convert a pointer to a char array to a char-pointer. – Tuhin Paul Apr 04 '18 at 06:29