怎么了?szname不对了?strncpy(szname,"testname",9); 这行有问题吧

解决方案 »

  1.   

    strncpy(szname,"testname",9); 
    改成
    strcpy(szname,"testname",strlen("testname"));
    szname[strlen("testname")] = '\0';
    再试试 
      

  2.   


    ……strcpy只能传2个参数吧直接strcpy(szname,"testname");就可以了,后面会自动加0x00.
    或按这位仁兄的思路应该用memcpy
      

  3.   

    strncpy(szname,"testname",9); 
    也不会有问题。
      

  4.   

    c++ 
    struct CTest 

    int nage; 
    char szname[128]; 

    void SetTestData(CTest *pTest) 

    pTest->nage = 10; 
    strncpy(pTest->szname,"testname",9); 
    } C# 
    using System.Runtime.InteropServices; 
    [StructLayout(LayoutKind.Sequential)] 
    public struct CTest 

    public int nage; 
    [MarshalAs(UnmanagedType.LPStr ,sizeset = 128)]    
    public  string  szname;  

    CTest ptest  = new CTest(); 
    SetTestData(ref ptest); 执行结果 
    ptest.nage = 10; 
    ptest.szmame = "";  // 这个值没有返回
    期待解决,谢谢! 谢谢大家, 我问问题的关键是在定义一个结构体,结构体里有字符串,如何转入字符串,不应该是strcpy strncpy这类问题