pointers

Dynamic creation of a pointer function in c++

I was working on my advanced calculus homework today and we're doing some iteration methods along the lines of newton's method to find solutions to things like x^2=2. It got me thinking that I could write a function that would take two function pointers, one to the function itself and one to the derivative and automate the process. This ...

c incompatible types in assignment, problem with pointers?

Hi I'm working with C and I have a question about assigning pointers. struct foo { int _bar; char * _car[SOME_NUMBER]; // this is meant to be an array of char * so that it can hold pointers to names of cars } int foofunc (void * arg) { int bar; char * car[SOME_NUMBER]; struct foo * thing = (struct foo *) arg; bar =...

An array of LPWSTR pointers, not working right.

Declare: LPWSTR** lines= new LPWSTR*[totalLines]; then i set using: lines[totalLines]=&totalText; SetWindowText(totalChat,(LPWSTR)lines[totalLines]); totalLines++; Now I know totalText is right, cause if i SetWindowText using totalText it works fine. I need the text in totalLines too. I'm also doing: //accolating more memory. ...

Better variant of getting the output dynamically-allocated array from the function?

Here is two variants. First: int n = 42; int* some_function(int* input) { int* result = new int[n]; // some code return result; } int main() { int* input = new int[n]; int* output = some_function(input); delete[] input; delete[] output; return 0; } Here the function returns the memory, allocated insi...

C++ vector pointer/reference problem

Please take a look at this example: #include <iostream> #include <vector> #include <string> using namespace std; class mySubContainer { public: string val; }; class myMainContainer { public: mySubContainer sub; }; void doSomethingWith( myMainContainer &container ) { container.sub.val = "I was modified"; } int main( ) { ...

How to get a float value the pointer points to?

Hello, In my app, i've created the TList type list where i store the pointers to 1 string and 2 float(real) values for every 3 items. aList.Add(@sName); //string aList.Add(@x1); //float aList.Add(@x2); //float Then, i want to get the values out from the list, but i could only do that for string sStr := string(lList.items[i]); But ...

How to tell where I am in an array with pointer arithmetic?

In C, I have declared a memory area like this: int cells = 512; int* memory = (int*) malloc ((sizeof (int)) * cells); And I place myself more or less in the middle int* current_cell = memory + ((cells / 2) * sizeof (int)); My question is, while I increment *current_cell, how do I know if I reached the end of the allocated memory ar...

Pointers in C with binary file

Hi All, I am reading the contents of the file using fread into an char array. But I am not sure why it is not getting printed in the output. Here is the code: void getInfo(FILE* inputFile) { char chunk[4]; int liIndex; for (liIndex = 0 ; liIndex < 4 ; liIndex++) { fread(chunk, sizeof(char), 4, inputFile); } prin...

Does (size_t)((char *)0) ever not evaluate to 0?

According to the responses in "Why subtract null pointer in offsetof()?" (and my reading of K&R), the C standard doesn't require that (size_t)((char *)0) == 0. Still, I've never seen a situation where casting a null pointer to an integer type evaluates to anything else. If there is a compiler or scenario where (size_t)((char *)0) != 0, ...

Dereference a pointer inside a structure pointer

I have a structure: struct mystruct { int* pointer; }; structure mystruct* struct_inst; Now I want to change the value pointed to by struct_inst->pointer. How can I do that? EDIT I didn't write it, but pointer already points to an area of memory allocated with malloc. ...

C++ function pointer as parameter

Hello, I try to call a function which passed as function pointer with no argument, but I can't make it work. void *disconnectFunc; void D::setDisconnectFunc(void (*func)){ disconnectFunc = func; } void D::disconnected(){ *disconnectFunc; connected = false; } ...

What is the Meaning of wild pointer in C ?

Hi,can anybody tell me,the meaning of wild pointer in C, how it obtain and is this available in C++ ? ...

Passing array of pointers to another class

Hi, I am trying to do the following: in main.cpp: // Create an array of pointers to Block objects Block *blk[64]; for (i=0; i<8; i++) { for (j=0; j<8; j++) { int x_low = i*80; int y_low = j*45; blk[j*8+i] = new Block(30, x_low+40.0f, y_low+7.5f, &b); } } And then I am trying to pass it to the graphics object I have cr...

C++ pointer to functions, Beginner Question...

Hi all, I want to ask about pointer in C++ I have some simple code: int add(int a, int b){ return a+b; } int runner(int x,int y, int (*functocall)(int, int)){ return (*functocall)(x,y); } now, suppose I call those functions using this way : cout<<runner(2,5,&add); or maybe cout<<runner(2,5,add); is there any difference? beca...

C String input confusion

C really isn't my strong point and after reading 3 chapters of a book on the subject and spending ages trying to get stuff working it just doesn't: #include <stdio.h> char *a,*b; int main( ) { char input[10]; fgets(input,sizeof input, stdin); a = input; fgets(input,sizeof input, stdin); b = input; printf("%...

C# Implementing a custom stream writer-esque class

How would I go about writing my own stream manipulator class? Basically what I'm trying to wrap my head around is storing the reference to the underlying stream in the writer. For example, when writing to a memory stream using a StreamWriter, when a Write() is made, the underlying memory stream is written to. Can I store the referenc...

Problem using void pointer as a function argument

Hi, I can't understand this result... The code: void foo(void * key, size_t key_sz) { HashItem *item = malloc(sizeof(HashItem)); printf("[%d]\n", (int)key); ... item->key = malloc(key_sz); memcpy(item->key, key, key_sz); } void bar(int num) { foo(&num, sizeof(int)); } And I do this call: bar(900011009); ...

compilation error: request member in something not a structure of union

Hi everybody, I'm having the above error request member rv in something not a structure of union. I've googled it and several answers told me it's when working with a pointer but tries to access it as a struct, where I should be using -> instead of . int foo(void * arg, struct message * msg) { struct fd_info * info = (struct som...

Function pointer demo

Hi, Check the below code int add(int a, int b) { return a + b; } void functionptrdemo() { typedef int *(funcPtr) (int,int); funcPtr ptr; ptr = add; //IS THIS CORRECT? int p = (*ptr)(2,3); cout<<"Addition value is "<<p<<endl; } In the place where I try to assign a function to function ptr with same function si...

What is the point of function pointers?

Hi, I have trouble seing the utility of the function pointers. I guess it may be useful in some cases (they exist, after all), but I can't think of a case where it's better or unavoidable to use a function pointer. Could you give some example of good use of function pointers (in C or C++)? Many thanks :) ...