字符數(shù)組名和字符指針大部分情況下使用沒有差別,但是——

①字符數(shù)組名不能被賦值
②字符指針只存地址,不存內容
2.變量的生命周期調用其他函數(shù)進行分配空間時要注意,在其他函數(shù)里申請的變量在被調函數(shù)結束后就沒了。除非malloc
3.C語言傳參全部是按值傳遞想通過其他函數(shù)改一個變量的值,要傳指針
Q1
已知char * strcpy(char * s1,const char * s2)的功能是:
將s2指向的字符串拷貝到s1指向的字符數(shù)組中,返回s1的值。
以下代碼對嗎?
char * r;
char s[]=“hearts”;
strcpy (r, s);Q2
以下代碼對嗎?
int main(void)
{
char * string1, string2[6]={'\0'};
string1="hello";
string2="hello";
printf("%s\n",string1);
printf("%s\n",string2);
return 0;
}Q3
以下代碼對嗎?
void GetMemory(char *p)
{
p = malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}Q4
以下代碼對嗎?
char * GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}Q5
以下代碼對嗎?
char * GetMemory(void)
{
char * p= "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
Q6
以下代碼對嗎?
char * * f(int length)
{
int i;
char * a[5];
for(i=0;i<=4;i++)
a[i]=malloc(sizeof(char)*length);
return a;
}
main()
{
char * *ptr;
int i;
ptr=f(100);
for(i=0;i<=4;i++)
gets(*(ptr+i));
for(i=0;i<=4;i++)
puts(*(ptr+i));
system("pause");
}你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
當前標題:C指針用法糾錯題-創(chuàng)新互聯(lián)
文章起源:http://chinadenli.net/article0/ccgdio.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供響應式網站、全網營銷推廣、網站導航、網站收錄、用戶體驗、自適應網站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)