simple prob passing a reference of arrays to a func.

M

MC felon

void DoThang (char (&cMovesDone)[9])
{
for(int i = 0; i<9; i++)
cMovesDone = 'k';
}

int main()
{
char c;
DoThang (c);
cout<<c;
}

I tried outputting the cMovesDone using cout<<. It's displaying wierd
characters. what's wrong?
 
I

Ian Collins

MC said:
void DoThang (char (&cMovesDone)[9])
{
for(int i = 0; i<9; i++)
cMovesDone = 'k';
}

int main()
{
char c;
DoThang (c);
cout<<c;
}

I tried outputting the cMovesDone using cout<<. It's displaying wierd
characters. what's wrong?


The snippet you posted won't compile.
 
M

MC felon

The snippet you posted won't compile.

Sorry!
That piece didn't initialize an array of chars.
it's basically this:

#include <max.h>
#include <dpl.h>

void DoThang (char (&cMovesDone)[9])
{
for(int i = 0; i<9; i++)
cMovesDone = 'k';
}

int main()
{
dpl_DosGraphixEnable();
char c[9] = "aaaaaaaa";
DoThang (c);
cout<<c;
}

max.h and dpl.h are my own headers. They do little things like make a
dos app look neater by putting up nice little lines and such
decorations.
Ignore them. The problem is when i compile the code above (bereft of
dpl_DosGraphixEnable()), i get an output of kkkkkkkkkr and some really
wierd symbols like a small smiley and lines. I mean there are already
9 k's there already, where did the r and the other stuff come from?
what should i do?
 
T

Triple-DES

The snippet you posted won't compile.

Sorry!
That piece didn't initialize an array of chars.
it's basically this:

#include <max.h>
#include <dpl.h>

void DoThang (char (&cMovesDone)[9])
{
 for(int i = 0; i<9; i++)
 cMovesDone = 'k';

}

int main()
{
 dpl_DosGraphixEnable();
 char c[9] = "aaaaaaaa";
 DoThang (c);
 cout<<c;

}

max.h and dpl.h are my own headers. They do little things like make a
dos app look neater by putting up nice little lines and such
decorations.
Ignore them. The problem is when i compile the code above (bereft of
dpl_DosGraphixEnable()), i get an output of kkkkkkkkkr and some really
wierd symbols like a small smiley and lines. I mean there are already
9 k's there already, where did the r and the other stuff come from?
what should i do?


The problem is that you are missing a \0 character at the end of the
char array. so operator<< will happily keep printing until it reaches
a \0 byte in memory. (Actually it's undefined behaviour, but this is
likely what you're seeing).

An easy fix would be to make the array one character larger and put a
'\0' there. Also consider using an std::string or std::vector instead
of an array.
 
M

MC felon

First off, Thanks for the prompt replies.
The problem is that you are missing a \0 character at the end of the
char array. so operator<< will happily keep printing until it reaches
a \0 byte in memory. (Actually it's undefined behaviour, but this is
likely what you're seeing).
An easy fix would be to make the array one character larger and put a
'\0' there. Also consider using an std::string or std::vector instead
of an array.

Thanks for that. I had forgotten about the infamous "Terminator"
problem.
I've substituted it with a string (std::). Works just fine!
Thank you!
 
V

Vidar Hasfjord

char c[9] = "aaaaaaaa";
DoThang (c);
cout<<c;

The subtle underlying issue is that ostream does not know how to
output your array type. Due to the C++ language rules the char array
decays to a char pointer before it is passed to the operator (<<). The
overload for char pointers expects a null-terminated string.

You could define/use a fixed-length non-terminated string type with
its own ostream operator overload, but std::string is usually the
better choice.

Regards,
Vidar Hasfjord
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top