c学习笔记:函数(指针数组、二级指针、指针作为函数参数、数组名作为函数参数、字符数组名作为函数参数)

目录

知识点1:多文件编程

知识点2:二级指针

知识点3:指针作为函数的参数

案例1:普通变量作为函数的参数(函数内部修改不了函数外部的值)

案例2:指针变量作为函数的形参

知识点4:数组名作为函数的参数

知识点5:字符数组名作为函数的参数

知识点7:字符串指针变量 与 字符数组的区别

知识点8:字符指针数组


知识点1:多文件编程

指针数组: 本质是数组只是每个元素的类型是指针

 

知识点2:二级指针

 

知识点3:指针作为函数的参数

  • 案例1:普通变量作为函数的参数(函数内部修改不了函数外部的值)

  • 案例2:指针变量作为函数的形参

 

知识点4:数组名作为函数的参数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void myinputarray(int *arr, int n)
{
	printf("请输入%d个int数据:", n);
	for (size_t i = 0; i < n; i++)
	{
		scanf("%d", arr + i);
	}
}

void mymaxint(int *arr, int n, int *p_max)
{
	int tmp_max = 0;
	for (size_t i = 0; i < n; i++)
	{
		if (tmp_max < arr[i])
		{
			tmp_max = arr[i];
		}
	}
	*p_max = tmp_max;
}

void myprintintarray(int *arr, int n)
{
	printf("数组总大小:%d\n", sizeof(arr));
	for (size_t i = 0; i < n; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}


void test()
{
	int arr[5] = { 0 };
	int n = sizeof(arr) / sizeof(arr[0]);

	//定义函数:给arr元素获取键盘输入
	myinputarray(arr, n);

	int max = 0;
	//定义函数:求最大值
	mymaxint(arr, n, &max);
	printf("max=%d\n", max);

	//遍历
	myprintintarray(arr, n);
}

int main(int argc, char *argv[])
{
	test();
	system("pause");
	return 0;
}

 

知识点5:字符数组名作为函数的参数

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void myinputchararray(char *arr, int n)
{
	printf("请输入一个字符串,长度小于%d\n", n);
	fgets(arr, n, stdin);
}

int mystrlen(char *arr)
{
	int count = 0;
	while (arr[count] != '\0')
	{
		count++;
	}
	return count;
}

void mychange(char *arr)
{
	int i = 0;
	while (arr[i] != '\0')
	{
		if (arr[i]>='a' && arr[i]<='z')
		{
			arr[i] -= 'a' - 'A';
		}
		else if (arr[i] >= 'A' && arr[i] <= 'Z')
		{
			arr[i] += 'a' - 'A';
		}
		i++;
	}
}




void test()
{
	char buf[32] = "";
	//给buf获取键盘输入
	myinputchararray(buf, sizeof(buf));
	buf[strlen(buf) - 1] = 0;
	printf("buf = %s\n", buf);

	//获取字符串的实际长度(遇到\0结束)
	int len = 0;
	len = mystrlen(buf);
	printf("len=%d\n", len);

	//将buf大写字符变小写,小写字符变大写
	mychange(buf);
	printf("buf=%s\n", buf);
}


int main(int argc, char *argv[])
{
	test();
	system("pause");
	return 0;
}

 

知识点7:字符串指针变量 与 字符数组的区别

 

知识点8:字符指针数组

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章