Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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:
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
It should work the same for null characters. Don't forget that the null character is 0.
 

kpua

macrumors 6502
Jul 25, 2006
294
0
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...
 

jcnewton

macrumors newbie
Feb 18, 2007
2
0
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.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
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.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
This code isn't too specific b/c it's in the context of the rest but here it is anyway.

Code:
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.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
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.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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

Try removing the -1.
 

darkwing

macrumors 65816
Jan 6, 2004
1,210
0
Try removing the -1.

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().
 

dcr

macrumors member
Jun 10, 2002
57
0
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'...

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.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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.

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
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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.

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.
 

fishkorp

macrumors 68030
Apr 10, 2006
2,536
650
Ellicott City, MD
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.
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
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.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
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.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
Do any other languages apart from C++ use cout and << ?

b e n

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

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

Code:
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.

Not really. Where does "thearray" come from?
 

dcr

macrumors member
Jun 10, 2002
57
0
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

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.