|
No, it means that IF changeAmount was an int, then the & syntax turns it into a pointer, but that would then be an int *. A cast changes the type, in this case, to char *. That's what the & sytnax does, it returns the address of a variable. And a cast changes a variable type.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Christian Graus wrote: & turns it into a pointer
how does & turn into a pointer.........
|
|
|
|
|
even Christian already gave you a valuable answer, here is another way to understand it.
in the expression (char*)&changeAmount , there are 2 operators called :
1. unary & usually called "address of" and used to get the address of changeAmount
2. (char*) is a cast operator, which explicits the cast of an address previously got with 1. into a char*
doing this changes the type of the pointer on the base address of changeAmount, so that it can be access each byte of that variable.
|
|
|
|
|
sorry to bother u but it makes sense to say changeAmount is an integer pointer and we want to cast it to a char pointer. But in our case changeAmount is NOT an integer pointer, its rather has the address of changeAmount (&changeAmount) ..... how can this be changed into a character pointer now.
it makes sense if it was like this:
(char*) int *changeAmount
pls get me running on the right track.. haha thanks
|
|
|
|
|
The function "add" has a second parameter that appears to take bytes as its type (an array of bytes actually). The & operator takes the address of a variable (that is, gives you a pointer to it) and the (char*) casts it to a charater pointer type. What it is doing is passing in the array of bytes for that variable (the size is passed as the third argument) to do something with them directly (e.g. transmit them over the network perhaps?).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
// hey here goes the program i have made pls help me out to find errors
#include <iostream.h>
//#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <process.h>
class sudoku{
int a[9][9][10],count;
public:
sudoku();
void initsudoku();
void redprob(int , int);
void dispsudoku();
void fillele(int , int );
void fillsudoku();
} ;
sudoku::sudoku()
{ int i , j ,k;
count=81;
cout<<"constructor";
for( i=1;i<=9;i++)
for ( j=1;j<=9;j++){
a[i][j][0]=99;
for ( k=1;k<=9;k++)
a[i][j][k]=0;
a[i][j][10]=9;
}
}
void sudoku::redprob(int i, int j )
{
int k,l,temp;
temp=a[i][j][0];
for ( k=1;k<=9;k++)
{
if (( k!=j) && (a[i][k][temp]!=99));
{ a[i][k][temp] = 99;
a[i][k][10]--;
if(a[i][k][10]==1)
fillele(i,k);
}
if (( k!=i)&&(a[k][j][temp]!=99));
{ a[k][j][temp] = 99;
a[k][j][10]--;
if(a[k][j][10]==1)
fillele(k,j);
}
}
cout<<"l"<<temp;
int="" h="i/3,g=j/3;
" for(="" k="0;k<3;k++)
" {="" l="0;l<3;l++){
" if((="" i!="h*3+k)||(j!=g*3" +l))
="" a[h*3="" +k][g*3="" +l][temp]="99;
" if="" (a[h*3="" +l][10]="=1)
" fillele(h*3="" +k,g*3="" +l);
="" }
="" }
="" }
="" void="" sudoku::initsudoku()
="" {
="" i,j,val;
="" char="" ch="y" ;
="" while(ch="='y')
" {
="" cout<<"enter="" the="" element="" ";
="" element\nrow="" no.";
="" cin="">>i;
cout<<"column no.";
cin>>j;
cout<<"enter value";
cin>>val;
if ((i<=9)&&(j<=9)&&(a[i][j][val]==0)&&(count<=0))
{
a[i][j][0]=val;
a[i][j][10]=0;
count--;
redprob(i,j);
}
else if (i>9)
cout<<"wrong row value";
else if (j>9)
cout<<"wrong column value";
else if (count<=0)
cout<<"already 81 elements have been identified";
else cout<<" you can not place this value here";
cout<<"enter any other element ";
cin>>ch;
}
}
void sudoku::fillele(int i, int j )
{ for( int k=1; k<=9;k++)
if( a[i][j][k] == 0){
a[i][j][0] = k;
a[i][j][10]=0;
count--;
redprob(i,j);
}
}
void sudoku::dispsudoku()
{ int i,j;
for( i=1;i<=9;i++)
{
for ( j=1;j<=9;j++){
if(a[j][i][0]>10){
cout<<" ";
}
else {
cout<
|
|
|
|
|
keshava shukla wrote: pls help me out to find errors
It's actually up to you to find the error(s). Narrow the problem down to just a few lines of code, and then you're bound to get lots of help. No one is going to wade through a bunch of code to find something that you should be finding.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Even if you posted this properly ( checked 'ignore HTML tags' ), no-one is going to fix your program for you. Work out which bit is not doing what you expect, post a relevant code snippet and ask specific questions.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Because VC8 don't alows to intit array of objects this way:
CElement elemArr[]={ {1, 2}, {3, 4}, {5, 6} };
I decided to convert it to that way, but I feel it not relible
CElement elemArr[]={ CElement(1, 2), CElement(3, 4), CElement(5, 6) };
Is it possible that elements of array will be self destroyed before elemArr?
class CElement
{
public:
CElement(int nA){m_nA = nA;};
CElement(int nA, int nB){m_nA = nA;m_nB = nB;};
virtual ~CElement(){m_nA = 0;m_nB = 1;};
virtual int Method(){m_nA = -1;m_nB = -3;return 0;};
protected:
int m_nA;
int m_nB;
};
|
|
|
|
|
definitely
a()
{
throw 0
}
try
{
a b
}
catch
{}
Kuphryn
|
|
|
|
|
No I talk only with situation with array, NOT just single object
void f(int nI)
{
CElement elemArr[]={ CElement(1, 2), CElement(3, 4), CElement(5, 6) };
int nJ = nI;
nJ++;
//Question elements off array could possible be destroyed right after declaration line?
//What is the scope/lifetime of my elements?
}
|
|
|
|
|
elements inside array are present
Kuphryn
|
|
|
|
|
Could you explain for how long?
Show line where distructor of CElement will be called?
void f(int nI)
{
CElement elemArr[]={ CElement(1, 2), CElement(3, 4), CElement(5, 6) }; //1
int nJ = nI; //2
nJ++; //3
}//4
I have guess it is ither 2 or 4?
But I want to know motivated answer.
|
|
|
|
|
Ok here is answer for myself
CElement elemArr[]={ CElement(1, 2), CElement(3, 4), CElement(5, 6) };
void OnBnClickedOk()
{
elemArr[1].Method();
elemArr[2] = CElement(7, 8); // here memcopy happens
//here destructor called of local element
}
//here dtor of global element called
|
|
|
|
|
you can't create a CElement simply with {2, 4} for instance, because it is not a struct ... moreover, the members m_na and m_nb are protected and then, can't be accessed from outside the class.
|
|
|
|
|
Are you kidding? You are, right? Why not code I posted works? Class, struct you can use either. Only in 2005 agregatable type like:
CFoo oFooV[] = {{...},{...}, {...}};
can't be declared if CFoo has contructor, virtual function or base class the rest is OK.
And never worry about private or protected members just take "this" pointer and add offset to access to the nesesarry field, and use it. Private, protected is only for compilers and school teachers, they like those restrictions. ))
|
|
|
|
|
Alex_Y wrote: Class, struct you can use either. Only in 2005 agregatable type like:
CFoo oFooV[] = {{...},{...}, {...}};
It worked on older versions of Microsoft's compiler because they didn't meet the standard for this particular case. You cannot set the values of protected/private members directly like this (there are ways to do it, but if you do them, you will quickly be beaten with a stick in any code-review).
Alex_Y wrote: And never worry about private or protected members just take "this" pointer and add offset to access to the nesesarry field, and use it. Private, protected is only for compilers and school teachers, they like those restrictions.
I do hope you are joking. If you truly believe this, never apply for any position if you see I'm the hiring manager.
To answer your original question, when you initialize the array in this manner:
CElement elements[] = { CElement(1, 2), CElement(2, 3), ...};
the elements in the array invoke the copy-assignment operator for each element. Thus, the elements themselves will be destroyed, but the values they hold will be copied into your array. For simple cases like this (where your data is just primitive types), you can get away without creating a copy constructor/copy-assignment operator; however, if you had a pointer to heap memory that your class was controlling, only the pointer value would be copied (and since the data in the original would be cleaned up, the pointer would be looking to a place in memory that was no longer allocated). You should get in the habit of creating copy constructors and copy-assignment operators (even if you mark them as private so they can't be invoked).
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
virtual void empty() = 0; // fucntion header
Why is this function header assigned a value of zero?
Is this suppose to mean its a pure virtual function from a derived class with no implementation in it?
like this:
virtual void empty()
{
// empty
}
Thanks,
Jay
|
|
|
|
|
|
Which functions usually have an empty implementation.... is it the Base Class or the Derived Class?
|
|
|
|
|
Base
class CBase //abstract class
{
virtual int Pure(int nA) = 0;
}
class CDerive : public CBase
{
virtual Pure(int nA)
{
return nA+1;
}
}
|
|
|
|
|
Yes, it means it's a pure virtual function with no implementation. That's not the same as a function whose implementation has an empty body.
|
|
|
|
|
Hello , i want to load the .BMP File of Image File in CImageList from my Hard drive , but when i load it to CImageList and reads and copy the Bitmap to a CBitmap Object the program crashes on the Run , can anyone help me in this , i tried to load image list this way -->>:
////////////////////////////////////////////////////
////////////////////////////////////////////
////////////////////////////////////
BOOL CXXXDlg::OnInitDialog()
{
..................
..........
// CImageList to load bitmap from Outside
CImageList m_imgList;
m_imgList.Create("C:\\xxxx.bmp" , 64 , 32, RGB(255,0,255));
//Copies the Single Frame from the Image List to CBitmap
CBitmap mBmp;
GetImageFromList(&m_imgList , 1 , &mBmp);
and
CBitmap *mBmp = new CBitmap;
GetImageFromList(&m_imgList , 1 , mBmp);
...........
.................
}
void CXXXDlg::GetImageFromList(CImageList *lstImages, int nImage,
CBitmap *destBitmap)
{
//First we want to create a temporary image list we can manipulate
CImageList tmpList;
tmpList.Create(lstImages);
//Then swap the requested image to the first spot in the list
tmpList.Copy( 0, nImage, ILCF_SWAP );
//Now we need to get som information about the image
IMAGEINFO lastImage;
tmpList.GetImageInfo(0,&lastImage);
//Heres where it gets fun
//Create a Compatible Device Context using
//the valid DC of your calling window
CDC dcMem; dcMem.CreateCompatibleDC (GetWindowDC());
//This rect simply stored the size of the image we need
CRect rect (lastImage.rcImage);
//Using the bitmap passed in, Create a bitmap
//compatible with the window DC
//We also know that the bitmap needs to be a certain size.
destBitmap->CreateCompatibleBitmap (this->GetWindowDC(),
rect.Width (), rect.Height ());
//Select the new destination bitmap into the DC we created above
CBitmap* pBmpOld = dcMem.SelectObject (destBitmap);
//This call apparently "draws" the bitmap from the list,
//onto the new destination bitmap
tmpList.DrawIndirect (&dcMem, 0, CPoint (0, 0),
CSize (rect.Width (), rect.Height ()), CPoint (0, 0));
//cleanup by reselecting the old bitmap object into the DC
dcMem.SelectObject (pBmpOld);
}
/////////////////////////////////////////////////////////
///////////////////////////////////////////////
/////////////////////////////////////////
and when i run this APP, its crashes ; please tell me whts wrong with the CODE, and tell me how to load Bitmap from the Hard Drive i realyy need it
Thnx...
-- modified at 14:02 Thursday 27th July, 2006
|
|
|
|
|
Do you need to load bitmap?
|
|
|
|
|
does anyone know if a MAKEDWORD macro exsists or a macro that does something similar to a MAKEWORD macro except you take 2 WORDS and make them into a DWORD?
Or am I stuck using bitwise manipulations to put the 2 WORDS together....
Thanks,
Kitty5
|
|
|
|
|