C Interview Questions – string literal, function pointer, structure pointer, sizeof operator


c interview questions This post has some interview questions that test advanced concepts in C language. These could be asked in interviewing programmers who have experience working with C language. Question.41 is a difficult question that tests whether the programmer is acquainted with the nuances of creating strings in C. Question.42 tests the knowledge of function pointers in C and recognizing an array of function pointers. Question.43 tests the interview candidate’s acquaintance with the unary operator ‘sizeof’ in C. Question.44 tests the programming ability and concept of return value of a function in C. Question.45 tests the programmer’s acquaintance of structure variables and ways of initializing structures. See all C interview questions in this blog in one page.

  1. (i)What are the differences between the C statements below:
    char *str = “Hello”;
    char arr[] = “Hello”;
    (ii)Whether following statements get complied or not? Explain each statement.
    arr++;
    *(arr + 1) = ‘s’;
    printf(“%s”,arr);
  2. (i) char *str=”Hello”;
    “Hello” is an anonymous string present in the memory. ‘str’ is a pointer variable that holds the address of this string.

    char arr[]=”Hello”;
    This statement assigns space for six characters: ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0′ . ‘arr’ is the variable name assigned to this array of characters. Read more about the differences.

    str[4] and arr[4] also have different meanings.
    str[4]: adds 4 to the value of ‘str’ and points to the address same as value of str + 4.
    arr[4]: points to the fourth element in array named ‘arr’.

    (ii) ‘arr’ is variable name of an array. A variable name can not be incremented or decremented. Hence arr++ is an invalid statement and would result in a compilation error.

    *(arr+1)=’s';
    ‘arr’ is the name of a character array that holds string “Hello”. Usually, name of an array points to its base address. Hence value of arr is same as &arr[0].
    arr+1 is address of the next element: &arr[1]

    Character ‘s’ is assigned to the second element in array ‘arr’, thereby string changes from “Hello” to “Hsllo”.

    printf(“%s”,arr );
    This statement prints the string stored in character array ‘arr’.
    Back to top

  3. Explain the variable assignment in the declaration
    int *(*p[10])(char *, char *);
  4. It is an array of function pointers that returns an integer pointer. Each function has two arguments which in turn are pointers to character type variable. p[0], p[1],….., p[9] are function pointers. Read more about Function Pointers in C.

    return type : integer pointer.
    p[10] : array of function pointers
    char * : arguments passed to the function

    Program: Example program to explain function pointers.

    #include<stdio.h>
    #include<stdlib.h>
    
    int *(*p[10])(char *, char *);
    
    //average function which returns pointer to integer whose value is
    average of ascii value of characters passed by pointers
    int *average(char *, char *);
    
    //function which returns pointer to integer whose value is sum
    of ascii value of characters passed by pointers
    int *sum(char *, char *);
    
    int retrn;
    
    int main(void) {
     int i;
     for (i = 0; i < 5; i++) {
      //p[0] to p[4] are pointers to average function.
      p[i] = &(average);
     }
    
     for (i = 5; i < 10; i++) {
      //p[5] to p[9] are pointers to sum function
      p[i] = &(sum);
     }
    
     char str[10] = "nodalo.com";
     int *intstr[10];
     for (i = 0; i < 9; i++) {
      //upto p[4] average function is called, from p[5] sum is called.
      intstr[i] = p[i](&str[i], &str[i + 1]);
      if (i < 5) {
       //prints the average of ascii of both characters
       printf(" \n average of %c and %c is %d",
       str[i], str[i + 1],*intstr[i]);
      }
      else {
       //prints the sum of ascii of both characters.
       printf(" \n sum of %c and %c is %d",
       str[i], str[i + 1], *intstr[i]);
      }
     }
     return 0;
    }
    
    //function average is defined here
    int *average(char *arg1, char *arg2) {
     retrn = (*arg1 + *arg2) / 2;
     return (&retrn);
    }
    
    //function sum is defined here
    int *sum(char *arg1, char *arg2) {
     retrn = (*arg1 + *arg2);
     return (&retrn);
    }

    Download Code

    Output:

    average of n and o is 110
    average of o and d is 105
    average of d and a is 98 average of d and a is 98
    average of a and l is 102
    average of l and o is 109
    sum of o and . is 157
    sum of . and c is 145
    sum of c and o is 210
    sum of o and m is 220

    Explanation:
    In this program p[10] is an array of function pointers. First five elements of p[10] point to the function: int *average(char *arg1,char *arg2). Next five elements point to the function int *sum(char *arg1,char *arg2). They return pointer to an integer and accept pointer to char as arguments.

    Function average:
    int *average(char *arg1,char *arg2) This function finds the average of the two values of the addresses passed to it as arguments and returns address of the average value as an integer pointer.

    Function sum:
    int *sum(char *arg1,char *arg2) This function finds the sum of the two values of the addresses passed to it as arguments and returns address of the sum value as an integer pointer.
    Back to top

  5. What is the value of sizeof(a) /sizeof(char *) in C code snippet below char *a[4]={“sridhar”,”raghava”,”shashi”,”srikanth”}; explain

  6. Explanation:
    Here a[4] is an array which holds the address of strings. Strings are character arrays themselves.

    Memory required to store an address is 4 bits. So memory required to store 4 addresses is equal to 4*4=16 bits.

    char *; is a pointer variable which stores the address of a char variable.
    So sizeof(char *) is 4 bits. Therefore sizeof(a) /sizeof(char *) = 16/4 = 4 bytes. Read more about sizeof operator in C.
    Back to top

  7. Write a program in C that returns 3 numbers from a function.
  8. A function in C can return only one value. If we want the function to return multiple values, we need to create a structure variable, which has three integer members and return this structure. Read more about structures in C.

    Program: Program with a function to return 3 values

    #include<stdio.h>
    
    //sample structure which has three integer variables.
    struct sample {
    int a, b, c;
    };
    
    //this is function which returns three values.
    struct sample return3val() {
     struct sample s1;
     s1.a = 10;
     s1.b = 20;
     s1.c = 30;
     //return structure s1, which means return s1.a ,s1.b and s1.c
     return s1;
    }
    
    int main() {
     struct sample accept3val;
     //three values returned are accepted by structure accept3val.
     accept3val = return3val();
     //prints the values
     printf(" \n %d", accept3val.a);
     printf("\n %d", accept3val.b);
     printf(" \n %d", accept3val.c);
     return 0;
    }

    Download Code

    Output:

    10
    20
    30.

    Explanation:
    In this program, we use C structure to return multiple values from a function. Here we have a structure holding three int variables and a function which returns it. ‘return3val’ is a function which assigns 10, 20, 30 to its integer variables and returns this structure. In this program, ‘accept3val’ is a structure used to accept the values returned by the function. It accepts those values and shows the output.
    Back to top

  9. In below code snippet:
    struct Date
    {
    int yr;
    int day;
    int month;
    } date1,date2;
    date1.yr = 2004;
    date1.day = 4;
    date1.month = 12;
    Write a function in C that assign values to date2. Arguments to the function must be pointers to the structure Date and integer variables date, month, year.

  10. Date is structure with three int variables as members. set_date(..) is a function used to assign values to the structure variable.

    Program: Program to illustrate a function that assigns value to the structure.

    #include<stdio.h>
    #include<stdlib.h>
    
    //declare structure Date
    struct Date {
     int yr;
     int day;
     int month;
    } date1, date2;
    
    //declare function to assign date to structure variable
    void set_date(struct Date *dte, int dt, int mnt, int year) {
     dte->day = dt;
     dte->yr = year;
     dte->month = mnt;
    }
    
    int main(void) {
     date1.yr = 2004;
     date1.day = 4;
     //assigning values one by one
     date1.month = 12;
     //assigning values in a single statement
     set_date(&date2, 05, 12, 2008);
     //prints both dates in date/month/year format
     printf("\n %d %d %d ", date1.day, date1.month, date1.yr);
     printf("\n %d %d %d ", date2.day, date2.month, date2.yr);
     return 0;
    }

    Download Code

    Output:

    4 12 2004
    5 12 2008

    Explanation:
    Two variables of type Date are created and named ‘date1′, ‘date2′. ‘date2′ is assigned by using the function set_date(..). Address of ‘date2′ is passed to set_date function. Read more about pointers to Structures.
    Back to top


About the Author:  This post was written by Ratan Dhoka. He is an alumnus of Kakatiya Institute of Technology. He enjoys coding in C and Java. You can reach him at ratan_jain77@yahoo.com



All comments of post - "C Interview Questions – string literal, function pointer, structure pointer, sizeof operator":

:Haha! I'am the first! Yeh~

Thank you!

Add a Comment / Trackback url

  1. #1  Stuart SemonNo Gravatar

    43.What is the value of sizeof(a) /sizeof(char *) in C code snippet below
    char *a[4]={“sridhar”,”raghava”,”shashi”,”srikanth”}; explain

    Are you talking about a 4 bit byte in your answer?
    Actually a char is 8 bits – 1 byte.
    the size of char * is 8 bits 1 byte as well.
    The sizeof operator returns the size in bytes of its operand.

    The answer is still the same but the explaination is incorrect.

    10/01/26 13:18

Show all Show 5 so far Close all

Comment begin from here or jump up!