www.digitalmars.com         C & C++   DMDScript  

D - Bug?: Well, find it

reply "Lars Ivar Igesund" <larsivi stud.ntnu.no> writes:
Look at the following program and output (given an input argument).

char [][] strings;

void main(char[][] args)
{
  strings.length = 3;

  strings[0] = "55";
  strings[1] = args[1];
  strings[2] = "teststring";

  printf(strings[0] ~ "\n");

  printf("\n1 --\n");

  printf(strings[1] ~ " " ~ "\n");

  printf("\n2 --\n");

  printf(strings[1] ~ " " ~ " " ~ "\n");

  printf("\n3 --\n");

  printf(strings[1] ~ " " ~ " " ~ " " ~ "\n");

  printf("\n4 --\n");
  printf(strings[0] ~ "\n");
  printf(strings[1] ~ " " ~ " " ~ "\n");

  printf("\n5 --\n");

  printf(strings[2] ~ "\n");
  printf(strings[1] ~ " " ~ " " ~ "\n");
}

OUTPUT:


C:\projects\code\foo>chararrtest2.exe chararrtest.d
55

1 --
chararrtest.d

2 --
chararrtest.d
chararrtest.d

3 --
chararrtest.d

4 --
55
chararrtest.d
55

5 --
teststring
chararrtest.d
teststring


What's happening? Well first off, as long as strings[1] isn't assigned the
input
argument, nothing strange happens. Also when printf'ing just the string
literal
entries: nothing strange. Just printf'ing the args entry of strings; nothing
strange
happens. But when the printf takes the following as input:
strings[1] ~ " " ~ " " ~ "\n"
then the previous entry in strings printed using printf is printed on the
next line.
If the printf input is one of these:
strings[1] ~ " " ~ "\n"
strings[1] ~ " " ~ "" ~ "\n"
strings[1] ~ " " ~ " "
strings[1] ~ " " ~ " " ~ " " ~ "\n"
then nothing strange happens. Having those two spaces interspersed between
other entries from strings also leads to the same symptoms. Is this a C
string
problem somehow? Must I format my printfs better? In any case; this is an
incredibly hard to debug bug. Please help.

Lars Ivar Igesund

Is this a C strings case
Oct 29 2003
parent reply "Walter" <walter digitalmars.com> writes:
The problem you're having is that the first argument to printf() must be a
zero-terminated string. char[] strings in D are not 0 terminated unless you
specifically append a 0.
Oct 30 2003
parent "Lars Ivar Igesund" <larsivi stud.ntnu.no> writes:
*Blush*
Thanks

Lars Ivar Igesund

"Walter" <walter digitalmars.com> wrote in message
news:bnru8u$25sm$3 digitaldaemon.com...
 The problem you're having is that the first argument to printf() must be a
 zero-terminated string. char[] strings in D are not 0 terminated unless

 specifically append a 0.

Oct 30 2003