書籍整理內容:
最近在看C primer plus(加深鞏固自己的C語言技巧,爲以後學習C++打個基礎)。
裏面知識針對自己以後要查的點整理出來。
使用工具:visual studio 2013
第二、三章內容:概述、變量、基本數據類型等:https://blog.csdn.net/answerMack/article/details/103766020
第四章內容:字符串和格式化輸入輸出:https://blog.csdn.net/answerMack/article/details/103805900
第五章內容:運算符、表達式和語句:https://blog.csdn.net/answerMack/article/details/103855794
第六章內容:循壞、賦值運算符等:https://blog.csdn.net/answerMack/article/details/103870182
第七章內容:if、if else、?:、switch、goto、continue、邏輯運算符優先級https://blog.csdn.net/answerMack/article/details/103891048
第八章內容:字符輸入輸出函數、輸入驗證(混合輸入)https://blog.csdn.net/answerMack/article/details/103953376
第九章內容:函數和指針:https://blog.csdn.net/answerMack/article/details/103978471
目錄
數組(重點:自動存儲類別)
初始化數組
1、直接賦值
例子:
判斷數組大小:sizeof
第三章內容:
2.指定初始化器
3.循環賦值
重點:
C不允許數組作爲一個單元賦給另一個數組,初始化外也不允許使用花括號形式賦值。
4.無效例子
C語言的編譯器:信任程序員,不檢查數組邊界。數組元素編號從0開始,最好是在聲明數組時使用符號常量表示數組的大小。
多維數組
二維數組
float rain[5][12] ;//數組rain是一個內含5個數組元素的數組,每個元素內含12個float類型的值的數組,即rain是一個5行12列的二維數組。
通過後面指針可知,rain爲rain[0]的地址,是包含12個float類型值的指針。
聲明函數形參時:float (* pt)[12]
初始化:
多維數組
指針和數組關係(指針見第九章&和*,%p)
short dates[4];//short類型, 其中dates爲數組初始地址,*dates爲dates[0]的值
dates+2==&dates[2];//相同的地址
*(dates+2)==dates[2]; //相同的值
1、指針表示數組
*(dates+2)== dates[2]; //相同的值
2、數組表示指針
數組名爲該數組首元素的地址
pti=dates+2== &dates[2];//相同的地址
202-01-30在家太懶了,哈哈哈。
函數、數組、指針關係
數組名是該數組首元素的地址。
函數引用數組方法:
1、指針引用
定義:
int marbles[20]={1,2,3,4,5,6,7,8,9,10,......};//數組初始化
total=sum(marbles);//數組名爲數組首地址,傳遞給sum函數
sum函數定義:
int sum(int * pt){
int total=0;
for(int i=0;i<20;i++)
total+=pt[i];
return total;
}
2、數組引用
int marbles[20]={1,2,3,4,5,6,7,8,9,10,......};//數組初始化
total=sum(marbles);//數組名爲數組首地址,傳遞給sum函數
sum函數定義:
int sum(int pt[]){
int total=0;
for(int i=0;i<20;i++)
total+=pt[i];
return total;
}
例程:(注意函數定義及內部求和方法,見註釋)
#include <stdio.h>
#define SIZE 10
int sum(int * ar, int n);////int sum(int ar[], int n);
int main(void)
{
int marbles[SIZE] = { 20,10,5,39,4,16,19,26,31,20 };
long answer;
answer = sum(marbles, SIZE);
printf("The total number of marbles is %ld.\n", answer);
printf("The size of marbles is %zd bytes.\n",
sizeof marbles);
getchar();
return 0;
}
int sum(int * ar, int n) // int sum(int ar[], int n)
{
int i;
int total = 0;
for (i = 0; i < n; i++)
total += *(ar+i);////total += ar[i];
printf("The size of ar is %zd bytes.\n", sizeof ar);
return total;
}
指針形參
例子:
書本錯誤
例子:
#include <stdio.h>
int data[2] = { 100, 200 };
int moredata[2] = { 300, 400 };
int main(void)
{
int * p1, *p2, *p3;
p1 = p2 = data;
p3 = moredata;
printf(" *p1 = %d, *p2 = %d, *p3 = %d\n",
*p1, *p2, *p3);
printf("*p1++ = %d, *++p2 = %d, (*p3)++ = %d\n",
*p1++, *++p2, (*p3)++);
printf(" *p1 = %d, *p2 = %d, *p3 = %d\n",
*p1, *p2, *p3);
getchar();
return 0;
}
指針操作
int * a;int * b;
int i;
int m[10];
指針變量的8種基本操作:
1、賦值:a=m; 數組名、a=&i;地址運算符&、a=b;指針賦值(同類型)
2、解引用:i=*b;
3、取址:a=&a;指針變量也有自己的地址和值。
4、指針和整數相加:a=m;a=a+2;//相當於m[2]的地址。(注意範圍)
5、遞增指針:a=m;a++;因爲int爲4字節(根據系統不定),a+1相當於當前a地址後移4個字節(注意範圍)
6、指針減去一個整數:(注意範圍)
7、遞減指針:(注意範圍)
8、指針求差:a=m;b=m+5;a-b就爲兩個元素m[0]與m[5]之間的距離,相隔5個int,5x4字節。
9、比較:a=m;b=m+5;while(a<b){ }前提是兩個指針都指向相同類型的對象。
不要解引用未初始化的指針!!!!!
指針的基本用法:
第一種用法:見第九章
第二種用法:見第十章(本章)。
使用const
1、不能修改數組中的數據:
指針和多維數組
增加數組維數會增加指針的複雜度。
多維數組越多,解引用越多。對二維數組名解引用兩次才能得到儲存在數組中的值。
#include <stdio.h>
int main(void)
{
int zippo[4][2] = { { 2,4 },{ 6,8 },{ 1,3 },{ 5, 7 } };
printf(" zippo = %p, zippo + 1 = %p\n",
zippo, zippo + 1);
printf("zippo[0] = %p, zippo[0] + 1 = %p\n",
zippo[0], zippo[0] + 1);
printf(" *zippo = %p, *zippo + 1 = %p\n",
*zippo, *zippo + 1);
printf("zippo[0][0] = %d\n", zippo[0][0]);
printf(" *zippo[0] = %d\n", *zippo[0]);
printf(" **zippo = %d\n", **zippo);
printf(" zippo[2][1] = %d\n", zippo[2][1]);
printf("*(*(zippo+2) + 1) = %d\n", *(*(zippo + 2) + 1));
getchar();
return 0;
}
指向多維數組的指針
指針兼容性
int (* pa)[3];//pa指向一個內含3個int類型數值的數組。(列數)
2020-01-31 星期五
例子
#include <stdio.h>
#define ROWS 3
#define COLS 4
void sum_rows(int ar[][COLS], int rows);
void sum_cols(int[][COLS], int); // ok to omit names
int sum2d(int(*ar)[COLS], int rows); // another syntax int sum2d(int ar[][COLS], int rows);
int main(void)
{
int junk[ROWS][COLS] = {
{ 2,4,6,8 },
{ 3,5,7,9 },
{ 12,10,8,6 }
};
sum_rows(junk, ROWS);
sum_cols(junk, ROWS);
printf("Sum of all elements = %d\n", sum2d(junk, ROWS));
getchar();
return 0;
}
void sum_rows(int ar[][COLS], int rows)
{
int r;
int c;
int tot;
for (r = 0; r < rows; r++)
{
tot = 0;
for (c = 0; c < COLS; c++)
tot += ar[r][c];
printf("row %d: sum = %d\n", r, tot);
}
}
void sum_cols(int ar[][COLS], int rows)
{
int r;
int c;
int tot;
for (c = 0; c < COLS; c++)
{
tot = 0;
for (r = 0; r < rows; r++)
tot += ar[r][c];
printf("col %d: sum = %d\n", c, tot);
}
}
int sum2d(int ar[][COLS], int rows)
{
int r;
int c;
int tot = 0;
for (r = 0; r < rows; r++)
for (c = 0; c < COLS; c++)
tot += ar[r][c];////tot += *(*(ar + r) + c);
return tot;
}
tot += ar[r][c];等價於tot += *(*(ar + r) + c);
int sum2d(int(*ar)[COLS], int rows);等價於int sum2d(int ar[][COLS], int rows);
//數組名junk是指向數組首元素的指針,首元素是子數組。
變長數組
int sum2d(int a,int b,int ar[a][b]);
vs 例程沒成功(自己沒成功)
複合字面量
vs 例程沒成功(自己沒成功)
本章小結:
2020-02-01 疫情中。祝大家平平安安,順順利利!!!!
從鍵盤讀取數組 scanf_s
scanf_s與getchar區別見第八章
void get_num(double ar[][COLS], int rows)
{
int r, c;
double a = 1.0; //是爲了讓編譯器鏈接浮點鏈接庫
printf("Please enter 3 groups of numbers, and there\n"
"are 5 numbers in each group:\n");
for (r = 0; r < rows; r++)
for (c = 0; c < COLS; c++)
scanf_s("%lf", &ar[r][c]);
getchar();
}
2020-03-27 大連