FMUSER Wirless Transmit Video And Audio More Easier !

[email protected] WhatsApp +8618078869184
Language

    How to use Strcpy in the C language

     

    "The string class of the standard library provides three member functions to get a character array of type C from a string: C_ str()、data()、copy(p,n)。 C language strcpy() usage This is a library function for copying strings in C language. The function declaration is included in the header file "string. H" specially dealing with strings: char * strcpy( char * dst, const char * src ); This function copies the string SRC into an allocated string space DST, including the null character marking the end of the string. DST is returned if the operation is successful, otherwise null is returned You should note that the length of your second string SRC + 1 (+ 1 is because the string ends with a null symbol) cannot exceed the size of the space you have opened up for DST, otherwise the null characters in SRC cannot be copied into DST, and your DST becomes a character array, not a string. In that case, if you have a printf or puts statement or something behind you, you should print out the DST, It will cross the line because there is no empty character to mark the end of the string. For example: Define a string char a [20], and a string c [] = "I am a teacher!"; Copy C to a and use it like this: strcpy (a, c); This function is contained in the header file "string. H". Program code: #include《string.h》 #include《iostream.h》 void main() {char a[20],c[]=“i am teacher!”; strcpy(a,c); cout《《a《《endl; } You see: char a[20] = “abcde” Strcpy (& A [1], a) function is copied character by character, First copy the first character, a [0] = = 'a'; a[1] = a[0]; That is, a [1] = 'a'; Then copy the second character, a [1] = = 'a'; a[2] = a[1]; That is, a [2] = 'a'; And so on!!! A [0] = 'a' is useless. In fact, the initial a [0] is equal to 'a'! copy(p,n,size_ type _ Off = 0): copy up to n characters from a string type object into the space pointed to by the character pointer P. By default, it starts from the first character, but you can also specify the starting position (remember to start from 0). Returns the character that was actually copied from the object------ The user should ensure that the space pointed to by P is enough to store n characters. // basic_ string_ copy.cpp // compile with: /EHsc /W3 #include 《string》 #include 《iostream》 int main( ) { using namespace std; string str1 ( “1234567890” ); basic_ string 《char》::iterator str_ Iter; char array1 [ 20 ] = { 0 }; char array2 [ 10 ] = { 0 }; basic_ string 《char》:: pointer array1Ptr = array1; basic_ string 《char》:: value_ type *array2Ptr = array2; cout 《《 “The original string str1 is: ”; for ( str_ Iter = str1.begin( ); str_ Iter != str1.end( ); str_ Iter++ ) cout 《《 *str_ Iter; cout 《《 endl; basic_ string 《char》:: size_ type nArray1; // Note: string::copy is potenTIally unsafe, consider // using string::_ Copy_ s instead. nArray1 = str1.copy ( array1Ptr , 12 ); // C4996 cout 《《 “The number of copied characters in array1 is: ” 《《 nArray1 《《 endl; cout 《《 “The copied characters array1 is: ” 《《 array1Ptr 《《 endl; basic_ string 《char》:: size_ type nArray2; // Note: string::copy is potenTIally unsafe, consider // using string::_ Copy_ s instead. nArray2 = str1.copy ( array2Ptr , 5 , 6 ); // C4996 cout 《《 “The number of copied characters in array2 is: ” 《《 nArray2 《《 endl; cout 《《 “The copied characters array2 is: ” 《《 array2Ptr 《《 endl; ////Be sure to make array3 have enough space //char array3[5]={0}; //basic_ string《char》::pointer array3Ptr=array3; //basic_ string《char》::size_ type nArray3; //nArray3 = str1.copy(array3,9); // Error!!!! //cout《《“The number of copied characters in array3 is: ” // 《《nArray3《《endl; //cout《《“The copied characters array3 is: ”《《array3Ptr《《endl; } For the last commented part above, although there are no errors in compilation, errors will be generated during runtime: stack around the variable 'array3' was corrupted Detailed explanation of mybatis dynamic SQL Design of HDB3 encoder in data transmission system with VHDL language How do bare metal programs drive hardware? Look what the elder said Typical examples of applying object-oriented programming SOC principles Introduction to common java development tools for embedded development“

     

     

     

     

    List all Question

    Nickname

    Email

    Questions

    Our other product:

    Professional FM Radio Station Equipment Package

     



     

    Hotel IPTV Solution

     


      Enter email  to get a surprise

      fmuser.org

      es.fmuser.org
      it.fmuser.org
      fr.fmuser.org
      de.fmuser.org
      af.fmuser.org ->Afrikaans
      sq.fmuser.org ->Albanian
      ar.fmuser.org ->Arabic
      hy.fmuser.org ->Armenian
      az.fmuser.org ->Azerbaijani
      eu.fmuser.org ->Basque
      be.fmuser.org ->Belarusian
      bg.fmuser.org ->Bulgarian
      ca.fmuser.org ->Catalan
      zh-CN.fmuser.org ->Chinese (Simplified)
      zh-TW.fmuser.org ->Chinese (Traditional)
      hr.fmuser.org ->Croatian
      cs.fmuser.org ->Czech
      da.fmuser.org ->Danish
      nl.fmuser.org ->Dutch
      et.fmuser.org ->Estonian
      tl.fmuser.org ->Filipino
      fi.fmuser.org ->Finnish
      fr.fmuser.org ->French
      gl.fmuser.org ->Galician
      ka.fmuser.org ->Georgian
      de.fmuser.org ->German
      el.fmuser.org ->Greek
      ht.fmuser.org ->Haitian Creole
      iw.fmuser.org ->Hebrew
      hi.fmuser.org ->Hindi
      hu.fmuser.org ->Hungarian
      is.fmuser.org ->Icelandic
      id.fmuser.org ->Indonesian
      ga.fmuser.org ->Irish
      it.fmuser.org ->Italian
      ja.fmuser.org ->Japanese
      ko.fmuser.org ->Korean
      lv.fmuser.org ->Latvian
      lt.fmuser.org ->Lithuanian
      mk.fmuser.org ->Macedonian
      ms.fmuser.org ->Malay
      mt.fmuser.org ->Maltese
      no.fmuser.org ->Norwegian
      fa.fmuser.org ->Persian
      pl.fmuser.org ->Polish
      pt.fmuser.org ->Portuguese
      ro.fmuser.org ->Romanian
      ru.fmuser.org ->Russian
      sr.fmuser.org ->Serbian
      sk.fmuser.org ->Slovak
      sl.fmuser.org ->Slovenian
      es.fmuser.org ->Spanish
      sw.fmuser.org ->Swahili
      sv.fmuser.org ->Swedish
      th.fmuser.org ->Thai
      tr.fmuser.org ->Turkish
      uk.fmuser.org ->Ukrainian
      ur.fmuser.org ->Urdu
      vi.fmuser.org ->Vietnamese
      cy.fmuser.org ->Welsh
      yi.fmuser.org ->Yiddish

       
  •  

    FMUSER Wirless Transmit Video And Audio More Easier !

  • Contact

    Address:
    No.305 Room HuiLan Building No.273 Huanpu Road Guangzhou China 510620

    E-mail:
    [email protected]

    Tel / WhatApps:
    +8618078869184

  • Categories

  • Newsletter

    FIRST OR FULL NAME

    E-mail

  • paypal solution  Western UnionBank OF China
    E-mail:[email protected]   WhatsApp:+8618078869184   Skype:sky198710021 Chat with me
    Copyright 2006-2020 Powered By www.fmuser.org

    Contact Us