1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Do Char Arrays Work The Same in Xcode as they do in Visual Studio?

Discussion in 'Mac Programming' started by bobber205, Feb 17, 2007.

  1. macrumors 68020


    I'm getting mixed results with my characters arrays. In Visual Studio 2005 when I have an element by "\0" or char null, when I print that array it does not print the rest of the elements I was "Forced" to make.

    It also makes the length shorter.

    Any comments? :confused:
  2. Moderator emeritus


    It should work the same for null characters. Don't forget that the null character is 0.
  3. macrumors 6502

    Also, I don't know if this is the problem or not, but make sure you use '\0' and not "\0"... Doing the latter is sure to give you unexpected results...
  4. macrumors newbie

    Assuming you are doing this in some C like language, the \0 character actually denotes the end of an array. For example, instantiating an array of length 3 actually reserves 4 characters in memory (the last one being an '\0'), so putting null characters in a character array will make most functions that deal with character arrays believe that the array terminates at the first occurrence of \0. The easiest way of fixing this is probably to just not use the null character, or use vectors or some other data type.
  5. macrumors 68020


    It seems Xcode's strlen() function goes or counts "something one extra" that VS does. In my output, I was always getting 1 ¿ mark in front of my output. I suspect that's the null char or something (I was going backwards through the array at the time). Subtracting one from the length solved my problem.
  6. Moderator emeritus


    It would help if you posted some code.
  7. Moderator emeritus

    How are you printing your array? It sounds as if you're not addressing the elements correctly.

    I'm not saying that Apple's gcc couldn't be wrong, but Microsoft's compilers aren't all that accurate, either.
  8. macrumors 68020


    This code isn't too specific b/c it's in the context of the rest but here it is anyway.

    void printarray(char thearray[], int base) {
    	/*below lines may need to have removed the -1 for i to work in VS */
        for (int i = (strlen(thearray) -1 ); i >= 0; i--) {
            cout << thearray[i]; //prints the array backwards!
    I hope it helps.
  9. macrumors 6502a

    Hi there,

    Don't think there is anything wrong as such with your code sample. For example printarray( "hello world!",10 ) works fine and produces what you'd expect, ie '!dlrow olleh'. I don't really understand what your problem is.

    b e n
  10. Moderator emeritus

    The code is fine for most purposes, but if you have multiple strings embedded in the array separated by NULL characters, then you need to work through the array differently. strlen() will return up to the NULL character from the point of origin. If you have multiple strings within the array, you need to increment past each string and call your function with the new address.
  11. macrumors 68020


    Try removing the -1.
  12. macrumors 65816

    If visual studio's strlen() gives you the length of the string including the null terminator then visual studio sucks and you should never use it again. Your code is correct to use with strlen().
  13. dcr
    macrumors member

    Absolutely not true. In C-derived languages, an array of 3 reserves only 3 storage locations. Read past the end and you'll get undefined behavior. Write past the end and you'll smash memory, probably other values on the stack. If you're lucky it will crash immediately so that you know there's a problem.

    You're probably thinking of string literals like "foo", which reserve 4 locations in (static) storage that look like this ['f', 'o', 'o', '\0'].

    If the behavior of your program is different between VS and XCode using arrays, you can bet with almost 100% certainty that the bug is in your code and is probably depending on the state of uninitialized memory. Pointers and arrays in C are notorious for being very confusing and the source of most newbie errors.
  14. macrumors 6502a

    I'm not suggesting that you would never come across the situation you describe, but I imagine that the most likely way that such an array would arise would be from a declaration something like this:-

    char* some_strings[] = { "1st string", "2nd string", "3rd string", "" } ;

    in which case you would access the individual strings through the array, eg

    strlen( some_strings[ 2 ] ) ;

    ie no need to start messing around searching for strings by looking for '\0' characters etc.

    b e n
  15. macrumors G5


    If there are any differences, it is most likely that (a) you used different code on both machines, or (b) your code invokes undefined behavior, so whatever output you get on any machine is pure coincidence, or (c) your code relies on implementation defined behavior which is different between the machines.

    Without copying complete compilable code and pasting it, it is impossible to know what is going on. And it would be really nice to tell us which programming language you are using.
  16. macrumors 6502a

    Do any other languages apart from C++ use cout and << ?

    b e n
  17. macrumors demi-god

    now, i haven't programming in C or C++ in a long time, and have never used Visual Studio, but looking at the way you're going through the array, you HAVE TO use the -1. Here's why. Say your array is this:

    { 'a', 'p', 'p', 'l', e'}

    The length of the array is 5, right? right. Well, array[5] is nothing (or the null terminator actually) because the first value is at position 0 (zero). So:

    array[0] = 'a';
    array[1] = 'p';
    array[2] = 'p';
    array[3] = 'l';
    array[4] = 'e';

    printing out array[5] will give you the null terminator (or an array index out of bounds exception in java). i'm guessing Visual Studio just ignores the null character and won't print it. maybe it's an OS/console specific display character, similar to '\n', '\r', and '\r\n' are. so in short, if you're going backwards, ALWAYS start at one less than the length.
  18. macrumors 603


    Yup, you have to use the "-1" as the array indices are zero based, not one based. If you have a four letter word in the array, the indices are 0..3, not 1..4.
  19. macrumors 68020


    After some testing at school, it seems that VS does not print the /0 character to the screen and Xcode does.

    That's ok. :)

    I guess I should have been doing it right from the get go.
  20. macrumors G5


    int main (void)
    int cout = 0;
    return cout << 1;

    Not really. Where does "thearray" come from?
  21. macrumors 6502a

    It would be really nice to tell us which programming language you are using ;)

    b e n
  22. dcr
    macrumors member

    Again, untrue. You're confusing null-terminated string literals with arrays. String literals are terminated by \0 by the compiler. Arrays contain what you put in them.

    Confusing char* "strings" and char arrays is the classic newbie mistake in C. in this case, array[5] returns the value one past the end of the defined storage for array[] which could be absolutely anything. If you're depending on it being a specific value, you have a bug.

Share This Page