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?
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));
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);
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);
Related
I have read a lot about the subject and I am confused .
What used to work in a C file ,not working on a cpp file :
char *builtinFunctions[20];
Then I get error on the strcpy function here :
void Intepreter::setBuiltIns(char *builtins)
{
strcpy(builtinFunctions, builtins); // no matching function call to strcpy
}
I probably don't understand the basics here, but why in C++ this will not work ( do i need to use = instead ? )
strcpy(char *, const char*) = thats the structure
if I change the builtinFunctions from being a pointer it works.
EDIT:
The reason for being a const before this edit is that I read here :
Why is conversion from string constant to 'char*' valid in C but invalid in C++
that char *builtinFunctions[20]; will produce warning when :
builtinFunctions[0]="me";
and it did. I could fix it by removing the const .
This is an array of pointers to char.
char *builtinFunctions[20];
So you call
strcpy(builtinFunctions, builtins);
gets treated as strcpy(char **, char*), not as strcpy(char *dest, const char *src). So you get a mismatch for first parameter type.
EDIT:
So let's suppose builtinFunctions is "an array of words" you wish to populate, with void Intepreter::setBuiltIns(char *builtins) meant to do just that with it's first parameter being a pointer to a new incoming word. (And you're doing this in a C-style manner. Well, up to you.)
Some things to consider.
If you declare an array type arrName[N]; then the array's name
being used all alone without index is treated as a variable of type
type *arrName. If you type is initially char *, then
builtinFunctions by itself is of type char**. That's why your
strcpy fails, but strcpy(builtinFunctions[someIndex], builtins);
works.
Before invoking strcpy you should consider, if you have a
destination space allocated. builtinFunctions[someIndex] is of
type char *. Where does it point to? Is it a valid pointer to an
allocated space, or a gateway to hell of undefined behaviour strcpy will happily take you to?
#include<iostream.h>
#include<conio.h>
void print(char *str){
cout<<str;
}
int main(){
clrscr();
char str[]="abcdef";
print(&str);
getch();
return 0;
}
Error
1. Cannot convert 'char[7]' to 'char *'
2.Type mismatch in parameter 'str' in call to print(char *)
Since the parameter list of function print consists of a pointer, then passing &str in function call should be correct
Also if I remove the '&' the program runs fine (even though the print function requires a character reference).
Since the parameter list of function print consists of a pointer, then passing &str in function call should be correct
That's not exactly true: it is not sufficient to pass just any pointer, it needs to be of the correct type. &str is a pointer to an array, while you need a pointer to a single array element.
Since arrays in C++ "decay" to pointers when you pass them to functions, all you need to do is removing the ampersand:
print(str);
if I remove the & the program runs fine (even though the print function requires a character reference)
That's right! An array name (in this case, str) is implicitly converted to a pointer, which is equal to the pointer to array's initial element. In other words, it's the same as writing
print(&str[0]);
print(&str);
is wrong since the type of &str is char (*)[7] while the expected argument type is char*.
To illustrate the difference between the two types:
char str[]="abcdef";
char (*ptr1)[7] = &str; // Take the address of the array.
char* ptr2 = str; // Use the array variable. It decays to a pointer
*ptr2 = 'A'; // OK.
*ptr1 = 'A'; // Not OK.
(*ptr1)[0] = 'A'; // OK
Just pass str directly to print() like so:
// this already provides std::cout
#include<iostream.h>
// this is unnecessary
//#include<conio.h>
// you could do this to avoid std:: everywhere:
//using namespace std;
void print(char *str){
// don't forget the namespace:
std::cout<<str;
}
int main(){
clrscr();
char str [] = "abcdef";
// no "&", just pass str
print (str);
getch();
return 0;
}
Arrays in C/C++ are just pointers to the first element of the array.
http://www.cplusplus.com/forum/articles/9/
Function print is declared as having a parameter of type char *
void print(char *str){
cout<<str;
}
Within the main it is called with an argument of type char ( * )[7] because it is this type that expression &str has.
char str[]="abcdef";
//...
print(&str);
You should call the function simply like
print( str );
In this case the array is converted to pointer to its first character and expression str has type char *
When you have an array of type T as for example
T a[10];
then the array used in expressions is converted to pointer to its first element. For example this
T *ptr = a;
is a correct declaration.
On the other hand if you are using expression &a then the correct declaration for a pointer will look like
T ( *ptr )[10] = &a;
That is in this case ptr is a pointer to an object of type T[10]
And I advice to use a more modern compiler.:)
The code below is in my header file:
char mystrcat(char *s1, const char *s2) //strcat function
{
mystrcpy(s1 + mystrlen(s1), s2);
return s1;
}
The error is the return s1; "return value type does not match the function type."
I know that the strcat function appends a copy of the source string to the destination string, but how would I fix this error.
The other part of my code is in the main.cpp If you need more code just tell me.
Your signature should look like
char* mystrcat(char *s1, const char *s2)
// ^~~~~~ Note the pointer
You function should return char, but you are returning s1 that is char*
Make sure you understand problem that it's possible that there's no allocated space at the end of s1
1) ERROR: "return value type does not match the function type
CAUSE: s1 is char*; your signature returns "char"
SOLUTION: change your signature to char *
2) What is mystrcpy()? What errors might be lurking there?
3) More importantly, who allocates space for the "s1" you're returning?
4) What happens if the returned value is larger than the original value (a likely case)? Do you get a buffer overrun?
5) What happens if the original value is read-only (another likely case)?
Etc...
I have the below mentioned function in a large piece of code(in c++):
void startup(const char *& start,
const char *& stop);
After this function has been called I want to access the character values i.e string stored between 'start' and 'stop'.
The way I am trying to access the same is:
char *var=(c.start);
cout<<"\n Iterating over char pointer \n";
while(var<=(c.stop))
{
cout<<*var;
var++;
}
cout<<"\n";
However, while trying to access it this way I am getting the below mentioned error:
error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
Can someone be kind enough to rectify the error...and help me access the character values
All you need to do is replace the line:
char *var=(c.start);
with:
const char *var=(c.start);
Note that that const refers to the character pointed to by the pointer, not the pointer itself. So an expression like var++ is perfectly fine, since the pointer isn't const.
You may try this conversion in the assigning statement:
char *pc = (char *)(c.start);
I came across the statement:
outbal.write( (char*) &acc , sizeof( struct status ) );
outbal is an object of ofstream and status is a type.
Therefore:
struct status {
// code
};
status acc;
Talking about the second line I don't understand the first argument, which is (char*) &acc. What are we doing and how?
(char*)&acc if the address of the variable acc, cast to a char pointer so as to be compatible with ostream::write(). It is that variable that is being written, to the outbal stream, for a length of sizeof(struct status).
ostream::write takes a memory address and a length and will output that memory to the specified stream. In other words, you're simply outputting the entire memory contents of the acc variable.
Your code is similar to:
struct xyz {int a; float b; void *c};
ostream os("myfile.dat");
struct xyz abc; // 'struct' not technically needed in C++
os.write ( (char *)abc, sizeof (struct xyz));
// <<-memory addr->> <<-----size----->>
You are taking the address of acc and casting it to char*, which is what the ostream::write member function expects.
In short, you are writing the in-memory representation of the struct as-is to a stream.
(char*) &acc takes the address of the struct acc (i.e. a pointer to acc) and then casts it into a pointer to a char.
That's just taking the address of acc and casting it to a pointer to char.
Most likely that .write() method is meant to just blindly write a given amount of bytes out as-is. char is a convienent type to use for that, since (on most platforms) it is exactly one byte in size. So you pass it a pointer to the data you want written out, telling it, "Pretend this is a pointer to char".