Translate

Labels

Monday 25 March 2013

C PUZZLE


  • main(){

            int a=10,*j;
      void *k;
            j=k=&a;
      j++; 
            k++;
      printf("\n %u %u ",j,k);
}
Answer:
            Compiler error: Cannot increment a void pointer
Explanation:
Void pointers are generic pointers and they can be used only when the type is not known and as an intermediate address storage type. No pointer arithmetic can be done on it and you cannot apply indirection operator (*) on void pointers.

  • Printf can be implemented by using  __________ list.

Answer:
            Variable length argument lists

  •  char *someFun(){

                  char *temp = “string constant";
                  return temp;
            }
            int main(){
                  puts(someFun());
            }
            Answer:
                  string constant
            Explanation:
      The program suffers no problem and gives the output correctly because the character constants are stored in code/data area and not allocated in stack, so this doesn’t lead to dangling pointers.

  • char *someFun1(){

                  char temp[ ] = “string";
                  return temp;
            }
            char *someFun2(){
                  char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’};
                  return temp;
            }
            int main(){
                  puts(someFun1());
                  puts(someFun2());
            }
            Answer:
            Garbage values.
            Explanation:
Both the functions suffer from the problem of dangling pointers. In someFun1() temp is a character array and so the space for it is allocated in heap and is initialized with character string “string”. This is created dynamically as the function is called, so is also deleted dynamically on exiting the function so the string data is not available in the calling function main() leading to print some garbage values. The function someFun2() also suffers from the same problem but the problem can be easily identified in this case.

  • Explain Internal linkage.

Internal linkage means that all declarations of the identifier within one source file refer to a single entity but declarations of the same identifier in other source files refer to different entities.

  • Can the formal parameter to a function be declared static?

No, because arguments are always passed on the stack to support recursion.

  • What is an lvalue?

Something that can appear on the left side of the "=" sign, it identifies a place where the result can be stored. For example, in the equation a=b+25, a is an lvalue.
In the equation b+25=a, b+25 cannot be used as an lvalue, because it does not identify a specific place. Hence the above assignment is illegal.

  • Every expression that is an lvalue, is also an rvalue. Is the reverse true?

No, lvalue denotes a place in the computer's memory. An rvalue denotes a value, so it can only be used on the right hand side of an assignment.

  • What happens if indirection is performed on a NULL pointer?

On some machines the indirection accesses the memory location zero. On other machines indirection on a NULL pointer cause a fault that terminate the program. Hence the result is implementation dependent.

  • Is the statement legal? d=10-*d.

Illegal because it specifies that an integer quantity (10-*d) be stored in a pointer variable

  • What does the below indicate?

 int *func(void)
void indicates that there aren't any arguments.
there is one argument of type void.
Answer: a

  • What are data type modifiers?

To extend the data handling power, C adds 4 modifiers which may only be applied to char and int. They are namely signed, unsigned, long and short. Although long may also be applied to double.

  • Interpret the meaning of the following.

“ab”,”a+b”
“w+t”
Answer:
"ab","a+b"->open a binary file for appending
"w+t" ->create a text file for reading and writing.

  • What is NULL in the context of files?

In using files, if any error occurs, a NULL pointer is returned.

What is Register storage class?
It concerns itself with storing data in the registers of the microprocessor and not in memory. The value of the variable doesn't have to be loaded freshly from memory every time. It's important to realize that this a request to the compiler and not a directive. The compiler may not be able to do it. Since the registers are normally of 16 bits long, we can use the register storage class for ints and char's.

  • What is an assertion statement?

They are actually macros. They test statements you pass to them and if the statement is false, they halt the program and inform you that the assertion failed. It is defined in the header file <assert.h>.

Parse int *(*(*(*abc)())[6])();
abc is a pointer to a function returning a pointer to array of pointer to functions returning pointer to integer.

No comments: