pointers

What is the best way to create a sub array from an exisiting array in C++?

OK, I am trying to get a sub array from an existing array and I'm just not sure how to do it. In my example I have a very large array, but I want to create an array from the last 5 elements of the array. An example of what I am talking about would be: int array1 = {1,2,3,...99,100}; int array2[5] = array1+95; I know this isn't correc...

Swapping elements in an array of structs

Say I have this struct: struct MyStruct { int iID; int iMyNumber; }; Then I define an array of MyStructs: struct MyStruct msTest[3]; I'm doing a sorting operation on a struct similar to this one by looking at the ID. Now, as soon as I find out which records should be swapped to sort the array I have to do the actual swapping. I...

Adding item to list in C++

Hi, I am using two classes in my C++ application. The code is as follows: class MyMessageBox { public: void sendMessage(Message *msg, User *recvr); Message receiveMessage(); list<Message> dataMessageList; }; class User { public: MyMessageBox *dataMsgBox; }; The msg is a pointer to a derived class object of Message cl...

How to pass objects to functions in C++?

I am new to C++ programming, but I have experience in Java. I need guidance on how to pass objects to functions in C++. Do I need to pass pointers, references, or non-pointer and non-reference values? I remember in Java there are no such issues since we pass just the variable that holds reference to the objects. It would be great if yo...

What does this short snippet of C code do

Could someone explain what this code does size = *(int *)data; // size of string plus header word off = (size + 3) & ~3; chan = *(int *)(data + off); data[size] = '\0'; // zero terminate I think it's got something to do with making the data a multiple of 4? ...

Need to write to a string constant, how do I work around this?

I have the following code: int main() { char *sPPhrase[51]; /* Input */ printf("Enter string (max. 50 chars):\n"); fflush(stdout); /* Works around an annoying Eclipse bug that fails to display the output from the printf command */ scanf("%s", *sPPhrase); /* Won't work */ /* More code g...

What to replace in this C Puzzle ?

Now this is a silly puzzle I got from some exam paper,sadly I am unable to figure it out from last 15 minutes. #include <stdio.h> int main(void){ /* <something> */ putchar(*(wer[1]+1)); return 0; } What should we replace in place of something in order to get the output e.Now we know putchar takes a int as argumen...

Using pointer to base class as array parameter

Hey, I have 2 classes: class Base { public: virtual int Foo(int n); virtual void Goo() = 0; virtual ~Base() ; }; class Derived : public Base { public: int Add4Bytes; void Goo(); int Foo(int n); }; int Test(Base* b) { for (int i=0;i<5;++i) { b->Foo(i); ++b; } re...

Common Uses For Pointers?

Hi, I'm a programming student with two classes in C#, but I'm just taking my first class in C++, and thus I'm being exposed to pointers. I know how they work, and the proper way to use them, but I wondered about some of the ways that professional programmers use pointers in their programs. So how do you use pointers? Or do you? Thi...

Difference between pointer in C++ and reference type in C#

In C++ a pointer is a pointer to an address of memory where another variable is stored and in C# a reference is some how same. What is the difference between these two? ...

pointer matrix get dereferenced in the middle of the code

Hi all! Banging my head for 1 hour, can't find the answer: int *get_possible_moves(int **board, int *w, int *h, int *x, int *y) { int movespossible=0; int moveslist[2][8]; //4 if(x + 1 <= w-1 && y + 2 <= h - 1) { if(board[*(int *)y + 2][*(int *)x + 1] == 0) { moveslist[movespossible][0] = *(int *)x + 1; breakpoint-> mo...

Need some help understanding pointers and memory in C

I'm writing a bit of code for a class, but since I have no experience in C I'm a bit unsure of what the code I've written actually does. Particularly what the memory looks like. Here's the relevant bits: typedef struct listnode *Node; typedef struct listnode { void *data; Node next; Node previous; } Listnode; typedef struct...

Passing argument from incompatible pointer type warning

I've been trying to figure out pointers in C most of today, even asked a question earlier, but now I'm stuck on something else. I've got the following code: typedef struct listnode *Node; typedef struct listnode { void *data; Node next; Node previous; } Listnode; typedef struct listhead *LIST; typedef struct listhead { ...

Ignoring leading whitespace with character array in C

I'm using the fgets function to read a line from a file. I don't understand how to ignore the leading whitespace of a file though. I thought if I had a char array I could check the first value to see if it is whitespace with isspace, and then increment to the next value. My code looks like: while (!feof(fp)) { fgets(str, LINE...

How can I display variable strings using C++ and PDCurses?

I'm extremely sorry to post such an embarrassingly newbish question, but I haven't mucked around much with C++ since my college days and I think at some point I drank all that I knew about pointers and C++ strings right out of my head. Basically, I'm creating a C++ console app (a roguelike, to be precise) with PDCurses to handle output....

Pointers, Arrays, printf

I'm trying to use an array to hold inputs for a survey that will have equal positive values on each side, but have a pointer point to the center of the array so negative pointer values can be used to access the array. For example, the array would hold values from 0 to 30, the pointer would point to 15, and the user would be prompted t...

How can I initialize a static pointer in C?

I want to use a static pointer in a function in order to point to a number of integers. The number of integers is not yet known while programming but it is known on runtime before the function is used first. So I want to give the function a parameter n and tell it to allocate memory space for n integers to the pointer and keep this. Howe...

Create a File in C# and pass it as an IntPtr to a COM object

I'm working with some COM objects in C#, one of them has a function called SetLogFile that takes an IntPtr. How can I create the file in C# then pass it as an IntPtr to that COM function? EDIT: The function requires an open file handle: http://msdn.microsoft.com/en-us/library/aa915939.aspx ...

C: Missing some logic with the pointers stuff...

I am writing my own string copy function. The following works: char *src, *dest; src = (char *) malloc(BUFFSIZE); //Do something to fill the src dest = (char *) malloc(strlen(src) + 1); mystringcpy(src, dest); void mystringcopy(char *src, char *dest) { for(; (*dest = *src) != '\0'; ++src, +dest); } But this doesn't work: char *sr...

Passing getter function, or tricky pointer in AS3

So I know as3 doesn't have pointers, but I thought there might be a way to use object variables as pointers. Right now I'm relying on passing a function that gets the get function, it's not a very elegant solution something like this: myvar = function():int{return objectName.getVarValue}; The other option would be to change all the g...