pointers

char *a, *b; what type is (b-a) and how do I printf it?

{ char *a, *b; printf("%lx\n",(b-a)); } Usually works, in fact, I can't imagine it giving a warning or failing on a 32-bit or 64-bit machine. But is that the proper thing to do for ANSI C and size awareness? I'd like this code to work on every platform possible, including non-Unixes and embedded systems. ...

Do shallow copies share pointers? (C++)

I know that if I do something like this: class Obj { public: int* nine; }; Obj Obj1; //Awesome name int eight = 8; Obj1.nine = &eight; Obj Obj2 = Obj1; //Another Awesome name then Obj1's and Obj2's nines will point to the same 8, but will they share the same pointer? I.e.: int Necronine = 9; Obj1.nine = &Necronine; Obj2.nine == ...

Explain this C# code: byte* p = (byte*) (void*) Scan0;

I found the code from the net in which i cant understand this line:- byte* p = (byte*)(void*)Scan0; There Scan0 is System.IntPtr. It is code of C#.Net. Plz Explain the above line. The complete code is given below. this is code to convert a image in grayscale. public static Image GrayScale(Bitmap b) { BitmapData bmData ...

Strange performance problem.

I have a container similar to this one. template <typename Nat, typename Elt> class NatMap { public: Elt& operator[] (Nat nat) { return tab [nat.GetRaw()]; } private: Elt tab [Nat::kBound]; }; I wanted to drop the requirement for Elt to have a default constructor: template <typename Nat, typename Elt> class NatMap { pub...

C++ function parameters: use a reference or a pointer (and then dereference)?

I was given some code in which some of the parameters are pointers, and then the pointers are dereferenced to provide values. I was concerned that the pointer dereferencing would cost cycles, but after looking at a previous StackOverflow article: http://stackoverflow.com/questions/431469/how-expensive-is-it-to-dereference-a-pointer-in-c...

Why does C need arrays if it has pointers?

If we can use pointers and malloc to create and use arrays, why does the array type exist in C? Isn't it unnecessary if we can use pointers instead? Thanks. ...

c++ - convert pointer string to integer

I am trying to convert treePtr->item.getInvest() which contains a string to an integer. Is this possible? ...

char* pointer from string in C#

Is it possible to get a char* for a string variable in C#? I need to convert a path string to a char* for using some native win32 function ... ...

Please help in understanding how the pointer referencing works

I am currently reading "Developer's Workshop to COM and ATL 3.0". Chapter 3 introduces GUIDs, referencing and comparisons. Pointers are painful. I could use some help in deciphering the REFGUID #define (see below) and how memcmp in IsEqualGUID works against the pointers. Given: typedef struct_GUID{ unsigned long Data1; u...

pointer arithmetic and the C# compiler

For the purpose of learning I recently looked at an existing assembly (using Reflector) that uses Win32 WriteFile. The implementation is: Write(IntPtr handleFile, void* bufferData, uint length){ void* buffer = bufferData while (length > 0) { uint wrtn; if (!WriteFile(handle, buffer, len, out wrtn, IntPtr.Zero)) { // Do some e...

C - Pointer to int to get elements in stack

Hello! I wanted to write a standard stack in C but I am not sure if my stk_size() function could work on other platforms except for my 32bit pc. I read that its not good to cast a pointer to int. But what could be a better implementation? I dont want to add a "size"-variable because its redundant in my eyes. Here are some parts of the ...

Change string pointed by pointer

hi friends Many functions in c take pointer to constant strings/chars as parameters eg void foo(const char *ptr) . However I wish to change the string pointed by it (ptr).how to do it in c ...

Using pointers to swap int array values.

I am supposed to use pointers to swap ints in an array. It compiles with no errors or warnings and runs but does not swap the ints. Any suggestions would be helpful!!! Here is the tester: #import <stdio.h> void swap( int ary[] ); int main( int argc, char*argv[] ) { int ary[] = { 25, 50 }; printf( "The array values are: %i an...

C - Excess elements in array

Hey, I am learning C and I am playing around with pointers and arrays. I am trying to create an array of pointers with the code below: const int NUM_P = 50; // Line 10 char *pS[NUM_P] = { NULL }; // Line 11 I am getting the following warnings and errors when I compile: → gcc array.c -o array array.c: In function ‘main’: array.c:11:...

Copying some strings from pointer array in C++

I have a string pointer like below, char *str = "This is cool stuff"; Now, I've references to this string pointer like below, char* start = str + 1; char* end = str + 6; So, start and end are pointing to different locations of *str. How can I copy the string chars falls between start and end into a new string pointer. Any existing ...

Marshalling struct with embedded pointer from C# to unmanaged driver

Hi, I'm trying to interface C# (.NET Compact Framework 3.5) with a Windows CE 6 R2 stream driver using P/Invoked DeviceIoControl() calls . For one of the IOCTL codes, the driver requires a DeviceIoControl input buffer that is the following unmanaged struct that contains an embedded pointer: typedef struct { DWORD address; cons...

Explicit Address Manipulation in C++

Please check out the following func and its output void main() { Distance d1; d1.setFeet(256); d1.setInches(2.2); char *p=(char *)&d1; *p=1; cout<< d1.getFeet()<< " "<< d1.getInches()<< endl; } The class Distance gets its values thru setFeet and setInches, passing int and float arguments respectively. It dis...

Writing a binary file in C# to be read by C program, with pointers?

I'm moving some old C code that generates a binary file into our C# system. The problem is, the resulting binary file will still need to be read by another old C program. The original code outputs several structs to a binary file, and many of those structs contain linked lists, with *next pointers. How can I write these in C# so that ...

C++ vector to pointer of pointers

Is there a way to convert a vector to a pointer to a pointer (ptr-to-ptr). Background: I have an arbitrary length set of data stored in a vector. But I have a library of algorithms that accept ptr-to-ptr (for image array access). I need to get the data from my vector to a ptr-to-ptr. How is that possible? ...

Doubt regarding de-referencing structure pointers. Please explain

I am compiling this piece of code and I get compliation errors saying " dereferencing pointer to incomplete type" . I get the errors for the last print statement and before that where I try to point (*temp). num to the address of b Here's the code snippet: void main() { struct { int xx; char *y; int * nu...