pointers

When should I use the new keyword in C++?

I've been using C++ for a short while, and I've been wondering about the new keyword. Simply, should I be using it, or not? 1) With the new keyword... MyClass* myClass = new MyClass(); myClass->MyField = "Hello world!"; 2) Without the new keyword... MyClass myClass; myClass.MyField = "Hello world!"; From an implementation perspect...

c++: when to use pointers?

After reading some tutorials I came to the conclusion that one should always use pointers for objects. But I have also seen a few exceptions while reading some QT tutorials (http://zetcode.com/tutorials/qt4tutorial/painting/) where QPaint object is created on the stack. So now I am confused. When should I use pointers? ...

How can I use pointers to display strings in an array?

I am practicing using pointers. I have a pointer to an array of 3 strings: "dog", "cat", "rat". I can print the contents using a for loop and using an array. However, I am having problems printing them using pointer arithmetic. I would like to increment the pointer to the next element in the array. However, all it does is print the d...

should we teach pointers in a "fundamentals of programming" course?

I will be teaching a course on the fundamentals of programming next Fall, first year computer science course. What are the pros and cons of teaching pointers in such a course? (My position: they should be taught). Edit: My problem with the "cater your audience" argument is that in the first couple of years in University, we (profs) do n...

Pointer vs array in C, non-trivial difference

I thought I really understood this, and re-reading the standard (ISO 9899:1990) just confirms my obviously wrong understanding, so now I ask here. The following program crashes: #include <stdio.h> #include <stddef.h> typedef struct { int array[3]; } type1_t; typedef struct { int *ptr; } type2_t; type1_t my_test = { {1, 2, 3}...

Error in returning a pointer from a function that points to an array

Greetings Everyone, I'm in a bit of a fiddle in that I dont know why my code brings up the following error when compiling: 1>..\SA.cpp(81) : error C2664: 'CFE' : cannot convert parameter 1 from 'int' to 'int []' 1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast Esse...

copying strings into a buffer

Hello, I have some code that stack dumps when using sprintf to copy a a pointer to strings. I am trying to copy the contents of animals into a new pointer array called output. However, I get a stack dump. What should be in the output is the following: new animal rabbit new animal horse new animal donkey An I going about this the right...

Pointer issues with basic C file I/O program

#include <stdio.h> #include <stdlib.h> int main() { FILE *fp = fopen("lr.txt", "r"); fseek(fp, 0L, SEEK_END); int size = ftell(fp); fseek(fp, 0L, SEEK_SET); char *lorem_ipsum; int i = 0; lorem_ipsum = (char*) malloc(sizeof(char) * size); while(fscanf(fp, "%s\n", lorem_ipsum) != EOF) { ...

Problem with dereference operator and functions.

I have a function A(), that returns a pointer to an object. In function B() I try to change a member of that object in the following way: void B() { ObjType o = *getObj(); o.set("abc"); } Object o is stored in an array, and when I print the value of the member, it seems nothing happened, and the member still has the old value;...

C++ "this" doesn't match object method was called on

I have come across what seems like a really annoying bug running my C++ program under Microsoft Visual C++ 2003, but it could just be something I'm doing wrong so thought I'd throw it out here and see if anybody has any ideas. I have a hierarchy of classes like this (exactly as is - e.g. there is no multiple inheritance in the real code...

Mixed indexing behaviour for typedef array?

I have a typedef: typedef unsigned char MyType[2]; I pass it to a function and the result is FAIL! void f(MyType * m) { *m[0] = 0x55; *m[1] = 0x66; } void main(void) { Mytype a; a[0] = 0x45; a[1] = 0x89; f(&a); } The manipulation of variable a in main() works on 1 byte indexing, so a is equal to {0x45, 0x89}. However in function...

C++ functions: ampersand vs asterisk

Let's say you have a function that modifies a variable. Should you write it like this: void myfunc(int *a) or like this void myfunc(int &a)? The former forces you to call the function with myfunc(&b) so the caller is aware that b will be modified, but the latter is shorter and can be called simply with myfunc(b). So which is better to ...

When teaching C, is it better to teach arrays before or after pointers?

For those of you with curriculum development experience: what is the best strategy regarding arrays? I have seen some schools that teach arrays after variables and control structures, often before even teaching functions. This allows teaching of some rudimentary algorithms, etc. However, it then brings the problem of how to pass arrays ...

C++: Pointer to class data member

I came across this strange code snippet which compiles fine: class Car { public: int speed; }; int main() { int Car::*pSpeed = &Car::speed; return 0; } Why does C++ have this pointer to a non-static data member of a class? What is the use of this strange pointer in real code? ...

Is it possible to create method call dispatcher in C++ ?

Consider a following code: struct X { void MethodX() { ... } }; struct Y { void MethodY() { ... } }; void test () { X x; Y y; Dispatcher d; d.Register("x", x, &X::MethodX); d.Register("y", y, &Y::MethodY); d.Call("x"); d.Call("y"); } The question is how to implement Dispatcher. I don't mind X and Y may ...

developed a strtok alternative.

Hello, I have developed my own version of strtok. Just to practice the use of pointers. Can anyone see any limitations with this or anyway I can improve. void stvstrtok(const char *source, char *dest, const char token) { /* Search for the token. */ int i = 0; while(*source) { *dest++ = *source++; if(*source ...

problems Wrapping Patricia Tries using Swig, python

Hello all! I'm trying to wrap the Patricia Tries (Perl's NET::Patricia) to be exposed in python. I am having difficulty with one of the classes. So instances the patricia node (below) as viewed from python have a "data" property. Reading it goes fine, but writing to it breaks. typedef struct _patricia_node_t { u_int bit; /* ...

Member pointer to array element

It's possible to define a pointer to a member and using this later on: struct foo { int a; int b[2]; }; int main() { foo bar; int foo::* aptr=&foo::a; bar.a=1; std::cout << bar.*aptr << std::endl; } Now I need to have a pointer to a specific element of an array, so normally I'd write int foo::* bptr=&(foo::b[0]); However, ...

Best Practice for Scoped Reference Idiom?

I just got burned by a bug that is partially due to my lack of understanding, and partially due to what I think is suboptimal design in our codebase. I'm curious as to how my 5-minute solution can be improved. We're using ref-counted objects, where we have AddRef() and Release() on objects of these classes. One particular object is de...

Accessing structure members with a pointer

Hello, I'm trying to translate the following code from C++ to C# ` struct tPacket { WORD size; WORD opcode; BYTE securityCount; BYTE securityCRC; } ... static char data[8192] = {0}; tPacket * packet = (tPacket *)data;` so far I've come up with: C# public struct tPacket { public ushort size; public ushort opcode; public byte sec...