int* const的强制转换
  • 板块灌水区
  • 楼主cjz13683356870
  • 当前回复8
  • 已保存回复8
  • 发布时间2021/10/18 00:31
  • 上次更新2023/11/4 03:25:35
查看原帖
int* const的强制转换
382198
cjz13683356870楼主2021/10/18 00:31
#include <stdio.h>
#include <string.h>
  
struct Queue {
    int* const  elems;	   	//elems申请内存用于存放队列的元素
    const  int  max;	  	//elems申请的最大元素个数max
    int   head, tail;	 	//队列头head和尾tail,队空head=tail;初始head=tail=0
};

void initQueue(Queue* const p, int m) {
	(int*)(p->elems) = (int*)malloc(m * sizeof(int));
	int* max2 = const_cast<int*>(&(p->max));
	*max2 = m;
	p->head = p->tail = 0;
}

initQueue函数内部的第一行会报错,显示左操作数必须是左值,但是这里明明用了int*的强制转换了,有大佬知道为什么吗?

2021/10/18 00:31
加载中...