pointers

Using a variable to represent a function in C

This has the functionality I want (and it works) #include <stdio.h> //includes other libraries needed int foo(); int main() { while(true) { while(foo()==1) { //do something } //does other unrelated things } } int foo() { // Returns if a switch is on or off when the function was called...

C - If/Else and Pointers Returning Wrong String

We have a function longest, which returns the longest substring that consists of letters. Example: longest("112****hel 5454lllllo454") would return: lllllo However, when I run the program it seems to return lllllo454. Here is the function: char *longest(char *s){ char *pMax = NULL; int nMax = 0; char *p = NULL; int n...

Access a function pointer without parenthesis

I have this code: #include <stdio.h> int getAns(void); int num; int main() { int (*current_ans)(void); current_ans = &getAns; // HERE printf("%d", current_ans()); } int getAns() { return num + 3; } However, is it possible to have something in the // HERE spot that allows the next line to be printf("%d", curre...

How do you program safely outside of a managed code environment?

If you are someone who programs in C or C++, without the managed-language benefits of memory management, type checking or buffer overrun protection, using pointer arithmetic, how do you make sure that your programs are safe? Do you use a lot of unit tests, or are you just a cautious coder? Do you have other methods? ...

Why are pointers and recursion looked upon as an complicated issues ?

Hi, Recently I was reading about Article on Interviewing an Software Engineering Position by Joel and he mentioned about asking candidate about Recursion and Pointer's after some simple puzzles. I wonder why Pointers and Recursion are considered to be complicated Issues ? Update: What can be done to improve on Pointers and Recursion...

delphi - how to use declare and use pointer to the const array in a const record?

Hi, I have a few const arrays of the same base type but different sizes, and I need to point to them in the const records. The code below compiles successfully, but finishes with error. type Toffsets = array of integer; Trec = record point1: Tpoint; //complete size point2: Tpoint; aOf...

What are some techniques to safely handle both 32bit and 64bit pointers without a conditional macro?

I'm updating some code in a library because i've found out my original code assumed 32bit pointers. I have my own thinkings on how to fix it, but for thoroughness, I want to ask what are some techniques you would use to make this code work for both 32bit and 64bit without a macro? char *argList = (char *)malloc(sizeof(id *) * arguments...

What does (int **array;) create?

I want to know what is happening in memory when you declare: int **array; ...

How to dereference a pointer passed by reference in c++?

I'm doing (something like) this: void insert(Node*& node, string val, Node* parent) { if (node == NULL) instantiateNode(node, val, parent); else insert(node->child, val, node); } The thing is, that instantiateNode(..., parent) seems to modify the original *&node passed into the function when setting the *parent. instan...

Java references values are addresses values?

When I do: int x[] = new int[2]; System.out.println("...> " + x); the output value is like this: [I@1b67f74 so that hex number is concerning to the memory address where the object has been allocated? and [I what does it meaning? ...

Is it possible for slicing to occur with Smart Pointers?

If I understand slicing correctly I don't think this could happen with pointers or smart pointers. For example, if you had: class A { int something; }; class B : public A { int stuff; int morestuff; }; int main() { std::shared_ptr<B> b(new B()); std::shared_ptr<A> a; a = b; } My understanding is that the block of memory ...

C typedef of pointer to structure

I had come across the following code: typedef struct { double x; double y; double z; } *vector Is this a valid type definition? The code compiles and runs fine. I was just curious if this is common practice. ...

pointers and references question

#ifndef DELETE #define DELETE(var) delete var, var = NULL #endif using namespace std; class Teste { private: Teste *_Z; public: Teste(){ AnyNum = 5; _Z = NULL; } ~Teste(){ if (_Z != NULL) DELETE(_Z); } Teste *Z(){ _Z = new Teste; return _Z; } void Z(Teste *va...

Pointer to class method

Hello! I'm trying to have pointer to class methods, so I have something like: class foo { public: static void bar() { } }; void (foo::*bar)() = &foo::bar; That doesn't compile :( I get: > error: cannot convert ‘void (*)()’ to > ‘void (foo::*)()’ in > initialization ...

What's the difference between *d++ and (*d)++ in C?

as in the title, what's the difference because these two seem to get me the same results? ...

How to cast an int's address to char pointer in C?

Hi all, Currently the below code gives me a warning when i try to compile it: int z; char *w; w = How can i cast &z properly so that w stores the pointer to z's address? ...

What does this notation mean in C?

int *w; int **d; d = &w; What does the **d store exactly? ...

How to use string and string pointers in C++

I am very confused about when to use string (char) and when to use string pointers (char pointers) in C++. Here are two questions I'm having. which one of the following two is correct? string subString; subString = anotherString.sub(9); string *subString; subString = &anotherString.sub(9); which one of the following two is correct?...

Why is this string changed?

I have the following code, so far, I want to check if a file name is already in the linked list fileList (or flist). according to the output, the string saved in the first Node was changed somewhere in Node* getFileName(Node *&flist) How did this happen? Also, is there anything else that I'm doing is wrong or not safe regarding pointers ...

return a list<int> from a function c++

Every time I try to use my add function and return a list from it. I get an undefined symbol error. What am I doing wrong here. this is the error: Undefined first referenced symbol in file add(std::list<int, std::allocator<int> > const&, std::list<int, std::allocator<int> >)/var/tmp//...