code.club

 找回密碼
 立即註冊
搜索
樓主: enter
打印 上一主題 下一主題

基本重要觀念筆記

[複製鏈接]
14#
 樓主| 發表於 2015-9-26 15:48:04 | 只看該作者
本帖最後由 enter 於 2015-9-26 15:55 編輯

char str[]="  ";//空字也算一個字
cout<<"Size of this string is:   "<<sizeof(str)<<endl<<"Length of this string is: "<<strlen(str);
Size of this string is:   3
Length of this string is: 2

記憶體大小比字串長度多一,因為最後都要加一個 null('\0)結束字元。\n \t \a ....這些字符也算在長度之內。
回復 支持 反對

使用道具 舉報

13#
 樓主| 發表於 2015-9-14 16:44:01 | 只看該作者
return和exit()的區別:

相同點:
二者都是讓現有的執行函式結束。
相異點:
一、return是函數的結束功能,exit(0)則是來自系統的中止。
二、return之後的函式和程式會繼續進行,但exit(0)則會完全終止這個程式。
三、return的值取決於函式的回傳值型態,如果是void f()則寫return;即可,否則會寫return 0;之類的。
四、exit(0)表示程式正常結束,除0之外的數就表示程式是非正常結束,但這是你自己定的,程式還是跑完,只是寫非0的數最後會回報說failed。
回復 支持 反對

使用道具 舉報

12#
 樓主| 發表於 2015-9-10 15:05:28 | 只看該作者
char *p1,*p2, str[80];
p1=str;
gets(str); 或 gets(p);
都可以cout<<p<<str;

但如果是str=p1;則不可以,因為str[80]是陣列,不能直接賦值。

而如果要 *p1=*p2; 則其中一個需要先給予一個空間,p=(char *)malloc(80); 如此才能讓*p1,*p2都能cout。
回復 支持 反對

使用道具 舉報

11#
 樓主| 發表於 2015-9-2 23:38:36 | 只看該作者
本帖最後由 enter 於 2015-9-2 23:50 編輯

#include <stdio.h>
#include <stdlib.h>

int main(){

char str[20],*p;

p=str;//可以將字符陣列直接賦值給指標,但是要在輸入字串之前。

printf("Enter a string:\n");

scanf("%s",p);

str=p;//error: array type 'char [20]' is not assignable -->不能直接賦值給字符陣列

p=str;//在已取得字串之後則不行,會得到錯誤。

printf("%s",str);

       
        return 0;
}
回復 支持 反對

使用道具 舉報

10#
 樓主| 發表於 2015-8-29 01:40:37 | 只看該作者
#include <iostream>
using namespace std;

int main(){
       
        char s[10]="123456789";
       
        cout << sizeof(s) <<endl << strlen(s);//10 & 9
       
        return 0;
}

sizeof()是算出陣列所佔記憶體的大小,所以是得到10。而strlen()則是求字串的長度,所以不會去計算最後一個\0,所以只得出9而已。
回復 支持 反對

使用道具 舉報

9#
 樓主| 發表於 2015-8-19 13:59:54 | 只看該作者
本帖最後由 enter 於 2015-8-19 14:04 編輯





回復 支持 反對

使用道具 舉報

8#
 樓主| 發表於 2015-8-17 23:32:28 | 只看該作者
#include <stdio.h>
#include <Stdlib.h>

int main(void){
       
        int a,b;
       
        a=0;
        b=10;
        a = 5>3&&2||8<4-(b=!0);
       
        printf("%d\n%d\n",a,b); // a=0, b=10,因為邏輯運算中,只要可以確定答案,則後面的b電腦就不會再去算,而直接印出它的原值。
       
        return 0;
}
回復 支持 反對

使用道具 舉報

7#
 樓主| 發表於 2015-8-14 11:59:47 | 只看該作者
#include <iostream>
using namespace std;

int main(void){
       
        int a=0,b=0,c=2,d=0,e=2,f=2;
        cout << a << " " << a++ << " " << endl; //1,0
        cout << ++b <<" " << b++ << " " << endl;//2,0
        cout << c << " " << (c++)+(++c) << " " << endl;//4,6
        cout << (d=f++)+(e=f) << endl;//4
        cout << f << " " << d << " " << e << endl;//3,2,2
       
        return 0;
}
回復 支持 反對

使用道具 舉報

6#
 樓主| 發表於 2015-7-30 22:11:39 | 只看該作者
char str1[5] = "hello";
char str2[5]= {'h','e','l','l','o'};

二者是不同的。前者是字串,最後要加上一個結尾的'\0',所以陣列子要是 str1[6],否則在C++編譯器中會出錯,在部份C編譯器中雖可通過,但有的也會出錯。
而str2[5]則是字符陣列,五個就是五個,無需最後加一。
回復 支持 反對

使用道具 舉報

5#
 樓主| 發表於 2015-7-22 15:26:38 | 只看該作者
#include <stdio.h>
#include <stdlib.h>

struct book {
        char name[20];
        int price;
        } *q1;
       
int main(void){

int x,y,*p;
struct book *q2;
p=&x;
q1=(struct book*)malloc(sizeof(struct book)); //結構的指標要先配置一塊記憶體,無論在哪宣都一樣
q2=(struct book*)malloc(sizeof(struct book));
printf("enter a number:\n");
scanf("%d",p); //一般指標就是位址,不用前面加上&
printf("you enter %d.\n",x);

printf("enter the name and price of book.\n");
gets(q1->name);//在字串前就不用&
scanf("%d",&q1->price); //但結構指標不知為何,在數字上前面還是要加上&
printf("the price for %s is %d.\n",q1->name,q1->price);

return 0;
}
回復 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-5-19 06:03 , Processed in 0.098829 second(s), 14 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表