Thread: strcpy error with arrays

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    12

    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); 
    }
    Regards,
    Trang

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    12
    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); 
    }
    Last edited by trang; 01-10-2004 at 10:12 PM.
    Regards,
    Trang

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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); 
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    12
    THANK YOU SO MUCH!!!! IT WORKED!!
    Regards,
    Trang

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D arrays, strcpy and strcmp Problems
    By Windu102 in forum C Programming
    Replies: 3
    Last Post: 08-23-2008, 01:00 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Using strcpy() and arrays of structs
    By vital101 in forum C Programming
    Replies: 3
    Last Post: 04-26-2007, 09:04 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. help with strcpy and arrays with header file
    By Agnesa in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2002, 05:06 PM