LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-15-2012, 11:31 PM   #1
c25fpdQ
LQ Newbie
 
Registered: Sep 2012
Posts: 1

Rep: Reputation: Disabled
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 !
 
Old 09-16-2012, 02:55 AM   #2
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 321

Rep: Reputation: 81
Quote:
Originally Posted by c25fpdQ View Post
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 View Post
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 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 ?
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 View Post
4)
Code:
p = printf("Hello!")
p is of type int; returns the number of characters printed, ?
yes.

Quote:
Originally Posted by c25fpdQ View Post
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 View Post
Code:
p = tf->tf_v0;
p is of type u_int32_t ?
yep.

Quote:
Originally Posted by c25fpdQ View Post
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 View Post
Thank you very much in advance !
you are welcome

Last edited by Celyr; 09-16-2012 at 02:57 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command for identifying the environment variable in a binary at the compilation time logeswari.r Linux - Newbie 8 06-15-2011 07:13 PM
[SOLVED] Conflicting type declarations between sys/types.h and linux/types.h Da Shi Cao Programming 3 09-13-2010 11:55 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
sys/types.h & linux/types.h conflict while compiling johnnyhal Linux - Software 1 12-28-2008 06:39 PM
conflicting redeclaration of sys/types.h and linux/types.h schmil Programming 6 12-11-2008 02:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration