प्रश्न और उत्तर का अभ्यास करें
8प्र: Improve the following code using typedef. struct node { int data1; float data2; struct node *left; struct node *right; }; struct node *ptr; ptr = (struct node *) malloc (sizeof (struct node) ); 1411 05b5cc7dce4d2b419777515c4
5b5cc7dce4d2b419777515c4- उत्तर देखेंउत्तर छिपाएं
- Workspace
- SingleChoice
उत्तर :
व्याख्या :
typedef struct node * treeptr typedef struct node { int data1; float data2; treeptr *left; treeptr *right; }treenode; treeptr ptr; ptr = ( treeptr ) malloc ( sizeof ( treenode ) );
प्र: Point out the error in the following program. main() { char mybuf[] = "Zanzibar" ; char yourbuf[] = " Zienckewiz"; char * const ptr = mybuf; *ptr = 'a'; ptr = yourbuf; } 1714 05b5cc7dce4d2b419777515c3
5b5cc7dce4d2b419777515c3- उत्तर देखेंउत्तर छिपाएं
- Workspace
- SingleChoice
उत्तर :
व्याख्या :
ptr pointer is constant. In ptr = yourbuf the program is trying to modify it, hence an error.
प्र: What would be the output of the following program ? main() { const int x = 5; int *ptrx; ptrx = &x; *ptr = 10; printf ("%d", x); } 1495 05b5cc7dce4d2b419777515be
5b5cc7dce4d2b419777515be- 15false
- 210true
- 3Errorfalse
- 4Garbage valuefalse
- उत्तर देखेंउत्तर छिपाएं
- Workspace
- SingleChoice
उत्तर : 2. "10"
व्याख्या :
Answer: B) 10 Explanation:
प्र: How would you dynamically allocate a 2-D array of integers? 1019 05b5cc7dce4d2b419777515bd
5b5cc7dce4d2b419777515bd- उत्तर देखेंउत्तर छिपाएं
- Workspace
- SingleChoice
उत्तर :
व्याख्या :
#include "alloc.h" #define MAXROW 3 #define MAXcol 4 main() { int *p, i, J; p = (int *) malloc (MAXROW * MAXCOL * sizeof (int)); for ( i=0; i < MaxROW ; i++) { for (j=0; j < MAXCOL ; j++) { p [ i * MAXCOL + j] = i; printf ( "%d", p [i * MAXCOL + j] ); } printf ("\n"); } }
प्र: How would you free the memory allocated by the following program? #include "alloc.h" #define MAXROW 3 #define MAXCOL 4 main() { int **p, i; p = (int **) malloc (MAXROW * sizeof (int *)); for ( i = 0; i < MAXROW ; i++) p[i] = (int *) malloc (MAXCOL * sizeof (int )); } 1959 05b5cc7dce4d2b419777515bc
5b5cc7dce4d2b419777515bc- उत्तर देखेंउत्तर छिपाएं
- Workspace
- SingleChoice
उत्तर :
व्याख्या :
for ( i=0; i < MAXROW ; i++) free (p[i]); free (p);
प्र: Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how? 942 05b5cc7dce4d2b419777515bb
5b5cc7dce4d2b419777515bb- उत्तर देखेंउत्तर छिपाएं
- Workspace
- SingleChoice
उत्तर :
व्याख्या :
Yes, using the realloc() function as shown below: main() { int *p; p = ( int *) malloc (20) ; t = p; t = (int *) realloc ( p, 40); if ( t == NULL ) Printf (" Cannot reallocate, leaves previous allocated region unchanged "); else { if ( p ==t ) ; / * the array expanded at the same region */ else { free ( p ); / * deallocate the original array */ p = t; /* set p to newly allocated region */ } } }
प्र: What is the difference between malloc() and calloc() functions? 1886 05b5cc7dce4d2b419777515ba
5b5cc7dce4d2b419777515ba- उत्तर देखेंउत्तर छिपाएं
- Workspace
- SingleChoice
उत्तर :
व्याख्या :
As against malloc(), calloc() needs two arguments, the number of elements to be allocated and the size of each element. For example, p = (int *) calloc (10, sizeof (int)); would allocate space for a 10- integer array. Additionally, calloc() would also set each of this element with a value 0. Thus the above call to calloc() is equivalent to: p = (int *) malloc (10 * sizeof (int)); memset (p, 0, 10 * sizeof( int ));
प्र: Point out the error, if any, in the following program. #include "stdarg.h" main() { display ( 4, 12.5, 13.5, 14.5, 44.3); } display(int num, ...) { float c; int j; va_list ptr; va_start (ptr, num); for ( j = 1; j <= num; j++) { c = va_arg ( ptr, float ); printf ("\n%f", c); } } 1478 05b5cc7dce4d2b419777515b9
5b5cc7dce4d2b419777515b9- उत्तर देखेंउत्तर छिपाएं
- Workspace
- SingleChoice
उत्तर :
व्याख्या :
While extracting a float argument using va_arg we should have useed c = va_arg (ptr, double)

