获取作为输入到C传递的char *数组的大小++函数 [英] Getting the size of the char* array passed as an input to C++ function

查看:236
本文介绍了获取作为输入到C传递的char *数组的大小++函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图传递一个字符指针数组作为从C测试驱动的C ++函数(在C包装用)

I am trying to pass a character pointer array as an input from C test driver to C++ function (available in C wrapper)

char* LowerList[10];
LowerList[0]="abc";
LowerList[1]="def";
LowerList[2]="hij";

当可在C ++侧的元素显示的数值显示为

When available on the C++ side the values displayed for the the element is displayed as

char* LowerList[0]="abc";
LowerList[1]="def";
LowerList[2]="hij";
LowerList[3]=ƒì¶S<\$ UV<35?Ä@
LowerList[34]=ƒ<bad pointer>

我需要传递给我可以用已经拿到了C ++的大小数组的大小
而(LowerList [计数]),但不能这样做,因为垃圾的价值等等。
请让我知道是否有方法可以让我找到字符的正确尺寸* LowerList []通过初始化,内存分配或将其转换为矢量。

I need to get the size of the array passed on to the C++ size which I could have got using while (LowerList[count]) but unable to do so because of the junk value. Please let me know if any way I can find the correct size of the char* LowerList[] by initialising ,memory allocation or converting it to vector.

修正上述code为错字误差

Corrected the above code for typo error

下面是我的一些给我到目前为止的建议意见:

Here are my comments on some of the suggestions given to me so far:

1)传递数组的大小作为一个参数作为一个良好的我的方案。2)转换为载体的限制需要的大小可用3)哨兵值在哪个位置,我需要补充。如前所述,我试图处理这样一个场景,用户传递价值3这导致failure.I没有关于限制的方式C的用户我的C ++包装用他的C测试驱动程序的任何控制权。

1) Pass the size of the array as a parameter as well- A limitation for my scenario .2) Conversion to the vector need the size to be available 3) Sentinel value at which position I need to add . As stated earlier I am trying to handle a scenario where the user pass the value as 3 which leads to the failure.I don't have any control on restricting the way C user my C++ wrapper with his C test driver.

推荐答案

该指针 LowerList [3] LowerList [9] 都是未初始化的,如果我没有记错。你总是可以只使用NULL作为一个哨兵值,要么明确指定,或做这样的事情:

The pointers LowerList[3] through LowerList[9] are all uninitialized, if I'm not mistaken. You could always just use NULL as a sentinel value, and either assign it explicitly or do something like this:

char* LowerList[10] = { "abc", "def", "hij" };

这应该数组为NULL初始化剩下的三分球。

This should initialize the remaining pointers in the array to NULL.

在回答你的第二个评论:

In response to your second comment:

#define LOWER_LIST_SIZE 10
#define MINIMUM_SIZE 2

#if LOWER_LIST_SIZE < MINIMUM_SIZE
    #error "LOWER_LIST_SIZE must be >= MINIMUM_SIZE"
#endif

char *LowerList[LOWER_LIST_SIZE] = // etc.

或者类似的东西。

Or something like that.

http://en.wikipedia.org/wiki/C_$p$pprocessor#User-defined_compilation_errors_and_warnings

这篇关于获取作为输入到C传递的char *数组的大小++函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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