pointers

how to grow a dynamic array of char pointers (in C++)?

I am trying to work with an array of char pointers. Let's say I dynamically declare such an array like so: int numrows=100; char** array = new char*[numrows]; And then I populate it by using getline to get strings from a file, converting the strings to char arrays, then setting a pointer in my array to point to said char array l...

Some problem using pointers to enter a string.

Hi all , I'm a beginner and i need to ask a question.. I wrote this small code that accepts a string from the user and prints it..very simple. #include <iostream> using namespace std; int main() { int i; char *p = new char[1]; for(i = 0 ; *(p+i) ; i++) *(p+i) = getchar(); *(p+i) = 0; for(i = 0 ; *(p+i) ; i++) ...

Simple Linked List Implementation Problem - C++

Hi, I'm a programming student in my first C++ class, and recently we covered linked lists, and we were given an assignment to implement a simple one. I have coded everything but my pop_back() function, which is supossed to return a pointer to the Node that needs to be deleted in Main(). No Node deletion is to be done in the actual funct...

How to get a pointer out of scope

This is not exactly my code but it looks a lot like it I have 2 tables of type CTable. They work in the same way as normal arrays but can be allocated dynamically and resized. So lets just pretend that they are arrays right now One table has object references MyObj obj1; MyObj obj2; MyObj table1[10]; table1[0] = obj1; table1[1] = obj...

Logic Issues - Help needed

I am trying to remove spaces from a string in c, not from the end, nor the beginning, just multiple spaces in a string for example hello everyone this is a test has 2 spaces between hello and everyone, and five spaces from this to is. Ultimatley i would want to remove 1 space from the 2 and 4 from the 5, so every gap has 1 space...

Pointer allocation vs normal declaration.

Hi, sometimes I see in various C++ programs, objects declared and used like so: object *obj = new object; obj->action(); obj->moreAction(); //etc... Is there any benefit of doing that, instead of simply doing: object obj; obj.action(); obj.moreAction(); //etc ...

Remove redundant whitespace from string (in-place)

Ok so i posted earlier about trying to (without any prebuilt functions) remove additional spaces so "this is <insert many spaces!> a test" would return "this is a test" http://stackoverflow.com/questions/2279679/logic-issues-help-needed As it was homework i asked for no full solutions and some kind people provided me with the fol...

Approved syntax for raw pointer manipulation

I am making a memory block copy routine and need to deal with blocks of raw memory in efficient chunks. My question is not about the specialized copy routine I'm making, but in how to correctly examine raw pointer alignment in C. I have a raw pointer of memory, let's say it's already cast as a non-null char *. In my architecture, I can ...

2 quick questions about passing by const pointer/const reference

1) Can someone explain the following? void OnCreate(HWND hWnd, const LPCREATESTRUCT lpCreateStruct) { lpCreateStruct->x = 2; // this compiles } void OnCreate(HWND hWnd, const CREATESTRUCT * lpCreateStruct) { lpCreateStruct->x = 2; // this does not compile } 2) Is it faster to pass by pointer or by reference? Or the same? ...

objective c reassign point

I cant wrap my head around this for some reason... I have a class (Model) with an ivar (value). I want to pass the value to a method and change the value ivar to point to something else. This changes the aValue to point to newString. So, how do I get the model.value to point to newString with out saying model.value = newString? (void)a...

C++ Swapping an array of integers passed through an int& parameter

Hi, I need to swap a couple of integers in the format int i[2] using a void swap(int& x) function. As you see the function takes an argument of type int&. Here is non-working version of the function: int i[2] = {3, 7}; void swap (int& x) { int temp; temp = x[1]; x[1] = x[0]; x[0] = temp; } int main() { cout << i[0...

Passing Arguments wrongly? C Question

When my TimerExpire function is finally called when the timer ticks out, it prints out gibberish. Anyone know why? But my printk function in IOCTL_MAKE_TIMER prints out correctly, so I think it's because I'm passing in the data wrong. setup_timer() works by setting up the timer in the first argument, telling it to call the function spec...

Array of classes. Stack or heap?

class temp; temp *t; void foo() { temp foo2; t[1] = foo2; } int main() { t = new temp[100]; foo(); //t[1] is still in memory? } If i want an array of classes like this, am i going to have to use pointer to pointer? (and use 'new' on each element in the array) E.G: temp **t; if i want to make an array of 100 ptr to ptr...

Passing a struct pointer as a parameter in C#

Hello all, I have a function in C++ that I exported to a DLL. I contains a struct pointer as one of the parameters. I need to use this function in C#, so I used DLLImport for the function and recreated the struct in C# using StructLayout. I've tried passing in the parameter using ref as well as tried Marshaling it in using MarshalAs(U...

Flaw in the strcpy sample program

I am using the following code to copy the string contents to another string. Two logics are used ,One with while loop(commented) is working and the other is not (as=at). Please help me to identify the flaw in this code. Thanks in advance #include<stdio.h> #include<conio.h> main() { char *s="SourceString"; char *t="TargetStri...

C# deallocate memory referenced by IntPtr

I am using some unmanaged code that is returning pointers (IntPtr) to large image objects. I use the references but after I am finished with the images, I need to free that memory referenced by the pointers. Currently, the only thing that frees the memory is to shutdown my entire app. I need to be able to free that memory from inside ...

ALLOCATABLE arrays or POINTER arrays?

Hello, I am writing a new code in Fortran and hesitating between using allocatable arrays or pointer arrays. I read somewhere that allocatable arrays have significant advantages over pointer arrays: 1) More efficient because they are always contiguous in memory 2) No memory leaks are possible Can someone confirm this? Which one wou...

Difference between pointers to variables, and pointers to structures in C

In learning C, I've just begun studying pointers to structures and have some questions. Suppose I were to create a structure named myStructure, and then create a pointer myStructurePointer, pointing to myStructure. Is *myStructurePointer, and myStructure two ways of referencing the same thing? If so, why is it necessary to have the ...

Problem with method in a class when called from another class

Hi, I'm writing a simple scene graph to hold some objects so I can control the rendering and drawing of these objects in OpenGL. I have two classes, one called GameObject which defines the objects with certain parameters such as position and velocity as well as how to draw the object. It has a method called update() which is used to ca...

Taking user input with pointers

I'm trying to get better at using pointers, and not using array notation. So I have a function to read user input and return a pointer to that array. I can do it like this and it seems to work ok: float *GetValues(float *p, size_t n) { float input; int i = 0; if ((newPtr = (float *)malloc(n * sizeof(float))) == NULL) ...