logo

CuriousTab

CuriousTab

Discussion


Home C Programming Constants See What Others Are Saying!
  • Question
  • Point out the error in the program.
    #include<stdio.h>
    #define MAX 128
    
    int main()
    {
        char mybuf[] = "India";
        char yourbuf[] = "CURIOUSTAB";
        char *const ptr = mybuf;
        *ptr = 'a';
        ptr = yourbuf;
        return 0;
    }
    


  • Options
  • A. Error: unknown pointer conversion
  • B. Error: cannot convert ptr const value
  • C. No error
  • D. None of above

  • Correct Answer
  • Error: cannot convert ptr const value 

    Explanation
    Step 1: char mybuf[] = "India"; The variable mybuff is declared as an array of characters and initialized with string "India".

    Step 2: char yourbuf[] = "CURIOUSTAB"; The variable yourbuf is declared as an array of characters and initialized with string "CURIOUSTAB".

    Step 3: char *const ptr = mybuf; Here, ptr is a constant pointer, which points at a char.

    The value at which ptr it points is not a constant; it will not be an error to modify the pointed character; There will be an error only to modify the pointer itself.

    Step 4: *ptr = 'a'; The value of ptr is assigned to 'a'.

    Step 5: ptr = yourbuf; Here, we are changing the pointer itself, this will result in the error "cannot modify a const object".


    More questions

    • 1. What will be the output of the program in Turbo C?
      #include<stdio.h>
      
      int main(int argc, char *argv, char *env[])
      {
          int i;
          for(i=1; i<argc; i++)
              printf("%s\n", env[i]);
          return 0;
      }
      

    • Options
    • A. List of all environment variables
    • B. List of all command-line arguments
    • C. count of command-line arguments
    • D. Error: cannot have more than two arguments in main()
    • Discuss
    • 2. What will be the output of the program?
      #include<stdio.h>
      int fun(int, int);
      typedef int (*pf) (int, int);
      int proc(pf, int, int);
      
      int main()
      {
          printf("%d\n", proc(fun, 6, 6));
          return 0;
      }
      int fun(int a, int b)
      {
         return (a==b);
      }
      int proc(pf p, int a, int b)
      {
         return ((*p)(a, b));
      }
      

    • Options
    • A. 6
    • B. 1
    • C. 0
    • D. -1
    • Discuss
    • 3. In the following program add a statement in the function fun() such that address of a gets stored in j?
      #include<stdio.h>
      int main()
      {
          int *j;
          void fun(int**);
          fun(&j);
          return 0;
      }
      void fun(int **k)
      {
          int a=10;
          /* Add a statement here */
      }
      

    • Options
    • A. **k=a;
    • B. k=&a;
    • C. *k=&a
    • D. &k=*a
    • Discuss
    • 4. The '->' operator can be used to access structures elements using a pointer to a structure variable only

    • Options
    • A. True
    • B. False
    • Discuss
    • 5. What are the types of linkages?

    • Options
    • A. Internal and External
    • B. External, Internal and None
    • C. External and None
    • D. Internal
    • Discuss
    • 6. Out of fgets() and gets() which function is safe to use?

    • Options
    • A. gets()
    • B. fgets()
    • Discuss
    • 7. A structure can be nested inside another structure.

    • Options
    • A. True
    • B. False
    • Discuss
    • 8. What will be the output of the program (sample.c) given below if it is executed from the command line?
      cmd> sample one two three
      /* sample.c */
      #include<stdio.h>
      
      int main(int argc, char *argv[])
      {
          int i=0;
          i+=strlen(argv[1]);
          while(i>0)
          {
              printf("%c", argv[1][--i]);
          }
          return 0;
      }
      

    • Options
    • A. three two one
    • B. owt
    • C. eno
    • D. eerht
    • Discuss
    • 9. Which of the following statement is True?

    • Options
    • A. User has to explicitly define the numeric value of enumerations
    • B. User has a control over the size of enumeration variables.
    • C. Enumeration can have an effect local to the block, if desired
    • D. Enumerations have a global effect throughout the file.
    • Discuss
    • 10. Point out the error in the program?
      struct emp
      {
          int ecode;
          struct emp *e;
      };
      

    • Options
    • A. Error: in structure declaration
    • B. Linker Error
    • C. No Error
    • D. None of above
    • Discuss


    Comments

    There are no comments.

Enter a new Comment