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:字符指針數組

 

 

 

 

 

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