Reputation: 3
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";
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>
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.
Corrected the above code for typo error
Here are my comments on some of the suggestions given to me so far:
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.
Upvotes: 0
Views: 381
Reputation: 21089
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" };
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_preprocessor#User-defined_compilation_errors_and_warnings
Upvotes: 1
Reputation: 2876
Furthermore, your initialization as given is totally wrong. You have:
char* LowerList[10];
char* LowerList[0]="abc";
char* LowerList[1]="def";
char* LowerList[2]="hij";
What you want is:
char* LowerList[10];
LowerList[0]="abc";
LowerList[1]="def";
LowerList[2]="hij";
or the alternative that JAB has. The issue with your version is that you are essentially declaring LowerList 4 times, but you can't do that.
Upvotes: 1