1. 程式人生 > 其它 >使用strcpy給字串變數和字串指標賦值

使用strcpy給字串變數和字串指標賦值

技術標籤:指標字串c++visual studio code

使用strcpy給字串變數和字串指標賦值

#include
#include<stdlib.h>
using namespace std;

void Str(char *x,const char *y)
{
char *z;
z=new char; //字串指標要初始化
strcpy(z,y);
cout<<z<<endl;
}

int main( )
{

char a[20], c[] = "student";
strcpy(a,c);
cout << a << endl;
const char * k="teacher";
char *m;
m=new char;
Str(m,k);
return 0;

}
執行結果
在這裡插入圖片描述