pointers

Why the swap program which collects addresses in pointers to pointers did not work ?

I have a program below void swap(char **s1,char **s2); int main() { char *list[] = { "Das", "Kannan", "Rajendran", "Shahul" }; printf("Before swap list[0] = %s,list[1] = %s\n",*list[0],*list[1]); swap(&list[0],&list[1]); printf("After swap list[0] = %s,list[1] = %s\n",*list[0],*list[1]); retur...

unary pointer increment in function call vs increment before/after function call

I am trying to understand a code, here is fragment which is causing confusion: typedef map<int, Person, less<int> > people_map; people_map people; . . . cout << "Erasing people of age 100" << endl; for (people_map::iterator j = people.begin(); j != people.end();) { if (j->second.GetAge() == 100) { peopl...

how can this happen?

struct a { public int x; } struct c { public byte x; } unsafe class Program { static void Main(string[] args) { a V = new a(); V.x = 1; c* b = (c*)(void*)&V; for(int n = 0;n != 300;n++) b->x++; Console.WriteLine(V.x); Console.ReadKey(true); } } 45 it really returns 45. wh...

String Manipulation in C Question

Possible Duplicate: Why does simple C code receive segmentation fault? Hey Everyone, I'm sure this is a very basic question, but apparently I'm not quite understanding something here. I've been playing around with C a lot over winter break and just came across something that I thought would work but is giving me a segmentation ...

How to read string from pointer to buffer in C#

Hi, How can I read the error string in C# from this C++ dll call? // // PARAMETERS: // objptr // Pointer to class instance. // // pBuffer // Pointer to buffer receiving NULL terminated error message string. // If this value is zero, the function returns the required buffer size, in bytes, // and makes no use of the pB...

Pointers, arrays and passing pointers to methods

Confused with the problem here. New to C, as made obvious by the below example: #include <stdlib.h> #include <stdio.h> void pass_char_ref(unsigned char*); int main() { unsigned char bar[6]; pass_char_ref(&bar); printf("str: %s", bar); return 0; } void pass_char_ref(unsigned char *foo) { foo = "hello"; } To my understan...

Can I treat a struct like an array?

I have a struct for holding a 4D vector struct { float x; float y; float z; float w; } vector4f And I'm using a library that has some functions that operate on vectors but take float pointers as their arguments. Is it legal to call something like doSomethingWithVectors( (float *) &myVector)? ...

How can I avoid creating new wrapper objects all over the place?

I have various classes that wrap an IntPtr. They don't store their own data (other than the pointer), but instead use properties and methods to expose the data at the pointer using an unmanaged library. It works well, but I've gotten to the point where I need to be able to refer to these wrapper objects from other wrapper objects. For ex...

Switch pointers in a function in the C programming language

How do you switch pointers in a function? void ChangePointers(int *p_intP1, int *p_intP2); int main() { int i = 100, j = 500; int *intP1, *intP2; /* pointers */ intP1 = &i; intP2 = &j; printf("%d\n", *intP1); /* prints 100 (i) */ printf("%d\n", *intP2); /* prints 500 (j) */ ChangePointers(intP1, intP2); printf("%d\n", *intP1); /* ...

Is using "operator &" on a reference a portable C++ construct?

Suppose I have: void function1( Type* object ); //whatever implementation void function2( Type& object ) { function1( &object ); } supposing Type doesn't have an overloaded operator &() will this construct - using operator & on a reference - obtain the actual address of the object (variable of Type type) on all decently standard-c...

pointer to pointer problem

Hi, I was studying trees and everything seem fine until i started a avl tree, which requires rotation. I built a rotation algorithm that works fine until the 2 or 3 rotation, the algorithm it's the following: static void _nodeRotateRight(avl_tree* t, avl_tree_node** n) { avl_tree_node* node = (*n)->left; // refresh parents before rota...

memory overhead of pointers in c/c++

I'm on a 64bit platform, so all memory adrs are 8 bytes. So to get an estimate of the memory usage of an array, should I add 8 bytes to the sizeof(DATATYPE) for each entry in the array. Example: short unsigned int *ary = new short unsigned int[1000000]; //length 1mio //sizeof(short unsinged int) = 2bytes //sizeof(short unsinged int*)...

stuck at defining static pointer and using it

hey there I stuck at defining a static pointer inside one of my class which is pointing to another class here is the schema of what I've done : #busineslogic.h class BussinesLogic { private : static Samenamespace::otherclass_DataLogic::clsDL *DL; }; #busineslogic.cpp samenamespace { businessnamespace{ clsBL{ Samenamespace::bu...

C++/SDL 'void*' is not a point-to-object type

Hello there, I'm new on C++ and I'm trying to make some testing with C++ and SDL and in SDL we have a function: SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param); which I can pass a callback for the timer created. But apparently it converts my instance this to *void so I can't retrieve it again on...

Casting between integers and pointers in C++

#include<iostream> using namespace std; int main() { int *p,*c; p=(int*)10; c=(int*)20; cout<<(int)p<<(int)c; } Somebody asked me "What is wrong with the above code?" and I couldn't figure it out. Someone please help me. ...

getint and getch

"As written, getint treats a + or - not followed by a digit as a valid representation of zero. Fix it to push such a character back on the input." Ok, well this is the original version: int getint2(int *pn) { int c, sign; while(isspace(c=getch())) ; if(!isdigit(c) && c!= EOF && c!= '+' && c!= '-') { ung...

Cast native pointer to a C++\CLI manged object reference?

hi, I have a callback that is called through a delegate. Inside it I will need to treat the buffer data that arrive from a record procedure. Normaly in a unmanaged context I could do a reinterpret_cast on dwParam1 to get the object reference. But in a manged context how can I cast a DWORD_PTR to a managed object ref? static void Wav...

Pointers or references for dynamically allocated members that always exist?

I have a class CContainer that has some members CMemberX, CMemberY, which are independent of each other and other CClientA, CClientB classes that use CContainer. #include "MemberX.h" #include "MemberY.h" class CContainer { public: CMemberX & GetX() const { return m_x; } CMemberY & GetY() const { return m_y; } private: CMem...

PostgreSQL notice processing

Hi, I'm working on a C application that is suppose to talk to PostgreSQL. Right now I need to handle notices and warnings sent by the server but I'm at a loss on how to make it work. The (very unclear) documentation says we should use PQsetNoticeReceiver to set a method as the receiver of notifications, as the default receiver just for...

Array of pointers member, is it initialized?

If I have a class A { private: Widget* widgets[5]; }; Is it guaranteed that all pointers are NULL, or do I need to initialize them in the constructor? Is it true for all compilers? Thanks. ...