|
Matthew Faithfull wrote: ppint += &i;
Doesn't work - you only can add integral types (size_t and anything that implicitely converts to size_t ) to a pointer.
Also, as Harold Aptroot pointed out in response to my variant, even if it would work as intended, pint would point to 42*sizeof(int) , not 42 .
|
|
|
|
|
I haven't tried it as it's, as you point out, not of practical use but &i is certainly integral. Another step may be needed to get around restrictions on adding pointers to pointers but the principle is the same. The multiplication by sizeof(int) is not an really issue as this can be overcome without any casting.
The addition was a syntactic nicety to explain what I was doing anyway and is not strictly necessary. How about:-
int i = 42;
int** ppint = &i;
int* pint = *ppint;
which is shorter and doesn't do pointer arithmetic.
"The secret of happiness is freedom, and the secret of freedom, courage."
Thucydides (B.C. 460-400)
|
|
|
|
|
Dear Matthew,
Thanks for submitting your solution. Unfortunately, ppint is of type int**, whereas &i returns an int*, so this will not work without casting int* to int**.
|
|
|
|
|
I would like to see the assmebler generated for the cast from int* to int**. My expectation is that it would consist of 0 instructions or at most a nop. If no actual cast takes place and none is specified in the code then I say the challenge is met. Assignment from int* to int** is certainly valid. In reality at the backend of most compilers I suspect int* and int** will be same thing anyway.
"The secret of happiness is freedom, and the secret of freedom, courage."
Thucydides (B.C. 460-400)
|
|
|
|
|
It's true that in many implementations of C/C++ a pointer to one type is technically (in terms of generated code and storage needs) the same as a pointer to any other type.
But in terms of the language, int* is certainly not an int**. At the very least, the compiler will give you an error on the line int **ppi = &i , saying there is no conversion from int* to int**. But ok, that's the compiler, so maybe it's just being annoying? One nice way, I think, of realising these two (int* and int**) are two very different objects is the following:
int* pi = ...;
int** ppi = ...;
*pi; *ppi; **ppi; **pi;
So I'd say that int* and int** can't be the same thing, because the one can do something the other can't 
|
|
|
|
|
int i = someIntValue();
int *pint = 0;
pint += i;
Note that this doesn't even require sizeof(int)==sizeof(int*) , as i will be cast to the correct size implicitely.
|
|
|
|
|
But it will also be multiplied by sizeof(int) implicitly..
And if you divide by sizeof(int) first, it kills the lowest bit(s).
|
|
|
|
|
Ah, you're right on both accounts.
Not that it would make much sense to have an incorrectly aligned pointer, but then the challenge didn't make much sense for practical use anyway...
|
|
|
|
|
The following code compiles and it does return the correct values:
int* f1(int i) { return 0; }
int* f2(int* pi) { return pi; }
union UPointers {
int*(*ag1[2])(int i);
struct SPointers {
int*(*g1)(int i);
int*(*g2)(int* pi);
} my_spointers;
} my_pointers;
int* foo(int i) {
int* (*h1)(int i);
my_pointers.my_spointers.g1 = &f1;
my_pointers.my_spointers.g2 = &f2;
h1 = my_pointers.ag1[1];
return h1(i);
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 12; i < 15; ++i) {
int* pi = foo(i);
std::cout << i << ' ' << pi << std::endl;
}
return 0;
}
The output is:
12 0000000C
13 0000000D
14 0000000E
The idea is to abuse a function pointer that is supposed to pass an int argument by assigning it to a function that expects an int* . I'm almost sure there is no implicit conversion involved, but I'm not too familiar with function pointers to be sure.
The 'conversion' happens when the function call writes the int argument to the stack, but the function that is called interprets it as a int*.
|
|
|
|
|
Dear Stefan,
I do indeed think (if I am not mistaken) that your code gets the job done without performing any casts.
I also enjoyed seeing you use a union for the job.
The solution I had in mind also employs unions, but does not use function pointers:
int* withoutCasts(int i)
{
union {int j; int* pint;} converter;
converter.j = i;
return converter.pint;
}
Thanks to everyone who spent time thinking on this, and if anyone has a different solution (possibly one without unions) then I'd love to hear it.
|
|
|
|
|
Originally my idea was to use a struct of function pointers and 'iterate' over them to arrive at a different pointer. However, I found that pointer arithmetic apparently doesn't work on function pointers. That is when I thought of union. It was only later that I realized the union construct can solve the task by itself. But since I already posted my solution, there was no point in 'watering it down'.
Another thing I thought of is overloading virtual functions with different return types. But it would require at least a downcast of the instance pointer:
class base {
public:
virtual ~base() {}
virtual int* foo(int i) { return &i; }
};
class derived : public base {
public:
virtual ~derived() {}
virtual int foo(int i) { return i; } };
int* bar(int i) {
base* caster = new derived; int* pi = caster->foo(i); delete caster;
return pi:
}
I wonder if I could get this to work if I put part of the code inside a constructor, before the construction of the vtable - but then the behaviour would be undefined 
|
|
|
|
|
how can use tinyxml to mfc dialogbased application,i mean which files i have to add to my project to use tinyxml
|
|
|
|
|
|
Straight from the TinyXML website:
Quote: To Use in an Application:
Add tinyxml.cpp, tinyxml.h, tinyxmlerror.cpp, tinyxmlparser.cpp, tinystr.cpp, and tinystr.h to your project or make file. That's it! It should compile on any reasonably compliant C++ system. You do not need to enable exceptions or RTTI for TinyXML.
|
|
|
|
|
Nothing,Just an Edit
modified 8-Mar-13 8:04am.
|
|
|
|
|
This is the C/C++ forum. You might ask this in the PHP forum[^] in the Web Development section.
But you should be more spefic about the problem.
|
|
|
|
|
hi
in my property sheet i have to put install button instead of Next button and add event to that, how can i put that.
|
|
|
|
|
|
venkatesh52867 wrote: ...Next button...
Are you using a "wizard" dialog? If not, what is the Next button for? I have an example of hiding/adding buttons to property sheets here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Need JSON parser, please help
|
|
|
|
|
Did you Google[^] for, yet?
Veni, vidi, vici.
|
|
|
|
|
What exactly is your C/C++/MFC question?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
HELLO
I am pretty new in c and now trying to use gmp.
Am trying to decrypt a file using gmp on rsa encryption.how do i go abt the primes, p and q. i am getting a wrong output on the file
please i need help asap
thanks
|
|
|
|
|
etesi wrote: now trying to use gmp Which is?
etesi wrote: i am getting a wrong output on the file Well unless we can see what is the difference between that and the correct output, and some of the code that is causing the error, then it is unlikely that we can offer much to help you.
Use the best guess
|
|
|
|
|
I think the OP means this[^].
"It was when I found out I could make mistakes that I knew I was on to something."
-Ornette Coleman
"Philosophy is a study that lets us be unhappy more intelligently."
-Anon.
|
|
|
|