pointers

Trim function in C, to trim in place (without returning the string)

I can't figure out what to do to make this work. Here's my code: char* testStr = " trim this "; char** pTestStr = &testStr; trim(pTestStr); int trim(char** pStr) { char* str = *pStr; while(isspace(*str)) { (*pStr)++; str++; } if(*str == 0) { return 0; } char *end = str + strlen(str) - 1; while(end > s...

Arrays & Pointers

Hi, Looking for some help with arrays and pointers and explanation of what I am trying to do. I want to create a new array on the heap of type Foo* so that I may later assign objects that have been created else where to this array. I am having troubles understanding what I am creating exactly when I do something like the following. Foo...

Confusion about pointers and their memory addresses

alright, im looking at a code here and the idea is difficult to understand. #include <iostream> using namespace std; class Point { public : int X,Y; Point() : X(0), Y(0) {} }; void MoveUp (Point * p) { p -> Y += 5; } int main() { Point point; MoveUp(&point); cout << point.X << point.Y; return 0; } Alrigh...

What's the difference between void* and void**?

It's the special property that void* can also be assigned a pointer to a pointer and cast back and the original value is received. I read this line somewhere. Does it means void* and void** are same? What is the difference? Edit void* can hold any pointer. Then what's void** needed for? ...

What does the C standard say about pointers to structs and their first member?

Consider the following two struct: struct a { int a; }; struct b { struct a a_struct; int b; }; the following instantiation of struct b: struct b b_struct; and this condition: if (&b_struct == (struct b*)&b_struct.a_struct) printf("Yes\n"); Does the C standard mandate this to always evaluate true? ...

Non binary tree solving in C

i have to create a program which can have n number of nodes and each subnode can have n number of subnodes and so on, its not a binary tree. i need to know how can one create it? ...

How does compiler understand the pointer type?

How c++ compiler understands the pointer type? As I know pointer has a size equal to WORD of the OS (32 or 64). So does it store some info in that 32(or 64) bits about type? Just because you can not have a pointer on one type and assign to that pointer another pointer with a different type. ...

Convert Decimal to ASCII

I'm having difficulty using reinterpret_cast. Lets just say right off the bat that I'm not married ot reinterpret_cast. Feel free to suggest major changes. Before I show you my code I'll let you know what I'm trying to do. I'm trying to get a filename from a vector full of data being used by a MIPS I processor I designed. Basically w...

Problem with Command Pattern under Visual Studio 2008 (C++)

Dear All, I've a problem with this pattern under c++ on VS 2008. The same code has been tested in gcc (linux, mac and mingw for widnows) and it works. I copy/paste the code here: class MyCommand { public: virtual void execute() = 0; virtual ~MyCommand () {}; }; class MyOperation { public: virtual void DoIt() {}; //I also...

How to const declare the this pointer sent as parameter

Hi, I want to const declare the this pointer received as an argument. static void Class::func(const OtherClass *otherClass) { // use otherClass pointer to read, but not write to it. } It is being called like this: void OtherClass::func() { Class::func(this); } This does not compile nad if i dont const declare the OtherCla...

pointer is always byte aligned

Hi, I read something like pointer must be byte-aligned. My understanding in a typical 32bit architecture... all pointers are byte aligned...No ? Please confirm. can there be a pointer which is not byte-aligned ? Basically this is mentioned in a hardware reference manual for tx descriptor memory. ...

Segmentation fault on writing char to char* address

Hi guys, I've got problem with my little C program. Maybe you could help me. char* shiftujVzorku(char* text, char* pattern, int offset){ char* pom = text; int size = 0; int index = 0; while(*(text + size) != '\0'){ size++; } while(*(pom + index) != '\0'){ if(overVzorku(pom + index, pattern)){ ...

non-scalar type requested

can somebody please help me with an error conversion from `A' to non-scalar type `B' requested I have class A and derived from it B, but I have problems with these rows: A a(1); A *pb = new B(a); B b = *pb; //here I have an error thanks in advance for any help class A { protected: int player; public: A(int initPlayer =...

How can I pass a const array or a variable array to a function in C?

I have a simple function Bar that uses a set of values from a data set that is passed in in the form of an Array of data structures. The data can come from two sources: a constant initialized array of default values, or a dynamically updated cache. The calling function determines which data is used and should be passed to Bar. Bar does...

Object allocation in C++

char *myfunc() { char *temp = "string"; return temp; } In this piece of code, where does the allocation of the object pointed to by temp happen and what would be its scope? Is this function a valid way to return a char* pointer? ...

pointer to const member function typedef

I know it's possible to separate to create a pointer to member function like this struct K { void func() {} }; typedef void FuncType(); typedef FuncType K::* MemFuncType; MemFuncType pF = &K::func; Is there similar way to construct a pointer to a const function? I've tried adding const in various places with no success. I've played ...

Detect pointer arithmetics because of LARGEADDRESSAWARE

I would like to switch my application to LARGEADDRESSAWARE. One of issues to watch for is pointer arithmetic, as pointer difference can no longer be represented as signed 32b. Is there some way how to find automatically all instances of pointer subtraction in a large C++ project? If not, is there some "least effort" manual or semi-auto...

Misaligned Pointer Performance

Aren't misaligned pointers (in the BEST possible case) supposed to slow down performance and in the worst case crash your program (assuming the compiler was nice enough to compile your invalid c program). Well, the following code doesn't seem to have any performance differences between the aligned and misaligned versions. Why is that? ...

C++: Trouble with Pointers, loop variables, and structs

Consider the following example: #include <iostream> #include <sstream> #include <vector> #include <wchar.h> #include <stdlib.h> using namespace std; struct odp { int f; wchar_t* pstr; }; int main() { vector<odp> vec; ostringstream ss; wchar_t base[5]; wcscpy_s(base, L"1234"); for (int i = 0; i < 4; i++)...

how to get the type of a deferred template parameter

Is there a way to get the defered type of a class template parameter ? template <class TPtr> struct foo { typedef TPtr ptr_type; typedef ??? element_type; /* shall be the type of a deferred TPtr*/ }; so foo<const char*>::element_type results in const char, and foo<std::vector<int>::iterator_type>::element_type results in int. ...