LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help identifying C variable types. (https://www.linuxquestions.org/questions/programming-9/need-help-identifying-c-variable-types-4175427462/)

c25fpdQ 09-15-2012 11:31 PM

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 !

Celyr 09-16-2012 02:55 AM

Quote:

Originally Posted by c25fpdQ (Post 4781386)
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 ?

in this case p is int
read it as int **vals
and then p = **(vals+2)

Quote:

Originally Posted by c25fpdQ (Post 4781386)
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 ?

foo return a pointer to a char
and also p is char* thus is _maybe_ a pointer to a sequence of char null terminated

3)

Quote:

Originally Posted by c25fpdQ (Post 4781386)
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 a pointer, and p is a u_int16_t, I think that the format used by most of C developers can be misleading for students, read it this way:
u_int16_t* val = ...;
p = *val;

in this way it should be clear that val is a pointer type and p is a u_int_16_t (well it isn't defined but it should be like that)

Quote:

Originally Posted by c25fpdQ (Post 4781386)
4)
Code:

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

yes.

Quote:

Originally Posted by c25fpdQ (Post 4781386)
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 ?

yep

Quote:

Originally Posted by c25fpdQ (Post 4781386)
Code:

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

yep.

Quote:

Originally Posted by c25fpdQ (Post 4781386)
6)

Code:

struct trapframe tf = ...;
p = &tf.tf_v0;

p is of type u_int32_t as well ?

No, when you use & you are getting the memory address of that variable. So p is u_int32_t*, a pointer

Quote:

Originally Posted by c25fpdQ (Post 4781386)
Thank you very much in advance !

you are welcome


All times are GMT -5. The time now is 10:29 AM.