Question:
Error in C/C++ programming ,please! I need help.?
_LiveGreen_
2012-01-12 05:06:49 UTC
error here! its says that arguement of type "char(*)[5] is incompatible with parameter of type "char * " \\

error C2664: 'display' : cannot convert parameter 1 from 'char [5][50]' to 'char *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

please help me understand and fix the code thanks!

#include
#include
#include
#include

using namespace std;
void start();
//void inputstr(char *str);
void display(char *result);
char str[5][50];

void main()
{ system("cls");
start();
getch();
}

void start()
{
int i=0;


for(i=0;i<5;i++)
{ printf("Enter string %d:",i+1);
gets(str[i]);
}
display(str); \\<--- i have error here! its says that arguement of type "char(*)[5] is incompatible with parameter of type "char * " \\

}

void display(char *result)
{ int i=0;

for(i=0;i<5;i++)
{
printf(" %s",result[i]);
}
getch();


}
Three answers:
L. Scott
2012-01-12 05:45:28 UTC
You're passing an array of five 50-byte buffers to display.



The actual size of the array (five) doesn't matter to display's call list, since it's really just a pointer to the first element, and that pointer will look the same no matter how many elements there are.



But the compiler does need to know the second part (the 50 part), so that it can know how many bytes each element of that array are (and thus how many to skip in order to move to the next element).



So buffer should be declared like



void display(char *result[50])



or to be a little clearer to the reader



void display (char result[][50])



I wouldn't recommend putting the '5' in there:



void display (char result[5][50])



since the compiler won't enforce that (see the bit about it being a pointer above), and that may lead to errors in the future, when the program gets bigger and other calls to display are made. (This leads to the idea that you need some sort of way to specify the 5 to display -- either by passing in a length parameter as well, or by using some sort of marker in the array itself to signify the end. But that's another topic).





BTW, gets() is inherently insecure, since it offers no way to limit the number of characters read. So if the user types in more than 50 characters, gets() will overwrite some of your other memory and possibly crash your program or worse.



You should use fgets instead (ans specify stdin as your FILE *).
llaffer
2012-01-12 05:13:33 UTC
Your argument is a char pointer, but you're trying to pass an array pointer, which doesn't line up properly to the compiler, even though the memory address would be the same.



You'll want to change it to pass the array of character pointers, so you'll want to change your prototype and your function's argument to:



void display(char *result[])



Then I think it will work the way that you want it to.
Abhi
2012-01-12 05:18:30 UTC
void display(char result[5][50])


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...