strcpy_s 用法 及 指针数组的理解
2011-02-28 16:40
435 查看
正确用法:
int n = 6;
char **argsmy = new char *
;
int maxlen = 600;
for(int i = 0; i < n; i ++)
{
argsmy[i] = new char [maxlen];// args[i];
}
strcpy_s(argsmy[1],maxlen,"e");
strcpy_s(argsmy[2],maxlen,"Lzma_");
strcat_s(argsmy[2], 600, cTAppEncTop.getBitstreamFile());
strcpy_s(argsmy[3],maxlen,"-BS12");
strcpy_s(argsmy[4],maxlen,"-CN0");
strcpy_s(argsmy[5],maxlen,"-d15");
错误用法:
argsmy[2] = "Lzma_"; strcpy_s(argsmy[2],maxlen,"see");
原因:
argsmy[2] = "Lzma_"; //因为 argsmy[2] 是个指针。他指向一块分配的空间 ,长度 maxlen。
而这样赋值后,指针指向位置变了。而再strcpy_s(argsmy[2],maxlen,"see"); 实际上是将常数变量空间强制赋值。因此出问题。
strcpy_s 用法:
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);
template <size_t size>
errno_t strcpy_s(
char (&strDestination)[size],
const char *strSource
); // C++ only
例子:
C/C++ code
int n = 6;
char **argsmy = new char *
;
int maxlen = 600;
for(int i = 0; i < n; i ++)
{
argsmy[i] = new char [maxlen];// args[i];
}
strcpy_s(argsmy[1],maxlen,"e");
strcpy_s(argsmy[2],maxlen,"Lzma_");
strcat_s(argsmy[2], 600, cTAppEncTop.getBitstreamFile());
strcpy_s(argsmy[3],maxlen,"-BS12");
strcpy_s(argsmy[4],maxlen,"-CN0");
strcpy_s(argsmy[5],maxlen,"-d15");
错误用法:
argsmy[2] = "Lzma_"; strcpy_s(argsmy[2],maxlen,"see");
原因:
argsmy[2] = "Lzma_"; //因为 argsmy[2] 是个指针。他指向一块分配的空间 ,长度 maxlen。
而这样赋值后,指针指向位置变了。而再strcpy_s(argsmy[2],maxlen,"see"); 实际上是将常数变量空间强制赋值。因此出问题。
strcpy_s 用法:
errno_t strcpy_s(
char *strDestination,
size_t numberOfElements,
const char *strSource
);
template <size_t size>
errno_t strcpy_s(
char (&strDestination)[size],
const char *strSource
); // C++ only
例子:
C/C++ code
// crt_strcpy_s.cpp // This program uses strcpy_s and strcat_s // to build a phrase. // #include <string.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> int main( void ) { char string[80]; // using template versions of strcpy_s and strcat_s: strcpy_s( string, "Hello world from " ); strcat_s( string, "strcpy_s " ); strcat_s( string, "and " ); // of course we can supply the size explicitly if we want to: strcat_s( string, _countof(string), "strcat_s!" ); printf( "String = %s/n", string ); }
相关文章推荐
- 理解C语言的数组和指针
- 指针与数组用法笔记
- 你真的理解指针、数组和字符串吗?
- 技术随笔:深入理解指针数组,数组指针,指向指针的指针,数组名含义
- C语言 字符数组和字符串指针的理解
- 理解指针数组、数组指针、函数指针、函数指针数组、指向函数指针数组的指针
- 进一步理解指针2:双指针、指针数组和数组指针
- 指针数组、数组指针、函数指针的理解
- 对于数组指针的一点理解
- 数组指针的理解
- 所谓"在表达式中,数组名总是被转换为指向该数组第一个元素的指针"应该如何理解?
- main函数参数的使用和指针数组的理解结合实例
- 对于数组与指针的理解
- C语言语法笔记 – 高级用法 指针数组 指针的指针 二维数组指针 结构体指针 链表
- C语言中 指针的基础知识总结, 指针数组的理解
- 关于指针和数组的重新理解
- 数组概念与理解及数组与指针的区别
- 深入理解C语言特性-指针 数组 声明
- 程序猿之--C语言细节13(二维数组和指针,&*a[i][0]的理解,数组1[e]和e[1]很可能你没见过)
- 关于函数指针数组和函数指针数组的指针的一些理解