gpt4 book ai didi

计算数组的长度给出了奇怪的结果

转载 作者:太空宇宙 更新时间:2023-11-04 05:19:37 24 4
gpt4 key购买 nike

<分区>

我试图找出数组的长度,但我得到了奇怪的数字......

#include <stdio.h>
#include <stdlib.h>

const int numOfCoordinates = 100;

typedef struct {
int x;
int y;
int counter;
} Coordinate;

Coordinate *coordinatesMainArray;
Coordinate endPoint;
Coordinate startPoint;


int main(int argc, const char * argv[])
{
endPoint.x = 8;
endPoint.y = 3;
endPoint.counter = 0;

startPoint.x = 1;
startPoint.y = 4;

coordinatesMainArray = malloc(sizeof(Coordinate) * 1);
coordinatesMainArray[0] = endPoint;



int a = sizeof(coordinatesMainArray);
int b = sizeof(coordinatesMainArray[0]);
int coordinatesMainArrayLength = (a / b);

这是我的代码,直到我需要 coordinatesMainArray 的长度的部分。但是我得到 a = 8 和 b = 12。

我假设我会得到两个相似的大小,所以它显示我的数组中有一个元素。

我做错了什么?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com