I had an exam today and ran into something i have never seen before.
Question went something like this:
int **x ();
what is the type of *x?
Any ideas?
I had an exam today and ran into something i have never seen before.
Question went something like this:
int **x ();
what is the type of *x?
Any ideas?
Meg: Everybody! Guess what I am?
Stewie: Hm, the end result of a drunken back-seat grope-fest and a broken prophylactic?
x is either a function that returns a pointer to a pointer to an int or it's just a regular pointer to pointer to int that your teacher decided would be acceptable to put parenthesis next to that don't contain an initialization value. The type of *x would be a pointer(in otherwords, it holds a memory address). Does this suit you better?Code:int** x();Code:#include <stdio.h> int foo = 2; int **x(); // this is the prototype you see int main() { printf("%i", **(x())); return 0; } int **x() { int* bar = &foo; int** baz = &bar; return baz; }
Last edited by SlyMaelstrom; 06-06-2006 at 04:21 AM.
Sent from my iPad®
Ah yes, thank you for your reply. Im aware of these functions. I wrote the original post in haste because I was hoping for a quick answer as i cant find one. it bugging me big time.
Ill reword the question:
If i declare a _variable_ like this
int **x ( );
what is the type of *x?
the word variable throwing me off here.. doesnt make sense to me, but that is how it was written in the test.
Meg: Everybody! Guess what I am?
Stewie: Hm, the end result of a drunken back-seat grope-fest and a broken prophylactic?
As I wrote, declaring a variable like that... that is to say not a function, is illegal. If this was C++, it could be legal however, you'd have to intialize the variable in the parenthesis, such asThis isn't C++, though, and as that currently stands, your linker will end up complaining about an undefined reference to x when you try to build.Code:int **x(&y);
Sent from my iPad®
I'm 99% sure it was in the ANSI C part of the test. But once reading it could be legal in C++ then it has put doubt in my mind. Well my answer was a pointer to an int, was a guess but anyways...
Thanks for your time.
Meg: Everybody! Guess what I am?
Stewie: Hm, the end result of a drunken back-seat grope-fest and a broken prophylactic?
It's not a variable. It's a function prototype. You must be remembering the question wrong. You can't get "*x" for the code you've listed.
Sly, your code is incorrect also, because your variable is not static, and as such, you're returning values which go out of scope when the function ends.
Quzah.
Hope is the first step on the road to disappointment.
Agreed, but I didn't want to pass an arguement which may confuse the OP. I suppose a global variable would be more proper.
Sent from my iPad®
> int **x ( );
x is a function taking unspecified arguments, and returning a pointer to a pointer to int.
Note that () means (...) in C, but (void) in C++
> what is the type of *x?
This makes no sense, since you can't dereference a function name
I suppose you could do
int *result = *x();
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
The question was small; I wrote it on my hand so I didn’t make a mistake. My self and my peers agree that it must have been a mistake in the wording, still waiting on confirmation from the lecturer. Thanks for your help though..
Meg: Everybody! Guess what I am?
Stewie: Hm, the end result of a drunken back-seat grope-fest and a broken prophylactic?
All you had to do is make your variables static:Originally Posted by SlyMaelstromCode:int **x( void ) { static int foo = 5; static int *bar = &foo; static int **baz = &bar; return baz; }
Quzah.
Hope is the first step on the road to disappointment.
Yeah sure that works. From now on, I shall begin all posts I type when I haven't slept in a while with...
---zzz
When one can't pick up on the bold word in a post, they're sleep habits are just plain unhealthy.
Sent from my iPad®