如何从指针获取数组大小? [英] How to get array size from a pointer?

查看:75
本文介绍了如何从指针获取数组大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是考试的一部分,我很难过。有一个功能


void foo(char ** x)


函数签名是给定的,不能更改,所以没有传递

其他值。测试案例涉及定义这个变量:


char * y [] = {/ *一堆东西* /}

并且调用


foo(y)


在上面,一堆东西是一系列三元组,两个字符串

后跟一个空字符串(")。然而,最后一个三元组以一个

整数0结束。这似乎意味着它应该表示

数组的结束。但是,在我看来,对于空字符串(NUL,\0,无论如何),0与

的二进制值相同。所以实际上,人们不能将它作为哨兵值进行测试,因为它实际上与前三胞胎之前的

相同。


怎么样,函数foo()能否确定数组的边界?

知道特定测试用例的界限是不够的,因为

实际测试套件可能有不同大小的数组。

解决方案



< gg ****** @ gmail。在消息中写道

news:37 ********************************** @ o40g2000 prn.googlegroups.com ...


这是测试的一部分,我很难过。有一个功能


void foo(char ** x)


函数签名是给定的,不能更改,所以没有传递

其他值。测试案例涉及定义这个变量:


char * y [] = {/ *一堆东西* /}

并且调用


foo(y)


在上面,一堆东西是一系列三元组,两个字符串

后跟一个空字符串(")。然而,最后一个三元组以一个

整数0结束。这似乎意味着它应该表示

数组的结束。但是,在我看来,对于空字符串(NUL,\0,无论如何),0与

的二进制值相同。所以实际上,人们不能将它作为哨兵值进行测试,因为它实际上与前三胞胎之前的

相同。



任何空字符串都不一定是NULL。


{" ABC",""," GHI" ;,0}


三个非NULL字符串后跟一个NULL。


(vippstar会说NULL与0不同。无论如何;如果所有你必须

标记结束是0,然后使用它并希望你的系统0永远不会是
a合法字符串指针。)


-

bartc


10月8日下午4:11, ggngu ... @ gmail.com" < ggngu ... @ gmail.comwrote:


这是测试的一部分,我很难过。有一个功能


void foo(char ** x)


函数签名是给定的,不能更改,所以没有传递

其他值。测试案例涉及定义这个变量:


char * y [] = {/ *一堆东西* /}

并且调用


foo(y)


在上面,一堆东西是一系列三元组,两个字符串

后跟一个空字符串(")。然而,最后一个三元组以一个

整数0结束。这似乎意味着它应该表示

数组的结束。但是,在我看来,对于空字符串(NUL,\0,无论如何),0与

的二进制值相同。所以实际上,人们不能将它作为哨兵值进行测试,因为它实际上与前三胞胎之前的

相同。



char * y []是一个char *指针数组。当值为0的常量整数

表达式出现在整数上下文中时,它变为一个

空指针常量。

它是如果你写了0或NULL,那就相同。


如何,函数foo()能否确定数组的边界?

了解特定测试用例的范围是不够的,因为实际的测试套件可能有不同大小的数组。



与strlen()类似; Strlen迭代直到遇到一个值为
为0的字节;你的函数将迭代,直到它遇到一个指针,其值为
值为NULL。


10月8日,下午4:22,Bartc。 < b ... @ freeuk.comwrote:


< ggngu ... @ gmail.comwrote in message


新闻:37 ********************************** @ o40g2000 prn.googlegroups.com ...


这是测试的一部分,我很难过。有一个函数


void foo(char ** x)


函数签名是给定的,不能更改,所以没有传递其他值的
。测试用例涉及定义这个变量:


char * y [] = {/ *一堆东西* /}


并且调用


foo(y)


在上面的一堆东西中。是一系列三元组,两个字符串

后跟一个空字符串(")。然而,最后一个三元组以一个

整数0结束。这似乎意味着它应该表示

数组的结束。但是,在我看来,对于空字符串(NUL,\0,无论如何),0与

的二进制值相同。所以实际上,人们不能将它作为哨兵值进行测试,因为它实际上与前三胞胎之前的

相同。



任何空字符串都不一定是NULL。



空字符串是_never_ NULL。


{" ABC","", GHI,0}

三个非NULL字符串后跟一个NULL。



什么是非NULL字符串?一个字符串不能为NULL!这并没有让我感觉到。


(vippstar会说NULL与0不一样。无论如何;如果你有的话to

标记结束为0,然后使用它并希望在你的系统0上永远不会是
a合法的字符串指针。)



Bollocks。不要再把话放在嘴里了。

0永远不能成为合法字符串指针。

NULL保证比较不等于所有地址对象。


It''s part of a test and I''m stumped. There is a function

void foo(char **x)

The function signature is given and cannot be changed, so no passing
of other values. The test case involves defining this variable:

char *y[] = { /* bunch of stuff */ }

and calling

foo(y)

In the above, "bunch of stuff" is a series of triplets, two strings
followed by a null string (""). However, the last triplet ends with an
integer 0. This seems that it''s supposed to signify the end of the
array. However, it appears to me that 0 is the same binary value as
for the empty string (NUL, \0, whatever). So in effect, one cannot
test for it as a sentry value because it''s actually the same as the
preceding triplets.

How then, can the function foo() determine the bounds of the array?
Knowing the bounds of the particular test case is not sufficient since
the actual test suite may have arrays of varying size.

解决方案


<gg******@gmail.comwrote in message
news:37**********************************@o40g2000 prn.googlegroups.com...

It''s part of a test and I''m stumped. There is a function

void foo(char **x)

The function signature is given and cannot be changed, so no passing
of other values. The test case involves defining this variable:

char *y[] = { /* bunch of stuff */ }

and calling

foo(y)

In the above, "bunch of stuff" is a series of triplets, two strings
followed by a null string (""). However, the last triplet ends with an
integer 0. This seems that it''s supposed to signify the end of the
array. However, it appears to me that 0 is the same binary value as
for the empty string (NUL, \0, whatever). So in effect, one cannot
test for it as a sentry value because it''s actually the same as the
preceding triplets.

Any empty string is not necessarily NULL.

{ "ABC","","GHI",0}

Three non-NULL strings followed by a NULL.

(vippstar will say NULL is not the same as 0. Whatever; if all you have to
mark the end is 0, then use it and hope that on your system 0 will never be
a legal string pointer.)

--
bartc


On Oct 8, 4:11 pm, "ggngu...@gmail.com" <ggngu...@gmail.comwrote:

It''s part of a test and I''m stumped. There is a function

void foo(char **x)

The function signature is given and cannot be changed, so no passing
of other values. The test case involves defining this variable:

char *y[] = { /* bunch of stuff */ }

and calling

foo(y)

In the above, "bunch of stuff" is a series of triplets, two strings
followed by a null string (""). However, the last triplet ends with an
integer 0. This seems that it''s supposed to signify the end of the
array. However, it appears to me that 0 is the same binary value as
for the empty string (NUL, \0, whatever). So in effect, one cannot
test for it as a sentry value because it''s actually the same as the
preceding triplets.

char *y[] is an array of char * pointers. When a constant integer
expression with the value 0 appears in integer context, it becomes a
null pointer constant.
It''s the same if you had written 0 or NULL.

How then, can the function foo() determine the bounds of the array?
Knowing the bounds of the particular test case is not sufficient since
the actual test suite may have arrays of varying size.

Like strlen(); Strlen iterates until it encounters a byte whose value
is 0; Your function would iterate until it encounters a pointer whose
value is NULL.


On Oct 8, 4:22 pm, "Bartc" <b...@freeuk.comwrote:

<ggngu...@gmail.comwrote in message

news:37**********************************@o40g2000 prn.googlegroups.com...

It''s part of a test and I''m stumped. There is a function

void foo(char **x)

The function signature is given and cannot be changed, so no passing
of other values. The test case involves defining this variable:

char *y[] = { /* bunch of stuff */ }

and calling

foo(y)

In the above, "bunch of stuff" is a series of triplets, two strings
followed by a null string (""). However, the last triplet ends with an
integer 0. This seems that it''s supposed to signify the end of the
array. However, it appears to me that 0 is the same binary value as
for the empty string (NUL, \0, whatever). So in effect, one cannot
test for it as a sentry value because it''s actually the same as the
preceding triplets.


Any empty string is not necessarily NULL.

An empty string is _never_ NULL.

{ "ABC","","GHI",0}

Three non-NULL strings followed by a NULL.

What is a non-NULL string? A string can''t be NULL! That doesn''t make
sense.

(vippstar will say NULL is not the same as 0. Whatever; if all you have to
mark the end is 0, then use it and hope that on your system 0 will never be
a legal string pointer.)

Bollocks. Don''t put words in my mouth ever again.
0 can never be a "legal string pointer".
NULL is guaranteed to compare unequal to all addresses of objects.


这篇关于如何从指针获取数组大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆