DECLARATIONS AND INITIALIZATIONS
QUESTIONS:
(SCROLL DOWN FOR ANSWERS)
1) What do you mean bu the following declaration ?
char * p1, p2;
2) What is wrong with the following code ?
char *p;
*p = malloc(10);
3) What is the difference between using a typedef or a preprocessor macro for a user-defined type ?
4) What's wrong with the following declaration and how will you correct it?
typedef struct
{
char *item;
NODEPTR next;
}*NODEPTR;
5) What is the difference between:
const char *p;
char const *p;
char * const p;
6) What is the problem with the following code ?
prashant.c namdeo.c
int a[] = { 1, 2, 3}; extern int a[];
7) What is the difference between the two:
char a[] = "prashant";
char *a = "prashant";
8) How to define a pair of mutually referential structures ?
9) Is the following code correct ?
char a[3] = "pra";
ANSWERS:
1) What do you mean by the following declaration ?
char* p1, p2;
Ans 1) In the above declaration p1 is a pointer to a char, and p2 is simply a char.
Remember, the * in a pointer declaration is not part of the base type; it is part of the declarator containing the name being declared.
In the declaration as written, no matter what the whitespace suggests, the first declarator is "*p1"; since it contains a *, it declares p1 as a pinter t o char. The declarator of p2, however, contains nothing but p1, so p2 is declared as a plain char.
2) What is wrong with the following code ?
char *p;
*p = malloc(10);
Ans 2) The pointer in the above declaration is p and not *p.
so *p = malloc(10); should be written as p = malloc(10);
3) What is the difference between using a typedef or a preprocessor macro for a user-defined type ?
Ans 3)
Consider the following code fragment:
A)
typedef char *name;
#define address char *
name n1, n2;
address a1, a2;
In the above declarations n1, n2 and a1 are pointers to char but a2 is just a char.
B)
typedefs obey scope rules.
4) What's wrong with the following declaration and how will you correct it?
Ans 4) A typedef declaration can't be used untill it is defined, and in the above code, it is not yet defined at the point where the "next: field is declared.
FIX:
A) typedef struct node
{
char *item;
struct node *next;
}*NODEPTR;
B) typedef struct node *NODEPTR;
struct node
{
char *item;
NODEPTR next;
};
C)
struct node
{
char *item;
struct node *next;
};
typedef struct node *NODEPTR;
5) What is the difference between:
const char *p;
char const *p;
char * const p;
Ans 5)
You can interchange the first two; both are pointer to a const character i.e., you can change the pointer but not the character pointed to by that pointer.
In the third case you can't change the pointer but you can change the character pointed to by the pointer,
6) What is the problem with the following code ?
prashant.c namdeo.c
int a[] = { 1, 2, 3}; extern int a[];
Ans 6)
In the "namdeo.c" file the size of array is not specified. And an extern array of unspecified size is an incomplete type. We can't apply sizeof to it, because sizeof operator operates at compile time and is not able to find the size of an array that is defined in another file.
FIX:
a) SENTINEL VALUE SOLUTION:
You can use any sentinel value in the array's last element, so the code can determine the end.
prashant.c namdeo.c
int a[] = {1, 2, 3, -1}; extern int array[];
b) MANIFEST CONSTANT SOLUTION:
cmaster.h
#define ARRAYSZ 3
prashant.c namdeo.c
#include"cmaster.h" #include"cmaster.h"
int a[ARRAYSZ]; extern int a[ARRAYSZ];
c) COMPANION VARIABLE SOLUTION:
prashant.c namdeo.c
int a[] = {1, 2, 3}' extern int a[];
int arrysz = sizeof(a); extern int arraysz;
7) What is the difference between the two:
char a[] = "prashant";
char *a = "prashant";
Ans 7)
The initialization, char *a = "prashant"; , makes this unnamed array to be stored in read-only memory and we can't safely modify it, i.e., if we try to do *a = 't'; , we will get segmentation fault.
8) How to define a pair of mutually referential structures ?
Ans 8)
Here I am posting two methods:
Method 1) Without typedefs:
struct a
{
int adata;
struct b *bptr;
};
struct b
{
int bdata
struct a *aptr;
};
Method 2) With typedef:
typedef struct a *APTR;
typedef struct b *BPTR;
struct a
{
int adata;
BPTR ptr;
};
struct b
{
int bdata;
APTR ptr;
};
9) Is the following statemnet correct ?
char a[3] = "pra";
Ans 9)
Yes, the above statement is legal in C. It declares an array of size 3 and initializes the array with three characters 'p', 'r' and 'a'.
The above array is not a TRUE STRING in C, so you can't use the library functions like strcpy, strcmp, etc. on the array a, because if you see the implementation of these functions , you will find that the traversed till the terminating NULL character.
So, if you want to use library functions like strcpy, strcmp, strstr, strpbrk, strlen etc. then the declaration should be like:
char a[4] = "abc";
The above statement can also be written as:
char a[4] = { 'a', 'b', 'c', '\0' };
Actually, In char a[4] = "abc", the presence of '\0' is implicit.
No comments:
Post a Comment