Cプログラミング言語:パラメータエラーの問題としてNULLポインタを初期化---理由は、NULLポインタを指しているが、特定のアドレス空間にポイントをしません

関数呼び出しで変更されたアドレス値へのポインタを保存するためのスペースのmallocの割り当てが必要です。
malloc関数にCプライムPLUS READに必要覚えていない
INT *白金と、
白金=(INT *)はmalloc(はsizeof(INT)*能力);
int型ポインタ型へのポインタを宣言し
、アドレスポインタのヘッダ部の位置とメモリサイズの点を

#include<stdio.h>
#include<stdlib.h>
char ** readBinaryWatch(int num, int* returnSize); 
int func(int x);
int main(){
	int *result = NULL;
	char **nums = readBinaryWatch(2, result);
	system("pause");
	return 0;
}

//参数 数组 得到的指向结果的指针
//二级指针 二维数组 根据返回值决定的
char ** readBinaryWatch(int num, int* returnSize){
	//使用结果
	char **res = NULL;
	//结果指针
	returnSize = (int*)malloc(sizeof(int)* 1);
	*returnSize = 0;
	//0到12对应二进制1的个数
	int table[12] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3 };
	//时间 分钟
	for (int minute = 0; minute < 60; ++minute) {
		//设计需要统计分钟数对应的1二进制的个数  可以取余的方式来获取二进制中1的个数
		int i = func(minute);

		for (int hour = 0; hour < 12; ++hour) {
			if (i + table[hour] == num) {
				//给二级指针所指向的空间分配空间
				res = res ? (char**)realloc(res, (1 + (*returnSize)) * sizeof(char*)) : (char**)malloc(sizeof(char*));
				//数组 字符串 相当于 *p 字符串中的每个字母*p++ ????
				res[*returnSize] = (char*)malloc(6 * sizeof(char));
				printf(res[(*returnSize)++], "%d:%02d", hour, minute);
			}
		}
	}
	return res;
}

int func(int x)
{
	int cnt = 0;
	while (x)
	{
		cnt++;
		x = x&(x - 1);
	}
	return cnt;
}
公開された212元の記事 ウォン称賛32 ビュー60000 +

おすすめ

転載: blog.csdn.net/qq_42664961/article/details/104373436
おすすめ