我試圖找出一個數組的長度,但我得到奇異數...計算長度給出了奇怪的結果
#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。
我想我會得到兩個相似的大小,所以它顯示我有我的數組中的一個元素。
我在做什麼錯了?
我假設教程沒有關於'sizeof'運算符的一章,是否......>。< – 2013-06-18 11:59:42