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

C and C++ Interview Questions

7Difference between arrays and pointers?

Ans:
- Pointers are used to manipulate data using the address. Pointers use * operator to access the data pointed to by them
- Arrays use subscripted variables to access and manipulate data. Array variables can be equivalently written using pointer expression.

Ex - 1:  What is the output?
void main()
{
int *mptr, *cptr;
mptr = (int*)malloc(sizeof(int));
printf(“%d”,*mptr);
int *cptr = (int*)calloc(sizeof(int),1);
printf(“%d”,*cptr);
}

Ans:
garbage-value 0

Explanation:

The memory space allocated by malloc is uninitialized, whereas calloc returns the allocated memory space initialized to zeros.

Ex - 2 what is the output?:

1.  const char *a;
2.  char* const a;
3.  char const *a;
-Differentiate the above declarations.
 
Ans:
1. 'const' applies to char * rather than 'a' ( pointer to a constant char )
            *a='F'       : illegal
            a="Hi"     : legal
 
2. 'const' applies to 'a'  rather than to the value of a (constant pointer to char )
            *a='F'       : legal
            a="Hi"     : illegal
 
3. Same as 1.


SLogix Student Projects
bottom