Thread: variable definitions

  1. #1
    Registered User dan20n's Avatar
    Join Date
    Jan 2005
    Posts
    24

    variable definitions

    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?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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®

  3. #3
    Registered User dan20n's Avatar
    Join Date
    Jan 2005
    Posts
    24
    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?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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 as
    Code:
    int **x(&y);
    This 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.
    Sent from my iPad®

  5. #5
    Registered User dan20n's Avatar
    Join Date
    Jan 2005
    Posts
    24
    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?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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®

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  9. #9
    Registered User dan20n's Avatar
    Join Date
    Jan 2005
    Posts
    24
    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?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SlyMaelstrom
    Agreed, but I didn't want to pass an arguement which may confuse the OP. I suppose a global variable would be more proper.
    All you had to do is make your variables static:
    Code:
    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.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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®

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  2. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM