473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

simple prob passing a reference of arrays to a func.

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

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

I tried outputting the cMovesDone using cout<<. It's displaying wierd
characters. what's wrong?
Feb 19 '08 #1
3 1191
MC felon wrote:
void DoThang (char (&cMovesDone)[9])
{
for(int i = 0; i<9; i++)
cMovesDone[i] = '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.

--
Ian Collins.
Feb 19 '08 #2
The snippet you posted won't compile.

--
Ian Collins.
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[i] = '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?
Feb 19 '08 #3
On 19 Feb, 11:10, MC felon <paec....@gmail.comwrote:
The snippet you posted won't compile.
--
Ian Collins.

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[i] = '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.
Feb 19 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: J. Campbell | last post by:
Suppose I have an object that is concatinatable (sp) using the '+' operator (eg string). I have a function(func)that will do some work to the object and return an int. For several reasons it is...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
26
by: Dave Hammond | last post by:
In document "A.html" I have defined a function and within the document body have included an IFRAME element who's source is document "B.html". In document "B.html" I am trying to call the function...
3
by: Thomas Christmann | last post by:
Hi! Sorry for the weird topic, I don't know how to describe it better... I have a little problem here I can't wrap my mind around. If I do: ------------------------------------- #define DWORD...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
4
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
15
by: the consiglieri | last post by:
hello there .. im not much an expert in C....im trying to use int** for reperesentation of matrices(as double subscripted arrays .)but at run time an error occurs ..saying ...general exception...
10
by: Olaf Dietrich | last post by:
I may just be temporarily (hopefully ...) stupid, but how can I pass a function pointer between functions using an array of (signed/unsigned) chars (in a standard-conforming way)? E.g.: I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.