#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Need help identifying C variable types.


    Hi guys, I need help with identifying type declaration of variable p.

    I've attempted, but I am not certain of my answers.

    Can someone help me out please ?


    1)

    Code:
    int *vals[5] = [1,3,5,3,5];
    p = *vals[2];
    p is of type of third pointer of array of 5 pointer to int ?

    2)

    Code:
    char *foo(int x) 
       { 
         ... 
          .
         ...
       }
    foo is a type of pointer to function returing char ?

    Code:
    p = foo(5);
    p is of type char ?

    3)

    Code:
    typedef unsigned short u_int16_t;
    u_int16_t *val = ...;
    p = *val;
    val is of type pointer to u_int16_t of type unsigned short ?
    p is type pointer to u_int16_t of type unsigned short ?



    4)
    Code:
    p = printf("Hello!")
    p is of type int; returns the number of characters printed, ?


    5)
    Code:
    struct trapframe 
    {
      u_int32_t tf_vaddr;	/* coprocessor 0 vaddr register */
      u_int32_t tf_status;	/* coprocessor 0 status register */
      ...
      u_int32_t tf_v0;      /* Saved register 2 (v0) */
      ...
    };
    
    
    struct trapframe *tf = ...;
    tf is a type of pointer to struct trapframe ?


    Code:
    p = tf->tf_v0;
    p is of type u_int32_t ?



    6)

    Code:
    struct trapframe tf = ...;
    p = &tf.tf_v0;
    p is of type u_int32_t as well ?


    Thank you very much in advance !
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Originally Posted by c25fpdQ
    Hi guys, I need help with identifying type declaration of variable p.

    I've attempted, but I am not certain of my answers.

    Can someone help me out please ?


    1)

    Code:
    int *vals[5] = [1,3,5,3,5];
    p = *vals[2];
    p is of type of third pointer of array of 5 pointer to int ?
    Nope and the code is not valid C or C++ either.

    Originally Posted by c25fpdQ
    2)

    Code:
    char *foo(int x) 
       { 
         ... 
          .
         ...
       }
    foo is a type of pointer to function returing char ?

    Code:
    p = foo(5);
    p is of type char ?
    Nope, neither the type of foo or p are correct, but you're getting close.

    Originally Posted by c25fpdQ
    3)

    Code:
    typedef unsigned short u_int16_t;
    u_int16_t *val = ...;
    p = *val;
    val is of type pointer to u_int16_t of type unsigned short ?
    p is type pointer to u_int16_t of type unsigned short ?
    val is pointer to unsigned short, that part is correct. Type of p is not correct. Study how pointer dereferencing notation works in C.

    Originally Posted by c25fpdQ
    4)
    Code:
    p = printf("Hello!")
    p is of type int; returns the number of characters printed, ?
    Yep, right on the money. A quick search on google for "man printf" will tell you exactly what the function returns.

    Originally Posted by c25fpdQ
    5)
    Code:
    struct trapframe 
    {
      u_int32_t tf_vaddr;	/* coprocessor 0 vaddr register */
      u_int32_t tf_status;	/* coprocessor 0 status register */
      ...
      u_int32_t tf_v0;      /* Saved register 2 (v0) */
      ...
    };
    
    
    struct trapframe *tf = ...;
    tf is a type of pointer to struct trapframe ?


    Code:
    p = tf->tf_v0;
    p is of type u_int32_t ?
    Yes and yes.

    Originally Posted by c25fpdQ
    6)

    Code:
    struct trapframe tf = ...;
    p = &tf.tf_v0;
    p is of type u_int32_t as well ?


    Thank you very much in advance !
    Nope, but you should study how pointer addresses are obtained.

    Of course, this is assuming that p is declared to be the exact same type as some of the functions. For example:
    Code:
    int foo(void) {
       ...
    }
    ...
    ...
    p = foo();
    It is perfectly legal in C for p to be int, long, long long etc. since the compiler will perform a silent type conversion where it is safe to do so. In all the answers above, I've only assumed that p is the exact type that the expression requires, but other possibilities could be valid as well.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Last edited by salem; September 16th, 2012 at 01:40 AM.
    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

IMN logo majestic logo threadwatch logo seochat tools logo