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...
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...
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...
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?
...
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...
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...
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...
I want to know what is happening in memory when you declare:
int **array;
...
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...
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?
...
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 ...
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.
...
#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...
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
...
as in the title, what's the difference because these two seem to get me the same results?
...
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?
...
int *w;
int **d;
d = &w;
What does the **d store exactly?
...
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?...
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 ...
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//...