Thread: Need help identifying C variable types.

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    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
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    These snippets aren't necessarily sufficient to determine anything about the type "storing" the result.

    That said, start with fixing your understanding of "dereferencing".

    Code:
    int *vals[5] = [1,3,5,3,5];
    p = *vals[2];
    The variable `vals' is an array of pointers to `int'.

    With that in mind, `vals[index]' must be a pointer to `int'.

    When you "dereference" a pointer you get the type of the thing where the pointer points; that goes for `vals[index]'.

    So then, `*vals[index]' is an `int'.

    Soma

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This will not compile:
    Code:
    int *vals[5] = [1,3,5,3,5];
    The initialization is wrong (and not just because it has square brackets instead of braces). I can't even figure out what you're trying to do there.


    1. p is an int.
    2. foo is a function taking an int and returning a char pointer.
      p is a char pointer.
    3. val is an unsigned short pointer
      p is an unsigned short
    4. p is an int.
    5. tf is a struct trapframe pointer.
      p is a u_int32_t.
    6. p is a u_int32_t pointer.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Demonoid View Post
    Code:
    int *vals[5] = [1,3,5,3,5];
    p = *vals[2];
    The definition of vals is invalid, so the assignment to p is meaningless. That code snippet will not even compile in any context.

    Quote Originally Posted by Demonoid View Post
    p is of type of third pointer of array of 5 pointer to int ?
    Even if the invalid definition of vals above was fixed, there is no such type as "third pointer of array" of anything.

    Quote Originally Posted by Demonoid View Post
    Code:
    char *foo(int x) 
       { 
         ... 
          .
         ...
       }
    foo is a type of pointer to function returing char ?
    Incorrect. foo is a function that accepts a single argument of type int, and returns a pointer to char. The name of a function may be converted into a pointer, but is not a pointer.

    Quote Originally Posted by Demonoid View Post
    Code:
    p = foo(5);
    p is of type char ?
    Assuming the definition of foo() from your previous example, you should be able to identify for yourself that your answer is incorrect.

    Quote Originally Posted by Demonoid View Post
    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 ?
    Incorrect. I'll leave you to work out what val is on your own. Hint: you need to work out what u_int16_t is separately before you can identify possibilities for what val is.
    Quote Originally Posted by Demonoid View Post
    p is type pointer to u_int16_t of type unsigned short ?
    Also incorrect. p is not (necessarily) even a pointer. There are a number of things p might be, and most of them are not pointers.

    Quote Originally Posted by Demonoid View Post
    Code:
    p = printf("Hello!")
    p is of type int; returns the number of characters printed, ?
    p may be, but is not necessarily of type int.

    Quote Originally Posted by Demonoid View Post
    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 ?
    You got that one right.

    Quote Originally Posted by Demonoid View Post
    Code:
    p = tf->tf_v0;
    p is of type u_int32_t ?
    p may be, but is not necessarily of type u_int32_t.

    Quote Originally Posted by Demonoid View Post
    Code:
    struct trapframe tf = ...;
    p = &tf.tf_v0;
    p is of type u_int32_t as well ?
    p is a pointer (I'll leave you to work out to what). It is (probably) not a u_int32_t.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable types
    By C_ntua in forum C Programming
    Replies: 4
    Last Post: 06-18-2008, 04:13 AM
  2. Variable types?
    By Darth_Paul in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2007, 04:30 AM
  3. variable types
    By pktcperlc++java in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2004, 10:30 PM
  4. variable types
    By chavezbri in forum C Programming
    Replies: 3
    Last Post: 01-16-2003, 02:39 PM
  5. Variable types
    By CodeMonkey in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2001, 06:58 PM