通过字符* ARR迭代[] [英] Iterate through char *arr[]

查看:104
本文介绍了通过字符* ARR迭代[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有这个问题。没有给出大小,我试图找到的sizeof(地名)时,如何遍历名字,但它给指针数组没有的大小。

 无效printNames(字符*名称[])
{
  //的sizeof(地名); //这给指针大小,所以如何遍历
}
诠释的main()
{
   字符*名称[MAX] = {
                   扎​​拉·阿里
                   雏阿里,
                   努哈·阿里
                   萨拉·阿里
   };
   printNames(地名);
}


解决方案

如此看来,有没有办法让 printNames 来自动确定数组的大小。

您有几种方式(从最高到最低preferable):


  • 使用的std ::矢量而不是数组。

  • 传递数组的大小到函数作为第二个参数。

  • 终止与定点值阵列,使函数查找了点。

I have this problem here. How to iterate names when the size is not given, I am trying to find sizeof(names), but it give the size of pointer not array.

void printNames(char *names[])
{
  //sizeof(names); //this gives pointer size so how to iterate
}
int main ()
{
   char *names[MAX] = {
                   "Zara Ali",
                   "Hina Ali",
                   "Nuha Ali",
                   "Sara Ali",
   };
   printNames(names);
}

解决方案

As things stand, there is no way for printNames to automatically determine the size of the array.

You have several options (from the most to the least preferable):

  • Use std::vector instead of an array.
  • Pass the size of the array into the function as a second argument.
  • Terminate the array with a sentinel value and make the function look for that.

这篇关于通过字符* ARR迭代[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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