确定指针数组的大小 [英] Determining the size of array of pointers

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

问题描述

hii



我使用二维char数组指针来保存名称列表如下:



int size;



char ** name;

name = new char * [size];



我正在读取用户的尺寸,然后我将用户输入的名称存储在此数组中。



现在我的问题是通过我的代码我怎么能得到这个数组的大小,即我想知道有多少名字被读?



感谢

hii

I am using two dimensional char array of pointers for saving list of names as follows :

int size;

char** name;
name=new char*[size];

I am reading the "size" from the user and then I am storing the names entered by the user in this array.

Now my question is through my code how can i get the size of this array, i.e I wanted to know how many names are being read?

Thankss

推荐答案





实际上你不能这样做,因为数组是动态分配的(=你使用运算符<我>新分配它)。因此,您必须记住变量中数组的长度(例如您拥有的 size ),并且需要将此大小作为参数传递给需要注意的所有方法/函数大小。



(如果你在类的方法中使用这个代码,你可以将数组的大小存储在一个成员变量中,这样就不需要了将它作为参数传递给成员方法。)



希望这会有所帮助。



祝你好运,

JK
Hi,

actually you cannot do t because the array is allocated dynamically (= you use operator new to allocate it). As a result you have to remember the length of the array in a variable (such as size that you have) and you need to pass this size as a parameter to all methods/functions that need to be aware the size.

(If you are using this code in method of a class, you can store the size of the array in a member variable and so avoid the need to pass it to the member methods as a parameter.)

Hope this helps.

Best regards,
J.K.


基本上有两种技术:



  • (正如 Kucera Jan 所建议的那样)制作 size 参数的副本,并在需要时使用它。
There are basically two techniques, either:

  • (As already suggested by Kucera Jan) make a copy of the size parameter and use it when needed.
  • sentinel 元素添加到数组中,即 NULL 数组末尾的指针。
  • Add a sentinel element to the array, namely a NULL pointer at the end of the array.


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

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