访问权限冲突,我看了会,也不知道哪儿错了,请大佬指点指点

问题截图

 

这是头文件

#pragma once
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#define MAX_ARRAY_DIM 8//假设数组维度的最大值为8
typedef int ElemType;
typedef struct
{
	ElemType* base;//数组元素基址,由InitArray分配
	int dim;//数组维度
	int* bounds;//数组维界基址,由InitArray分配
	int* constants;//数组映像函数常量基址,InitArray分配
}Array;
//构造数组
bool InitArray(Array* A, int dim, ...);
//销毁数组
void DestroyArray(Array* A);
//求某元素的相对地址
bool Locat(Array A, va_list ap, int* off);
//
bool Value(ElemType* e, Array A, ...);
//
bool Assign(Array A, ElemType e, ...);

 

这是函数的实现

// 构造数组
bool InitArray(Array* A, int dim, ...)
{
	int elemtotal = 1, i;//elemtotal是数组元素总数,初值为1
	va_list ap;//变长参数表类型,在stdarg.h中
	if (dim<1 || dim>MAX_ARRAY_DIM)//数组维数超出范围
		return false;
	A->dim = dim;//数组维度
	A->bounds = (int*)malloc(dim * sizeof(int));//动态分配数组维界基址
	if (!A->bounds)
		exit(-1);
	va_start(ap, dim);//变长参数“...”从形参dim之后开始
	for (i = 0; i < dim; i++)
	{
		A->bounds[i] = va_arg(ap, int);//将逐一变长参数赋值给A->bounds[i]
		if (A->bounds[i] < 0)
			return UNDERFLOW;//在math.中被定义为4
		elemtotal *= A->bounds[i];//数组元素总数=各维长度之乘积
	}
	va_end(ap);//结束提取变长参数
	A->base = (ElemType*)malloc(elemtotal * sizeof(ElemType));//动态分配数组存储空间
	if (!A->base)
		exit(-1);
	A->constants = (int*)malloc(dim * sizeof(int));//动态分配数组偏移量基址
	if (!A->constants)
		exit(-1);
	A->constants[dim - 1] = 1;//最后一维的偏移量为1
	for (i = dim - 2; i >= 0; i--)
		A->constants[i] = A->bounds[i + 1] * A->constants[i + 1];//每一维的偏移量
	return true;
}
//销毁数组
void DestroyArray(Array* A)
{
	if (A->base)//A.base指向存储单元
		free(A->base);//释放A.base所指向的存储单元
	if (A->bounds)
		free(A->bounds);
	if (A->constants)
		free(A->constants);
	A->base = A->bounds = A->constants = NULL;//使他们不在指向任何存储单元
	A->dim = 0;
}
//求某元素的相对地址
bool Locat(Array A, va_list ap, int* off)
{
	int i, ind;
	off = 0;
	for (i = 0; i < A.dim; i++)
	{
		ind = va_arg(ap, int);//逐一读取各维的下标值
		if (ind < 0 || ind >= A.bounds[i])//各维的下标值不合法
			return -1;
		off += A.constants[i] * ind;//相对地址=各维的下标值*本维的偏移量之和
	}
	return true;
}
//
bool Value(ElemType* e, Array A, ...)
{
	va_list ap; // 变长参数表类型,在stdarg.h中
	int off;
	va_start(ap, A);//变长参数“...”从形参A之后开始
	if (Locat(A, ap, &off) == -1)//调用Locat(),求得变长参数所指单元的相对地址off
		return false;
	e = *(A.base + off);//将变长参数所指单元的值赋给e
	return true;
}
//
bool Assign(Array A, ElemType e, ...)
{
	va_list ap;
	int off;
	va_start(ap, e);
	if (Locat(A, ap, &off) == -1)
		return false;
	*(A.base + off) = e;//将e的值赋给变长参数所指单元
	return true;
}

 

问题出在这

bool Assign(Array A, ElemType e, ...)
{
	va_list ap;
	int off;
	va_start(ap, e);
	if (Locat(A, ap, &off) == -1)
		return false;
//问题出在这,访问冲突
	*(A.base + off) = e;//将e的值赋给变长参数所指单元
	return true;
}

 

这是main 函数

int main()
{
	Array A;
	int i, j, k, dim = 3, bound1 = 3, bound2 = 4, bound3 = 2;//A[3][4][2]
	ElemType e;
	InitArray(&A, dim, bound1, bound2, bound3);
	printf("A.base=");
	for (i = 0; i < dim; i++)//顺序输出A.bounds
		printf("%d ", *(A.bounds + i));
	printf("\nA.constants=");
	for (i = 0; i < dim; i++) // 顺序输出A.constants
		printf("%d ", *(A.constants + i));
	printf("\n%d页%d行%d列矩阵元素如下:\n", bound1, bound2, bound3);
	for (i = 0; i < bound1; i++)
	{
		for (j = 0;j< bound2; j++)
		{
			for (k = 0;k< bound3; k++)
			{
				Assign(A, i * 100 + j * 10 + k, i, j, k);//将i*100+j*10+k赋值给A[i][j][k]
				Value(&e, A, i, j, k);//将A[i][j][k]的值赋给e
				printf("A[%d][%d][%d]=%2d", i, j, k, e);//输出A[i][j][k]
			}
			printf("\n");
		}
		printf("\n");
	}
	printf("A.base=\n");
	for (i = 0; i < bound1 * bound2 * bound3; i++)//顺序输出A.base
	{
		printf("%4d", *(A.base + i));
		if (i % (bound2 * bound3) == bound2 * bound3 - 1)
			printf("\n");
	}
	printf("A.dim=%d\n", A.dim);
	DestroyArray(&A);
	return 0;
}

 

确保你 A 申请了足够的空间