Saturday 30 March 2013

Quiz 5: switch case


Quiz 5: switch case

1. Which follows the case statement?
A. :
B. ;
C. -
D. A newline

2. What is required to avoid falling through from one case to the next?
A. end;
B. break;
C. Stop;
D. A semicolon.

3. What keyword covers unhandled possibilities?
A. all
B. contingency
C. default
D. other

4. What is the result of the following code?
int x=0;

switch(x)

{

  case 1: cout<<"One";

  case 0: cout<<"Zero";

  case 2: cout<<"Hello World";

}
A. One
B. Zero
C. Hello World
D. ZeroHello World

Quiz 4: Functions


Quiz: Functions

1. Which is not a proper prototype?
A. int funct(char x, char y);
B. double funct(char x)
C. void funct();
D. char x();

2. What is the return type of the function with prototype: "int func(char x, float v, double t);"
A. char
B. int
C. float
D. double

3. Which of the following is a valid function call (assuming the function exists)?
A. funct;
B. funct x, y;
C. funct();
D. int funct();

4. Which of the following is a complete function?
A. int funct();
B. int funct(int x) {return x=x+1;}
C. void funct(int) {cout&tl;<"Hello"}
D. void funct(x) {cout<<"Hello"}



Some Complex Pointers

Some Complex Pointers


(2) How to read following pointer?
float (* ptr)(int)
Answer: Assign the priority considering precedence and associative.


Now read it following manner:
ptr is pointer to such function whose parameter is int type data and return type is float type data.
Assign the priority of each function parameter separately and read it also separately.

(3) How to read following pointer?

void (*ptr)(int (*)[2],int (*) void))

Answer:

Assign the priority considering rule of precedence and associative.


Now read it following manner:

ptr is pointer to such function which first parameter is pointer to one dimensional array of size two which contentint type data and second parameter is pointer to such function which parameter is void and return type is int data type and return type is void


(4) How to read following pointer?

int ( * ( * ptr ) [ 5 ] ) ( )

Answer:
Assign the priority considering rule of precedence and associative.



Now read it following manner:

ptr is pointer to such array of size five which content are pointer to such function which parameter is void and return type is int type data.



(5) How to read following pointer?

double*(*(*ptr)(int))(double **,char c)

Answer:
Assign the priority considering rule of precedence and associative.

 


Now read it following manner:

ptr is pointer to function which parameter is int type data and return type is pointer to function which first parameter is pointer to pointer of double data type and second parameter is char type data type and return type ispointer to double data type.


(6) How to read following pointer?

unsigned **(*(*ptr)[8](char const *, ...)

Answer: 
Assign the priority considering rule of precedence and associative.

 


Now read it following manner:

ptr is pointer to array of size eight and content of array is pointer to function which first parameter is pointer to character constant and second parameter is variable number of arguments and return type is pointer to pointer of unsigned int data type. 


Pointer Explanation


Pointer

What is pointer in c programming?

Pointer is a user defined data type which creates special types of variables. These variables can hold the address of primitive data type like char, int, float, double or user defined data type like function, pointer etc. or derived data type like array, structure, union, enum.

Examples of pointer variables:
int *ptr;                // primitive data type
int (*ptr)();           // User defined data type
int (*ptr)[2];                   // derived data type

In c programming every variable keeps two type of value.
1. Value of variable.
2. Address of variable where it has stored in the memory.

(1) Meaning of following simple pointer declaration and definition:
int a=5;
int *ptr;
ptr=&a;
Explanation:
About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 5
3. Address where it has stored in memory : 1025 (assume)
About variable ptr:
4. Name of variable : ptr
5. Value of variable which it keeps address of variable a: 1025
6. Address where it has stored in memory : 5000 (assume)
(2) Meaning of following pointer declaration and definition (pointer to pointer):
int a=50;
int *ptr1,**ptr2;
ptr1=&a;
ptr2=&pt1;
Explanation:
About variable a:
1. Name of variable : a
2. Value of variable which it keeps: 50
3. Address where it has stored in memory : 5000 (assume)
About variable ptr1:
4. Name of variable : ptr1
5. Value of variable which it keeps: 5000
6. Address where it has stored in memory : 9000 (assume)
About variable ptr2:
7. Name of variable : ptr2
8. Value of variable which it keeps: 9000
9. Address where it has stored in memory : 9555 (assume)

Note:
* is known as indirection operator which gives content of any variable
& is known as reference operator which gives address

Cancellation rule of above two operators:
* and & operators always cancel to each other i.e. *&p=p
But it is not right to write: &*p=p




What will be output of following c program?
#include<stdio.h>
#include<conio.h>
void main()
{
    int x=25;

    int *ptr=&x; //statement one

    int **temp=&ptr; //statement two

          printf(“%d%d%d”,x,*ptr,**temp);

    getch();
}

Output: 25 25 25

Explanation:
As we know value of variable x is 25.

*ptr= *(&x) //from statement one
=*&x
=x //using cancellation rule
=25




How to read complex pointers in C Programming?

Assign the priority to the pointer declaration considering precedence and associative according to following table.
Where
(): This operator behaves as bracket operator or function operator.
[]: This operator behaves as array subscription operator.
*: This operator behaves as pointer operator not as multiplication operator.
Identifier: It is not an operator but it is name of pointer variable. You will always find the first priority will be assigned to the name of pointer.
Data type: It is also not an operator. Data types also includes modifier (like signed int, long double etc.)
You will understand it better by examples:
(1) How to read following pointer?
char (* ptr)[3]
Answer:
Step 1: () and [] have equal precedence. So rule of associative will decide the priority. Its associative is left to right So first priority goes to ().


Step 2: Inside the bracket * and ptr enjoy equal precedence. From rule of associative (right to left) first priority goes to ptr and second priority goes to *.


Step3: Assign third priority to [].


Step4: Since data type enjoys least priority so assign fourth priority to char.


Now read it following manner:
ptr is pointer to such one dimensional array of size three which content char type data.