Thread: strcpy() function

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    strcpy() function

    strcpy() function is not working in MSVC++ 6

    Heres what I have

    #include <stdio.h>
    main()
    {
    char varname[10];
    strcpy(varname, string);
    }

    this gives me invalid identifier or something to that effect

    do you have to do anything special with #include for MSVC++ 6 to recognize strcpy()

    BTW I got this code from a book that I am learning from, however, it doesnt include visual c++ compiler stuff

    any help appreciated

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    5
    you might need #include <string.h>

    also you need to declare the variable 'string' if you haven't already, char sintrg[ 10 ] = "thestring";

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    9
    you have to give the array a value? I mean you cant simply define it first?

    also i forgot to mention, im getting "strcpy invalid identifier"

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi
    As JRoosh says you will have to implement the library file for strings- which is <string.h>- this library contains various functions which can manipulate char strings- because you have omitted this your compiler recognises strcpy as just another variable. What you are trying to acheive is to copy the second argument in the function to the first argument- You have not declared the second variable string-
    In your example code above you neither define the string or declare it as a variable. To copy the second string to the first you must give it a value as described below.
    You will have to declare variable string and then either intialize it with whatever or have user input a string..as follows

    Code:
    /*FIRST ROUTINE*/
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
      char varname[10], string[]="Hello You";
    
      strcpy(varname, string);
      printf("%s", varname);
      /*this prints Hello You to display*/
    }
    
    /*2ND ROUTINE*/
    #include <stdio.h>
    #include <string.h>
    
    int main (void)
    {
      char varname[10], string[10];
    
      printf("\nEnter a string up to nine characters");
      gets(string);
      strcpy(varname, string);
      printf("\n %s", varname);
      /*this prints whatever user enters to the display*/
    }
    Last edited by bigtamscot; 09-21-2001 at 12:38 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    9
    Cool, that pretty much clears it up thanks =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM