您现在的位置是:首页 > 学无止境 > C语言网站首页C语言 如何学习C语言、入门C语言,看这篇就够了
如何学习C语言、入门C语言,看这篇就够了
- C语言
- 2019-11-10
简介该文章将会把C语言基础语法梳理一遍,主要记录帮助理解相关知识点的代码示例和重要或容易忽略的面试知识,将会陆续发布系类文章.字数
24775
指针
1.程序 使用指针实现了一个参数和返回值q均为字符串的函数
// 使用指针实现了一个参数和返回值q均为字符串的函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *function(char *n){
char *s1 = "Hello";
// 将n指向的字符串追加到s1尾部
strcat(s1, n);
return s1;
}
int main(int argc, const char * argv[]){
char *name;
name = (char*)malloc(20);
printf("Enter your name: ");
gets(name);
printf("%s",function(name));
}
输出:
Enter your name: Nihaar Chandra
Hello Nihaar Chandra
2.程序 使用指针和函数传递一位数组并返回它
// 使用指针和函数传递一维数组并返回f它
#include <stdio.h>
int *get(int n){
int i;
int *x; // 该指针指向一个数组
// 动态内存分配n个内存块
x = (int*)malloc(n*sizeof(int));
printf("Enter elements: \n");
for (i = 0; i<n; i++)
scanf("%d",x+i);
return x;
}
// 接收一个一维数组并显示它
void display(int *x,int n){
int i;
printf("\n1D array elements: \n");
for (i = 0; i<n; i++)
printf("%d\t",*(x+i));
}
int main(int argc, const char * argv[]){
int *a;
int n;
printf("How many elements? ");
scanf("%d",&n);
a = get(n); // get()函数创建并返回一个数组
display(a, n); // 传递指针参数并显示数组元素
}
输出:
How many elements? 5
Enter elements:
11 22 33 44 60
1D array elements:
11 22 33 44 60
3.程序 使用指针和函数来接收和显示一个二维数组
/ 使用指针和函数来接收和显示一个二维数组
#include <stdio.h>
// 将数据元素保存至二维数组
void get(int *x[],int r,int c){
int i,j;
printf("Enter elements: \n");
for (i = 0; i<r; i++) {
for (j = 0; j<c; j++)
scanf("%d",(*(x+i)+j));
}
}
// 该函数以矩阵形式显示一个二维数组
void display(int *x[],int r,int c){
int i,j;
printf("\nThe array elements are: \n");
for (i = 0; i<r; i++){
for (j = 0; j<c; j++) {
printf("%d\t",*(*(x+i)+j));
}
printf("\n");
}
}
int main(int argc, const char * argv[]){
int *a[50];
int i,r,c;
printf("Enter rows cols: ");
scanf("%d%d",&r,&c);
// r行c列的二维数组分配动态内存
for (i = 0; i<r; i++)
a[i] = (int*)malloc(c*sizeof(int));
// 读入数据元素
get(a, r, c);
// 显示数组元素
display(a, r, c);
}
输出;
Enter rows cols: 3 4
Enter elements:
1 2 3 4 5 6 7 8 9 10 11 12
The array elements are:
1 2 3 4
5 6 7 8
9 10 11 12
4.程序 使用指针数组来表示一组字符串
/ 对字符串排序-冒泡排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 50
void sorting(char *arr[],int n){
int i,pass;
char *t = (char*)malloc(20); // 临时字符串
for (pass = 0; pass<=n-2; pass++) {
for (i = 0; i<=n-2; i++) {
if (stricmp(arr[i],arr[i+1])>0) {
strcpy(t,arr[i]);
strcpy(arr[i], arr[i+1]);
strcpy(arr[i+1], t);
}
}
}
}
int main(int argc, const char * argv[]){
char *city[MAX];
int i,n;
printf("How many strings? ");
scanf("%d",&n);
// 分配动态内存存储字符串
for (i = 0; i<n; i++) {
printf("Enter city name: ");
city[i] = (char*)malloc(20);
gets(city[i]);
}
sorting(city, n);
// 显示已排序的字符串数组
printf("Sorted city names:\n");
for (i = 0; i<n; i++) {
puts(city[i]);
}
}
输出:
How many strings? 5
Enter city name: Hyderabad
Enter city name: Delhi
Enter city name: Kolkata
Enter city name: Bangalore
Enter city name: Beijing
Sorted city names:
Bangalore
Beijing
Hyderabad
Delhi
Kolkata
5.重要的面试题
什么是指针数组?它们有什么用途?
答:单个指针在引用单个数组元素时非常有用,而一个指针数组能代表一组指针,指针数组在处理二维数组或一组字符串时非常有用.
6.重要的面试题
指针与数组之间有什么区别?
答:数组创建在静态内存上,并且数组元素是连续分布的,数组元素通过“数组名+下标”来写读,而指针引用的时堆内存空间随机分配的内存块,只能通过内存地址来访问;
数组容量是在编译期确定的,而指针引用的内存大小可以在运行时更改.
7.程序 创建和 使用函数指针
// 帮助理解创建使用函数指针
#include <stdio.h>
void sum(int a,int b){
printf("sum = %d\n",a+b);
}
int main(int argc, const char * argv[]){
void (*fp)(int a,int b);
fp = sum;
(*fp)(5,10);
fp(5,10);
}
输出:
sum = 15
sum = 15
8.重要的面试题
什么是回调函数机制?
答:通过传递函数地址的方式调用一个函数叫做回调函数机制,为了向函数传递函数地址,需要使用函数指针.
9.程序 回调函数机制动态调用函数
// 回调函数机制动态调用函数
#include <stdio.h>
#include <string.h>
// 按AP邦法规计算税额的函数
float ap_tax(float salesamt){
printf("According to AP Govt rules");
// 税率为25%
return salesamt*0.25;
}
// 按karnataka邦法规计算税额的函数
float karnataka_tax(float salesamt){
printf("According to Karnataka rules");
// 税率为20%
return salesamt*0.20;
}
// 该函数中实现了回调机制,使用传入的函数指针及销售额参数
float cac_tax(float salesamt,float(*fp)(float)){
// 调用fp代表的函数
return (*fp)(salesamt);
}
int main(int argc, const char * argv[]){
float salesamt;
char state[20];
printf("\nEnter sales amount: ");
scanf("%f",&salesamt);
fflush(stdin);
printf("\nEnter the state (AP/Karnataka): ");
gets(state);
// 用户输入AP,调用calc_tax函数时传递的参数为ap_tax函数的地址
if (strcmp(state, "AP")==0)
printf("\nState tax = %.2f",calc_tax(salesamt,ap_tax));
// 用户输入karnatake,调用calc_tax函数时传递的参数为karnatake_tax函数的地址
else if (strcmpi(state, "karnatake")==0)
printf("\nState tax = %.2f",calc_tax(salesamt,karnatake_tax));
else printf("\nWrong state name entered.");
}
输出:
Enter sales amount: 100000.00
Enter the state (AP/Karnataka): ap
According to AP Govt rules
State tax = 25000.00
10.程序 near,far,huge指针
// 指向指针的指针
#include <stdio.h>
int main(int argc, const char * argv[]){
int var =100; // 变量
int *p1; // 指针
int **p2; // 指向指针的指针
p1 = &var; // p1中存储变量的地址
p2 = &p1; // p2中存储指针变量的地址
// p1的值为变量的地址.而*p1代表变量的值
printf("Value through p1 = %d",*p1);
// p2的值为p1 的地址,*p2 为p1的值.即变量的地址,因此**p代表变量的值
printf("\nValue through p2 = %d",**p2);
}
输出:
Value through p1 = 100
Value through p2 = 100
11.重要的面试题
什么是null指针,什么是野指针?
答:null指针指的是不指向任何对象的指针,可以用常量NULL来初始化nill指针,使该指针不指向如何内存地址;
野指针,被声明但是没有被初始化的指针,一个野指针里存的是垃圾数据.
12.重要的面试题
指针常量和常量指针有什么区别?
答:指针常量,指的是指针的值是常量,其值不能被改变,例如:
int *const ptr1;
这意味着ptr只能指向一个整型变量,而不能在后续操作中指向另外一个整型变量.尽管指针常量指向的整型变量的值是可以改变的;
常量指针,代表一个指针指向其变量的值不能改变,例如:
const int*ptr2;
在这里,ptr引用的变量的值不能改变,尽管该指针可以指向其他整数.
转载:
感谢您对莫愁个人博客网站平台的认可,非常欢迎各位朋友分享到个人站长或者朋友圈,但转载请说明文章出处“来源莫愁个人博客 https://www.mochoublog.com/study/367.html”。
- C语言
- 2019-11-10
指针
1.程序 使用指针实现了一个参数和返回值q均为字符串的函数
// 使用指针实现了一个参数和返回值q均为字符串的函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *function(char *n){
char *s1 = "Hello";
// 将n指向的字符串追加到s1尾部
strcat(s1, n);
return s1;
}
int main(int argc, const char * argv[]){
char *name;
name = (char*)malloc(20);
printf("Enter your name: ");
gets(name);
printf("%s",function(name));
}
输出:
Enter your name: Nihaar Chandra
Hello Nihaar Chandra
2.程序 使用指针和函数传递一位数组并返回它
// 使用指针和函数传递一维数组并返回f它
#include <stdio.h>
int *get(int n){
int i;
int *x; // 该指针指向一个数组
// 动态内存分配n个内存块
x = (int*)malloc(n*sizeof(int));
printf("Enter elements: \n");
for (i = 0; i<n; i++)
scanf("%d",x+i);
return x;
}
// 接收一个一维数组并显示它
void display(int *x,int n){
int i;
printf("\n1D array elements: \n");
for (i = 0; i<n; i++)
printf("%d\t",*(x+i));
}
int main(int argc, const char * argv[]){
int *a;
int n;
printf("How many elements? ");
scanf("%d",&n);
a = get(n); // get()函数创建并返回一个数组
display(a, n); // 传递指针参数并显示数组元素
}
输出:
How many elements? 5
Enter elements:
11 22 33 44 60
1D array elements:
11 22 33 44 60
3.程序 使用指针和函数来接收和显示一个二维数组
/ 使用指针和函数来接收和显示一个二维数组
#include <stdio.h>
// 将数据元素保存至二维数组
void get(int *x[],int r,int c){
int i,j;
printf("Enter elements: \n");
for (i = 0; i<r; i++) {
for (j = 0; j<c; j++)
scanf("%d",(*(x+i)+j));
}
}
// 该函数以矩阵形式显示一个二维数组
void display(int *x[],int r,int c){
int i,j;
printf("\nThe array elements are: \n");
for (i = 0; i<r; i++){
for (j = 0; j<c; j++) {
printf("%d\t",*(*(x+i)+j));
}
printf("\n");
}
}
int main(int argc, const char * argv[]){
int *a[50];
int i,r,c;
printf("Enter rows cols: ");
scanf("%d%d",&r,&c);
// r行c列的二维数组分配动态内存
for (i = 0; i<r; i++)
a[i] = (int*)malloc(c*sizeof(int));
// 读入数据元素
get(a, r, c);
// 显示数组元素
display(a, r, c);
}
输出;
Enter rows cols: 3 4
Enter elements:
1 2 3 4 5 6 7 8 9 10 11 12
The array elements are:
1 2 3 4
5 6 7 8
9 10 11 12
4.程序 使用指针数组来表示一组字符串
/ 对字符串排序-冒泡排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 50
void sorting(char *arr[],int n){
int i,pass;
char *t = (char*)malloc(20); // 临时字符串
for (pass = 0; pass<=n-2; pass++) {
for (i = 0; i<=n-2; i++) {
if (stricmp(arr[i],arr[i+1])>0) {
strcpy(t,arr[i]);
strcpy(arr[i], arr[i+1]);
strcpy(arr[i+1], t);
}
}
}
}
int main(int argc, const char * argv[]){
char *city[MAX];
int i,n;
printf("How many strings? ");
scanf("%d",&n);
// 分配动态内存存储字符串
for (i = 0; i<n; i++) {
printf("Enter city name: ");
city[i] = (char*)malloc(20);
gets(city[i]);
}
sorting(city, n);
// 显示已排序的字符串数组
printf("Sorted city names:\n");
for (i = 0; i<n; i++) {
puts(city[i]);
}
}
输出:
How many strings? 5
Enter city name: Hyderabad
Enter city name: Delhi
Enter city name: Kolkata
Enter city name: Bangalore
Enter city name: Beijing
Sorted city names:
Bangalore
Beijing
Hyderabad
Delhi
Kolkata
5.重要的面试题
什么是指针数组?它们有什么用途?
答:单个指针在引用单个数组元素时非常有用,而一个指针数组能代表一组指针,指针数组在处理二维数组或一组字符串时非常有用.
6.重要的面试题
指针与数组之间有什么区别?
答:数组创建在静态内存上,并且数组元素是连续分布的,数组元素通过“数组名+下标”来写读,而指针引用的时堆内存空间随机分配的内存块,只能通过内存地址来访问;
数组容量是在编译期确定的,而指针引用的内存大小可以在运行时更改.
7.程序 创建和 使用函数指针
// 帮助理解创建使用函数指针
#include <stdio.h>
void sum(int a,int b){
printf("sum = %d\n",a+b);
}
int main(int argc, const char * argv[]){
void (*fp)(int a,int b);
fp = sum;
(*fp)(5,10);
fp(5,10);
}
输出:
sum = 15
sum = 15
8.重要的面试题
什么是回调函数机制?
答:通过传递函数地址的方式调用一个函数叫做回调函数机制,为了向函数传递函数地址,需要使用函数指针.
9.程序 回调函数机制动态调用函数
// 回调函数机制动态调用函数
#include <stdio.h>
#include <string.h>
// 按AP邦法规计算税额的函数
float ap_tax(float salesamt){
printf("According to AP Govt rules");
// 税率为25%
return salesamt*0.25;
}
// 按karnataka邦法规计算税额的函数
float karnataka_tax(float salesamt){
printf("According to Karnataka rules");
// 税率为20%
return salesamt*0.20;
}
// 该函数中实现了回调机制,使用传入的函数指针及销售额参数
float cac_tax(float salesamt,float(*fp)(float)){
// 调用fp代表的函数
return (*fp)(salesamt);
}
int main(int argc, const char * argv[]){
float salesamt;
char state[20];
printf("\nEnter sales amount: ");
scanf("%f",&salesamt);
fflush(stdin);
printf("\nEnter the state (AP/Karnataka): ");
gets(state);
// 用户输入AP,调用calc_tax函数时传递的参数为ap_tax函数的地址
if (strcmp(state, "AP")==0)
printf("\nState tax = %.2f",calc_tax(salesamt,ap_tax));
// 用户输入karnatake,调用calc_tax函数时传递的参数为karnatake_tax函数的地址
else if (strcmpi(state, "karnatake")==0)
printf("\nState tax = %.2f",calc_tax(salesamt,karnatake_tax));
else printf("\nWrong state name entered.");
}
输出:
Enter sales amount: 100000.00
Enter the state (AP/Karnataka): ap
According to AP Govt rules
State tax = 25000.00
10.程序 near,far,huge指针
// 指向指针的指针
#include <stdio.h>
int main(int argc, const char * argv[]){
int var =100; // 变量
int *p1; // 指针
int **p2; // 指向指针的指针
p1 = &var; // p1中存储变量的地址
p2 = &p1; // p2中存储指针变量的地址
// p1的值为变量的地址.而*p1代表变量的值
printf("Value through p1 = %d",*p1);
// p2的值为p1 的地址,*p2 为p1的值.即变量的地址,因此**p代表变量的值
printf("\nValue through p2 = %d",**p2);
}
输出:
Value through p1 = 100
Value through p2 = 100
11.重要的面试题
什么是null指针,什么是野指针?
答:null指针指的是不指向任何对象的指针,可以用常量NULL来初始化nill指针,使该指针不指向如何内存地址;
野指针,被声明但是没有被初始化的指针,一个野指针里存的是垃圾数据.
12.重要的面试题
指针常量和常量指针有什么区别?
答:指针常量,指的是指针的值是常量,其值不能被改变,例如:
int *const ptr1;
这意味着ptr只能指向一个整型变量,而不能在后续操作中指向另外一个整型变量.尽管指针常量指向的整型变量的值是可以改变的;
常量指针,代表一个指针指向其变量的值不能改变,例如:
const int*ptr2;
在这里,ptr引用的变量的值不能改变,尽管该指针可以指向其他整数.
转载: 感谢您对莫愁个人博客网站平台的认可,非常欢迎各位朋友分享到个人站长或者朋友圈,但转载请说明文章出处“来源莫愁个人博客 https://www.mochoublog.com/study/367.html”。
下一篇:C语言如何将正整数分解质因数