|
* is backward compatible with C.
Edit: I was off by one character, I meant to indicate that C doesn't support references.
modified on Thursday, March 5, 2009 9:56 AM
|
|
|
|
|
You can use pointers in C too (thus using the '*' is valid in C).
|
|
|
|
|
Ah, I'm not awake yet, coffee just finished dripping.
I must have meant it the other way around. I only dabbled in C++ a little, and not recently.
|
|
|
|
|
I can't believe there're people who know C and not C++ still!
|
|
|
|
|
C++ is all hype; it's not necessary for most real work.
|
|
|
|
|
Is all the position the '*' use can replace by '&'?
|
|
|
|
|
in a declaration ie int *X means a a pointer to x named *;
You cannot define a reference with initializing it
so
int &X //doesn't work however
---
int Y;
int &x = Y; does
Y and X now refer to the same place in memory changing the value of one will change the value of the other. When you use a & in a function prototype we are saying I want my parameter to access memory at the location of the variable the caller specified; When you declare a pointer in the prototype you are going to explicitly pass in a pointer that pointer can however be reassigned but once reassinged the caller will no longer be able to read changes to what was passed in
I wrote a little example.
<br />
<br />
#include "stdafx.h"<br />
<br />
<br />
void NoSideEffect(int *T)<br />
{<br />
<br />
int G = 5;<br />
T = &G;<br />
<br />
<br />
}<br />
<br />
void SideEffect(int &T)<br />
{<br />
int G = 5;<br />
T = G;<br />
}<br />
<br />
void PointerSideEffect(int *T)<br />
{<br />
int G = 10;<br />
*T = G;<br />
}<br />
<br />
<br />
<br />
int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
char buffer[2];<br />
int X = 2;<br />
NoSideEffect(&X);<br />
printf("X is Now %d \n",X);<br />
SideEffect(X);<br />
printf("X is Now %d \n",X);<br />
PointerSideEffect(&X);<br />
printf("X is Now %d \n",X);<br />
gets(buffer);<br />
}<br />
<br />
this will print out
X is Now 2
X is Now 5
X is Now 10
a programmer traped in a thugs body
|
|
|
|
|
<br />
void NoSideEffect(int *T)<br />
{<br />
<br />
int G = 5;<br />
T = &G;<br />
<br />
<br />
}<br />
int X = 2;<br />
NoSideEffect(&X);<br />
printf("X is Now %d \n",X);<br />
X is Now 2
don't change why?could you explain more detail
|
|
|
|
|
This is because c++ accurately always passes by value When you pass a pointer you are passing the value of the pointer which is a memory address. when the compiler creates a stack frame the value of the memory address is put on the stack as a local variable when you change the value of that variable you point it to a new memory location and no longer maps to memory address of the variable in the caller. So any change will not affect the caller since you are changing the location associated with another variable in this case another local variable which is allso on the stack.
a programmer traped in a thugs body
|
|
|
|
|
using references is safer, you do not have to check for NULL pointers.
This signature was proudly tested on animals.
|
|
|
|
|
|
That's right. You cannot pass a null to a reference like you can for a pointer.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Hi All,
I m developing the sdi application.I want to handle the System menu move option.
If I press ALT+SPACE on my application and select move ..then I need the event handler..
can anybody pls help me out in this..
Thanks in Advance,
Ashok.
|
|
|
|
|
I believe this[^] is what you need.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
I am attempting to control mouse events, like moving the mouse and pressing any mouse key ...
I am using the following code to set the mouse position as (0,0)
LPINPUT i;
MOUSEINPUT mi;
mi.dx=0;
mi.dy=0;
mi.dwFlags=MOUSEEVENTF_LEFTDOWN;
mi.time=0;
mi.dwExtraInfo=GetMessageExtraInfo();
mi.mouseData=0;
i[0].mi=mi;
i[0].type=INPUT_MOUSE;
SendInput(1,i,sizeof(i));
But, I am getting an exception at the line
i[0].mi=mi;
Actually I want to control the mouse pointer through an external hardware, which will be connected at the serial port.
I will read the data from the serial port like mouse movement and any mouse key being pressed, then SET this mouse status to the system mouse ....this is my ultimate aim ...
So I wrote the above code to set the mouse position first, but its not working ...
Please someone help me here ...
Apurv
A man is but the product of his thoughts. What he thinks, he becomes.
.......Mahatma Gandhi
Be the change you want to see in the world.
.......Mahatma Gandhi
|
|
|
|
|
LPINPUT i;
LPINPUT means pointer to structure INPUT. Here i is of type LPINPUT. You have to allocate memory for this before you use this variable.
ie LPINPUT i = new INPUT;
akt
|
|
|
|
|
oh ... thanks ...
its not giving anymore errors ...
but the mouse position is not set ....
here is my updated code ...
LPINPUT i = new INPUT;
MOUSEINPUT mi;
mi.dx=0;
mi.dy=0;
mi.dwFlags=MOUSEEVENTF_MOVE;
mi.time=0;
mi.dwExtraInfo=GetMessageExtraInfo();
mi.mouseData=0;
i[0].mi=mi;
i[0].type=INPUT_MOUSE;
SendInput(1,i,sizeof(i));
Apurv
A man is but the product of his thoughts. What he thinks, he becomes.
.......Mahatma Gandhi
Be the change you want to see in the world.
.......Mahatma Gandhi
|
|
|
|
|
Try this
INPUT i;
MOUSEINPUT mi;
mi.dx=0;
mi.dy=0;
mi.dwFlags=MOUSEEVENTF_MOVE;
mi.time=0;
mi.dwExtraInfo=GetMessageExtraInfo();
mi.mouseData=0;
i.mi=mi;
i.type=INPUT_MOUSE;
SendInput(1, &i, sizeof(i));
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
its working ... superman .... thank you very much ....
will this SendInput() function work outside the application window ?
I mean, while i interface it with my hardware, so that the key inputs are coming from the hardware, know suppose the mouse is over the start button, and i send the Left-button pressed event to the SendInput() function ... will this result the Start button being pressed ???
Apurv
A man is but the product of his thoughts. What he thinks, he becomes.
.......Mahatma Gandhi
Be the change you want to see in the world.
.......Mahatma Gandhi
|
|
|
|
|
I believe that it will work where ever the mouse pointer is located.
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Please check the last parameter of SendInput();
It requires the size of structure INPUT. For this you have call sizeof(INPUT).
In your code, you have taken sizeof(i). i means LPINPUT.
As i said earlier LPINPUT is a pointer. So sizeof(i) will return the size of a pointer variable only(4 bytes).
akt
|
|
|
|
|
Hi All
I am getting assertion in altsimpstr.h file.
void AddRef() throw()
{
ATLASSERT(nRefs > 0);
_AtlInterlockedIncrement(&nRefs);
}
how can i find solution of this asserton.
Plz help me
|
|
|
|
|
Can you show your code that it products this error?
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|
|
Davitor wrote: how can i find solution of this asserton.
Use your debugger to gather as much information as possible: what is the context in your code (use the callstack), inspect the different variables just before the crash (put a breakpoint just before the call where it crashes), ...
|
|
|
|
|
Hi all,
i Have SDI type application,I m creating a Mutex to avoid multiple instance of application. and use system tray icon feature.
for creating mutex i m using this code:
HANDLE hMutex = ::CreateMutex(NULL, TRUE, _T("GlobalMainMutex"));
switch(::GetLastError())
{
case ERROR_SUCCESS:
break;
case ERROR_ALREADY_EXISTS:
return FALSE;
default:
return FALSE;
}
for system tray icon i m taking help of this article.
http://www.codeproject.com/KB/shell/systemtray.aspx[^]
My problem is that is application is already running and its in hide mode and icon present in system tray,now when i click on my exe than its not open becoz of mutex.
i what when application is already running or its hide than if i click on exe icon than the hide application can show.
please tell me how can i do this.
thanks in advance.
To accomplish great things, we must not only act, but also dream;
not only plan, but also believe.
|
|
|
|