© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

9. Array is an lvalue or not?

Ans:
An lvalue was defined as an expression to which a value can be assigned. Is an array an expression to which we can assign a value? The answer to this question is no, because an array is composed of several separate array elements that cannot be treated as a whole for assignment purposes. The following statement is therefore illegal:
int x[5], y[5]; x = y;
Additionally, you might want to copy the whole array all at once. You can do so using a library function such as the memcpy() function, which is shown here:
memcpy(x, y, sizeof(y));
It should be noted here that unlike arrays, structures can be treated as lvalues. Thus, you can assign one structure variable to another structure variable of the same type, such as this:
typedef struct t_name
{
char last_name[25];
char first_name[15];
char middle_init[2];
} NAME;
...
NAME my_name, your_name;
...
your_name = my_name;

Ex - 1: What is the output?
            #define max 5
            #define int arr1[max]
            main()
            {
            typedef char arr2[max];
            arr1 list={0,1,2,3,4};
            arr2 name="name";
            printf("%d %s",list[0],name);
            }

Ans:
Compiler error (in the line arr1 list = {0,1,2,3,4})

Explanation:
arr2 is declared of type array of size 5 of characters. So it can be used to declare the variable name of the type arr2. But it is not the case of arr1. Hence an error.
Rule of Thumb:
#defines are used for textual replacement whereas typedefs are used for declaring new types.

Ex - 2: What is the output?
#include
main()
  {
    register i=5;
    char j[]= "hello";                    
     printf("%s  %d",j,i);
}

Ans:
hello 5

Explanation:

if you declare i as register  compiler will treat it as ordinary integer and it will take integer value. i value may be  stored  either in register  or in memory.


SLogix Student Projects
bottom