Ⅰ c語言strcpy()用法
1、strcpy函數是復制字元串的,接受兩個參數,一個是被復制字元串,另一個新字元串。具體的用法,首先打開編輯器新建一個c語言的程序文件,寫入頭文件和主函數:
Ⅱ C語言指針
可能是你的LCD_ShowString函數的參數是u8*類型的,而你的JOYPAD_SYMBOL_TBL是const u8*
,所以要做類型匹配。
Ⅲ 如何用c語言實現CString的構造函數,析構函數和賦值函數
類是編程人員表達自定義數據類型的C++機制。它和C語言中的結構類似,C++類
支持數據抽象和面向對象的程序設計,從某種意義上說,也就是數據類型的設
計和實現。
那麼
String
類的原型如下
class
String
{
public:
String(const
char
*str=NULL);
//構造函數
String(const
String
&other);
//拷貝構造函數
~String(void);
//析構函數
String&
operator=(const
String
&other);
//等號操作符重載,賦值函數
ShowString();
private:
char
*m_data;
//字元指針
};
String::~String()
{
delete
[]
m_data;
//析構函數,釋放地址空間
}
String::String(const
char
*str)
{
if
(str==NULL)//當初始化串不存在的時候,為m_data申請一個空間存放'/0';
{
m_data=new
char[1];
*m_data='/0';
}
else//當初始化串存在的時候,為m_data申請同樣大小的空間存放該串;
{
int
length=strlen(str);
m_data=new
char[length+1];
strcpy(m_data,str);
}
}
String::String(const
String
&other)//拷貝構造函數,功能與構造函數類似。
{
int
length=strlen(other.m_data);
m_data=new
[length+1];
strcpy(m_data,other.m_data);
}
String&
String::operator
=(const
String
&other)
//賦值函數
{
if
(this==&other)//當地址相同時,直接返回;
return
*this;
delete
[]
m_data;//當地址不相同時,刪除原來申請的空間,重新開始構造;
int
length=sizeof(other.m_data);
m_data=new
[length+1];
strcpy(m_data,other.m_data);
return
*this;
}
String::ShowString()//由於m_data是私有成員,對象只能通過public成員函數來訪問;
{
cout<<this->m_data<<endl;
}
測試一下:
main()
{
String
AD;
char
*
p="ABCDE";
String
B(p);
AD.ShowString();
AD=B;
AD.ShowString();
}
Ⅳ c語言編程:字元串拷貝(程序改錯)
不知道你這是做的題目還是練習什麼的?這題有很大的問題。
1字元串數組大小的問題,然後因為這個導致後面函數strcpy和後面的字寫函數都有問題。哪怕代碼改的可以正常運行,以後肯定還是會有指針溢出
#include <stdio.h>
#include <string.h>
#define BUF_SIZE 10
#define setString for (i=0;i<BUF_SIZE;i++) string1[i]=i
#define showString printf("[%s]\n",string1)
void strCpy1(char* t,const char *f);
void strCpy2(char* t,const char *f);
void strCpy3(char* t,const char *f);
int main() {
int i;
char string[BUF_SIZE]="123456789";
char string1[BUF_SIZE];
setString;
strcpy(string1,string);
showString;
setString;
strncpy(string1,string,strlen(string)+1);//change (1)
showString;
setString;
strCpy1(string1,string);
showString;
setString;
strCpy2(string1,string);
showString;
setString;
strCpy3(string1,string);
showString;
return 0;
}
void strCpy1(char* t,const char *f) {
for (unsigned i=0;i<strlen(f)+1;i++)//change (2)
t[i]=f[i];
}
void strCpy2(char* t,const char *f) {
while ((*t++=*f++)!='\0');//change3
}
void strCpy3(char* t,const char *f) {
while ((*t++=*f++)!='\0'); //change 4
}
Ⅳ C語言中如何判斷是不是合法轉義字元
'