|
I am not able to understand the output of below lines.
As per my understanding output should be 8 because we are subtracting int address which is having 8 byte diffrence but output is coming 2. Please expalin.
int a[]={5,10,15,20};
int d = &a[2]-&a[0];
|
|
|
|
|
|
See this;
&a[2] - &a[0]
(a + 2) - (a + 0)
a + 2 - a - 0
2 - 0
2
the above is true because use pointers you will always get the difference of elements
You talk about Being HUMAN. I have it in my name
AnsHUMAN
|
|
|
|
|
_AnsHUMAN_ wrote: a + 2 - a - 0
2 - 0 While your end result is correct, shouldn't that be:
a + 2 - a + 0
2 + 0
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
If the address of (the first item in) a is, for example, 0x1000 and each item in a is 4 bytes, then the difference between the third item (0x1008) and the first item would be 8 divided by the size of an int (most likely 4), thus 2 would be the output.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
You are correct, the differene in the addresses is 8. I can only assume the compiler is dividing that by the type of dm which being an int, makes the result 2.
Did you try it with char d = &a[2] - &a[0] ?
It wold be interesting.
IMO the compiler shouldnt do this, it is unexpected. I would expect to get 8 back, after all, that is the difference in the pointers.
==============================
Nothing to say.
|
|
|
|
|
Erudite_Eric wrote: IMO the compiler shouldnt do this, it is unexpected.
Quite the opposite, the compilers must do that, because it is a C programming language rule (you may find it, for instance, in the B&K book).
Veni, vidi, vici.
|
|
|
|
|
Erudite_Eric wrote: You are correct, the differene in the addresses is 8. On the contrary he is totally wrong. The difference is the count of elements in the array, between the first and last item. This is C language, not machine code, as designed by Kernighan & Ritchie.
Use the best guess
|
|
|
|
|
What happens if you do the following?
int d = ((void*)&a[2])-((void*)&a[0]);
|
|
|
|
|
You can subtract two pointers of the same base type, and the result is the number of elements of the base type that fit between the addresses which the pointers hold. Likewise, you can add an integer value to a pointer, and that will result in the address of a data type instance so and so many steps above the original pointer.
Confusing? - Take your example:
int a[]={5,10,15,20};
Assume a starts at address 0x00040000. This is also the address of a[0] . In C, a is the same as &a[0] . a+1 is &a[1] or 0x00040004, and a+2 is &a[2] or 0x00040008.
&a[2]-2 is &a[0] or 0x00040000.
When x-y==z is true, then x-z==y should also be true; when &a[2]-2==&a[0] is true, then &a[2]-&a[0]==2 should also be true. The C language is nice enough to fulfil this.
To calculate the number of bytes between two memory locations, convert the pointers to BYTE* like this:
(BYTE*)&a[2]-(BYTE*)&a[0]
This would evaluate to 8, as you expected. Assuming, of course, that 'int' is a 32-bit integer type.
|
|
|
|
|
Hi,
i am not able to get Date value from ADSI directory
VARIANT var;
hr = ADsOpenObject(path,NULL,NULL,0, IID_IADsUser, id**)&pUsr);
HRESULT h1= pUsr->Get(L"pwdLastSet", &var);
INT8 bb=var.date;
This code is not working pwdLastSet type is integer8 .it stores date value how can i get date value from adsi with c++ code please help anyone
|
|
|
|
|
Check your return codes to see if any function calls have failed and find out why. Single stepping through your code with the debugger should quickly show what is happening.
Use the best guess
|
|
|
|
|
Hi Richard,
Thanks for reply .The method is return success var.date value returns 2.613867737909e-316#DEN .
i am not able to understand this value.
|
|
|
|
|
What does the documentation say about the format of this value? Your original sample code stated that it is an INT8 , but you are displaying it as (a very small) floating point number.
Use the best guess
|
|
|
|
|
hello Friends
I am accessing USB Devices through SetupDiCalls. I am able to get USb printers'Device Path to communicate with them and its working fine. I want to know that How can I access their name also using SetuDiCalls so that I can differentiate between USB Printer Devices to with whom I have to communicate.
I tried EnumPrinters to get Printers name but that doesn't provide device Path for which we needed to pass to createFile fn for communication with USB Printer. And Nothing else I found common between ENumprinter returns or setupDicalls so that I will access from their.
Any Ideas ?
Thanks & Regards
Y
|
|
|
|
|
When already using Setup API functions, call one that accepts a pointer to a SP_DEVINFO_DATA structure (e.g. SetupDiGetDeviceInterfaceDetail() or SetupDiEnumDeviceInfo() ). Then call SetupDiGetDeviceRegistryProperty() passing the structure to get the SPDRP_FRIENDLYNAME property which contains the name as shown by Windows. If that fails, you can fallback to get the SPDRP_DEVICEDESC property.
|
|
|
|
|
Thanks For your Reply.
I tried Friendlyname,it is returning NULL. then I tried DeviceDesc , it is returning
"USB PRINTING SUPPORT " for each USB connected Printer.
I tried by passing SPDRP_MFG even, it is returning Microsoft.
Any other way to get name so, I can differentiate.
Thanks & Regards
Y
|
|
|
|
|
It seems that you have enumerated the generic Microsoft USB printing support devices. You should look up the specific printers instead which usually provide a friendly name. If that did not provide the device path, you must perform two enumerations and compare common data (e.g. the hardware ID).
|
|
|
|
|
Ahh, But I used Class ID
#define CLSID_STR_WEIUSB (L"{28D78FAD-5A12-11D1-AE5B-0000F803A8C2}") to get List of USB Printer Devices.
I tried one another too #define CLSID_STR_WEIUSB1 (L"{A5DCBF10-6530-11D2-901F-00C04FB951ED}")
but this one was not showing some of printers while Enumerating.
Give me some Idea.How can I look up specific printers only and their Friendlynames?
Regards
Y
|
|
|
|
|
Those IDs seem to belong to specific devices.
It is often difficult to get the proper GUID. See System-Defined Device Setup Classes[^] for a list of GUIDs. You may try the Printers GUID from that list and if that does not find all try also the USB Devices GUID.
|
|
|
|
|
I tried system Defined specific printer GUID and USB GUID too.
Printer = {4d36e979-e325-11ce-bfc1-08002be10318}
USB = {88BAE032-5A81-49f0-BC3D-A4FF138216D6}
but in this case it is not providing any device while enumerating.
Y
|
|
|
|
|
|
I am using it like this
#define CLSID_STR_WEIUSB1 (L"{A5DCBF10-6530-11D2-901F-00C04FB951ED}")
GUID DevClass;
CLSIDFromString(CLSID_STR_WEIUSB1, &DevClass);
SetupDiGetClassDevs(
&DevClass, NULL, NULL, DIGCF_DEVICEINTERFACE + DIGCF_PRESENT
|
|
|
|
|
I found another fn but I don't know it is useful or am I using it Wrong ?
SetupDiOpenDeviceInterfaceRegKey
Passing SP_DEVICE_INTERFACE_DATA DevNode which we Get from SetupDiEnumDeviceInterfaces
HKEY hKey = SetupDiOpenDeviceInterfaceRegKey(hDevInfoSet,&DevNode,0,KEY_READ);
RegQueryValueEx(hKey,_T("Port"),NULL,NULL,(LPBYTE)buf,&dwBufSize);
But When I tried buf is coming NULL always.
Is it Right way to find from resitry?
Regards
Y
|
|
|
|
|
The GUID you are using is GUID_DEVINTERFACE_USB_DEVICE[^]. You should use this or a similar name when not using the predefined one. Using this GUID, you are enumerating the raw USB devices which may not provide the user friendly name.
You also may have a look at the USBView sample application[^].
To check if there is any entry for your printers with the user friendly name, enumerate all devices (GUID NULL, DIGCF_ALLCLASSES ) and compare the hardware ID or other properties with those found enumerating the raw devices.
|
|
|
|