-
strcpy error with arrays
I am currently using the Visual C++ compiler on a WIN 2000 system. I am trying to scan in user input and then assigning what was entered into one of the dimensioanl arrays.
In the Visual C++ compiler, it gave me this error:
error C2664: 'strcpy' : cannot convert parameter 2 from 'char [3][20]' to 'const char *'
So, I tried to copy and paste the same exact code into Miracle C (another C compiler) and it worked.
Is this a Visual Studio bug? How can I work around it? HELP PLEASE!!!
Code:
#include <stdio.h>
#include <string.h>
char charname[3][20];
int i;
int main()
{
for (i=0; i<3; i++)
{
printf("Enter in something: ");
scanf("%s", charname);
strcpy(charname[i],charname);
printf("%s\n",charname[i]);
}
return(0);
}
-
Look where you are storing the user input. It's the 2 dimensional array. Just make a one dimensional temporary array and then copy that in to your two dimensional one as indexed by [i]. That should be fine.
-
Well, I'm trying to accept 3 different string values and then have them displayed on the screen.
That's why they are in 2 dimensional arrays.
Can you show me an example of what you are asking me to do?
Edit to add:
I figured it out!!!!! Thank you so much!!!!!!!!
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char charname[3][20];
char charname2[20];
int i, x;
int main()
{
for (i=0; i<3; i++)
{
printf("Enter in something: ");
scanf("%s", charname2);
strcpy(charname[i],charname2);
printf("%s\n",charname[i]);
}
return(0);
}
-
Something like this..
Code:
#include <stdio.h>
#include <string.h>
char charname[3][20];
int i;
int main()
{
char buffer[20];
for (i=0; i<3; i++)
{
printf("Enter in something: ");
scanf("%s", buffer);
strcpy(charname[i],buffer);
printf("%s\n",charname[i]);
}
return(0);
}
-
THANK YOU SO MUCH!!!! IT WORKED!!