如何获得以下数组的大小

时间:2022-01-22 21:28:14
int first[] = {1, 4};
int second[] = {2, 3, 7};

arrayOfCPointers[0] = first;
arrayOfCPointers[1] = second;

NSLog(@"size of %lu", sizeof(arrayOfCPointers[0]) / sizeof(int));

I want to have an array of sub arrays. Each sub array needs to be a different size. But I need to be able to find out the size of each sub array?

我想要一个子数组的数组。每个子数组都需要有不同的大小。但是我需要知道每个子数组的大小?

The Log keeps returning 1

日志一直返回1

4 个解决方案

#1


4  

You need to store the size somewhere. The language does not do so for bare C arrays. All you have is the address of the first element.

你需要把尺寸放在某个地方。对于裸C数组,语言没有这样做。你只有第一个元素的地址。

#2


2  

I'd write a wrapper class or struct to hold the array and it's metadata (like length).

我将编写一个包装类或struct来保存数组,它是元数据(比如长度)。

typedef struct tag_arrayholder
{
    int* pArray;
    int  iLen;
}ArrayHolder;


    int first[] = {1, 4};

    ArrayHolder holderFirst;
    holderFirst.pArray = first;
    holderFirst.iArrayLen = sizeof(first) / sizeof(int);

    arrayOfCPointers[0] = holderFirst;

    NSLog(@"size of %lu", arrayOfCPointers[0].iLen);

Or, like *foe said, store special value marking the last position (exactly the approach zero-terminated string uses)

或者,就像*foe所说的那样,存储标记最后一个位置的特殊值(正是零终止字符串使用的方法)

#3


0  

The "sizeof" instruction could be used to know the amount of bytes used by the array, but it works only with static array, with dynamics one it returns the pointer size. So with static array you could use this formula : sizeof(tab)/sizeof(tab[0]) to know the size of your array because the first part give you the tab size in bytes and the second the size of an element, so the result is your amount of element in your array ! But with a dynamic array the only way is to store the size somewhere or place a "sentinal value" at the end of your array and write a loop which count elements for you !

“sizeof”指令可以用来知道数组使用的字节数,但是它只对静态数组起作用,动态数组返回指针大小。因此,对于静态数组,您可以使用这个公式:sizeof(tab)/sizeof(tab[0])来了解数组的大小,因为第一部分给出的是字节大小和一个元素的大小,所以结果就是数组中元素的数量!但是,对于动态数组,唯一的方法是将大小存储在某个地方,或者在数组的末尾放置一个“sentinal value”,并编写一个循环来为您计数元素!

(Sorry for my English i'm french :/)

(对不起,我是法国人。)

#4


0  

The NSLog statement is printing the value 1 because the expression you're using is dividing the size of the first element of the array (which is the size of an int) by the size of an int.

NSLog语句将输出值1,因为您使用的表达式将数组的第一个元素(即int的大小)的大小除以int的大小。

So what you currently have is this:

你现在看到的是

NSLog(@"size of %lu", sizeof(arrayOfCPointers[0]) / sizeof(int));

If you remove the array brackets, you'll get the value you're looking for:

如果删除数组括号,就会得到所需的值:

NSLog(@"size of %lu", sizeof(arrayOfCPointers) / sizeof(int));

As other answers have pointed out, this won't work if you pass the array to another method or function, since all that's passed in that case is an address. The only reason the above works is because the array's definition is in the local scope, so the compiler can use the type information to compute the size.

正如其他答案所指出的,如果将数组传递给另一个方法或函数,这将不起作用,因为在这种情况下传递的只是一个地址。上面的操作之所以有效,是因为数组的定义在本地范围内,所以编译器可以使用类型信息来计算大小。

#1


4  

You need to store the size somewhere. The language does not do so for bare C arrays. All you have is the address of the first element.

你需要把尺寸放在某个地方。对于裸C数组,语言没有这样做。你只有第一个元素的地址。

#2


2  

I'd write a wrapper class or struct to hold the array and it's metadata (like length).

我将编写一个包装类或struct来保存数组,它是元数据(比如长度)。

typedef struct tag_arrayholder
{
    int* pArray;
    int  iLen;
}ArrayHolder;


    int first[] = {1, 4};

    ArrayHolder holderFirst;
    holderFirst.pArray = first;
    holderFirst.iArrayLen = sizeof(first) / sizeof(int);

    arrayOfCPointers[0] = holderFirst;

    NSLog(@"size of %lu", arrayOfCPointers[0].iLen);

Or, like *foe said, store special value marking the last position (exactly the approach zero-terminated string uses)

或者,就像*foe所说的那样,存储标记最后一个位置的特殊值(正是零终止字符串使用的方法)

#3


0  

The "sizeof" instruction could be used to know the amount of bytes used by the array, but it works only with static array, with dynamics one it returns the pointer size. So with static array you could use this formula : sizeof(tab)/sizeof(tab[0]) to know the size of your array because the first part give you the tab size in bytes and the second the size of an element, so the result is your amount of element in your array ! But with a dynamic array the only way is to store the size somewhere or place a "sentinal value" at the end of your array and write a loop which count elements for you !

“sizeof”指令可以用来知道数组使用的字节数,但是它只对静态数组起作用,动态数组返回指针大小。因此,对于静态数组,您可以使用这个公式:sizeof(tab)/sizeof(tab[0])来了解数组的大小,因为第一部分给出的是字节大小和一个元素的大小,所以结果就是数组中元素的数量!但是,对于动态数组,唯一的方法是将大小存储在某个地方,或者在数组的末尾放置一个“sentinal value”,并编写一个循环来为您计数元素!

(Sorry for my English i'm french :/)

(对不起,我是法国人。)

#4


0  

The NSLog statement is printing the value 1 because the expression you're using is dividing the size of the first element of the array (which is the size of an int) by the size of an int.

NSLog语句将输出值1,因为您使用的表达式将数组的第一个元素(即int的大小)的大小除以int的大小。

So what you currently have is this:

你现在看到的是

NSLog(@"size of %lu", sizeof(arrayOfCPointers[0]) / sizeof(int));

If you remove the array brackets, you'll get the value you're looking for:

如果删除数组括号,就会得到所需的值:

NSLog(@"size of %lu", sizeof(arrayOfCPointers) / sizeof(int));

As other answers have pointed out, this won't work if you pass the array to another method or function, since all that's passed in that case is an address. The only reason the above works is because the array's definition is in the local scope, so the compiler can use the type information to compute the size.

正如其他答案所指出的,如果将数组传递给另一个方法或函数,这将不起作用,因为在这种情况下传递的只是一个地址。上面的操作之所以有效,是因为数组的定义在本地范围内,所以编译器可以使用类型信息来计算大小。