Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.29/5 (3 votes)
See more:
So, I'm using the following method to print each element in an array:

C++
char* a[] = {"this","is","a","test","string"};
int i = 2;
while(a[i])
{
    std::cout << a[i - 2] << std::endl;
    i++;
}


Now, the reason I implement the '-2' is because when I didn't include the '-2', each element would print, but the last two lines would be some ascii unicode gibberish stuff. Could anyone explain what that gibberish is and why my '-2' is neccessary? My code works I'm just interested in knowing why it works. Thanks!
Posted
Comments
CHill60 26-Mar-13 10:46am    
while(a[i]) ... you're running off the end of your array. Try ending the array with a null
Sergey Alexandrovich Kryukov 26-Mar-13 10:51am    
The whole idea is bad. The array length should be known prior running the loop...
—SA
Sergey Alexandrovich Kryukov 26-Mar-13 11:05am    
I tried to explain the behavior; please see my answer.
You comment is of course credited.
—SA
CHill60 26-Mar-13 11:07am    
Thank you
Sergey Alexandrovich Kryukov 26-Mar-13 10:49am    
There is no such thing as "Unicode gibberish" :-)
—SA

You should write:
C++
char* a[] = {"this","is","a","test","string", 0};

otherwise your while loop is going to run off the end of the pointer array and it is pure luck what it sees there.

Or you use a for loop like this:
C++
#define COUNT(x) (sizeof(x)/sizeof(x[0]))
    ...
    for (int i=0; i < COUNT(a); ++i)
        ...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Mar-13 11:04am    
This will certainly work, but the whole idea of null-termination is really bad; and OP asked only for the explanation ;-)
My 5 anyway.
Please see my answer where I tried to provide the explanation.
—SA
nv3 26-Mar-13 12:51pm    
Thanks, Sergey. I would not go so far as to say the null-termination in general is a bad concept. In the given case I would prefer the for-loop with a well-defined count. But if RadXPictures has to live with a crooked interface, null-termination may be his only real choice.
Sergey Alexandrovich Kryukov 26-Mar-13 13:05pm    
Basically, I agree, but null could be considered fine only in some special or simple cases. I still would prefer to avoid null termination if possible, due to intrinsic uncertainty which exists before reading/copying. For example, if you need to read some data into some buffer, you cannot know in advance, without additional operations and the use of CPU time, the buffer size your would need to allocate, as well as other similar things.
—SA
RadXPictures 26-Mar-13 11:17am    
I should have mentioned that, as strange as this code is, I cannot use (sizeof(x)/sizeof(x[0])) because I am trying to determine the length of an array that acts as a argument to a function. Doing so returns the static size of a generic char* array and will not display the actual size of the array because it is passed at runtime, and the code determines the size before the parameter is passed to the function. I tried everything, and this seemed to be the only working solution. If anyone can contribute a better idea, I would be happy to hear it. Thanks for your help.
nv3 26-Mar-13 11:38am    
So I understand that your function has a char** pStrings argument. In that case you should either pass the number of string in a separate parameter or request the caller to append a NULL-pointer to the end of the list as shown above. -- You are welcome!
First of all, there is no such thing as "Unicode gibberish". Unicode is not involved at all (I have an impression that you don't have a clue on Unicode, which is no good. :-))

The code is exactly as weird as explainable. No, it is not "working", too. Your result is unpredictable.

Let's number the elements of your array:
C#
char* a[] = {
    "this",            // 0
    "is",              // 1
    "a",               // 2
    "test",            // 3
    "string"           // 4
    // something else  // 5 you always have something past the last element, does not have to be null
    // something else
    // ...
    };


You start with 2, which is "a", but in print actually shift -2, so you print "this" and then increment. By some random reason, you get a null element past the array and your loop stops. As CHill60 correctly explained, nothing guarantees that your have null (0) anywhere past the array data. Some other data goes there. In other cases (for example, in different configuration of the build), you can get an exception, such as General Protection Fault.

I'm not writing the "correct" code because 1) you did not ask about it; 2) because you did not explain what should be the expected result.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900