logo

CuriousTab

CuriousTab

Discussion


Home Technical Questions Programming Comments

  • Question
  • Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how?


  • Correct Answer
  • 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 */ } } } 


  • Programming problems


    Search Results


    • 1. 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 )); }
    • Discuss
    • 2. How would you dynamically allocate a 2-D array of integers?
    • Discuss
    • 3. 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) );
    • Discuss
    • 4. In the following code can we declare a new typedef name emp even though struct employee has not been completely defined while using typedef? < Yes / No> typedef struct employee *ptr; struct employee { char name[20]; int age; ptr next; };
    • Discuss
    • 5. Answer the following Program #define CHARSIZE 8 #define MASK(y) (1 << y % CHARSIZE) #define BITSLOT (y) (y / CHARSIZE) #define SET(x,y) ( x[BITSLOT(y)] = MASK(y) ) #define TEST(x,y) ( x[BITSLOT(y)] & MASK(y) ) #define NUMSLOTS(n) ((n + CHARSIZE - 1) / CHARSIZE) Give the above macros how would you 1. declare an array arr of 50 bits 2. put the 20th bit on 3. test whether the 40th bit is on or off
    • Discuss
    • 6. What is the difference between malloc() and calloc() functions?
    • Discuss
    • 7. If I use the following printf() to print a long int why I am not warned about the type mismatch? printf ("%d",num );
    • Discuss
    • 8. How would you use qsort() function to sort an array of structures?
    • Discuss
    • 9. What will be output of following c code? #include int main() { int i; for(i=10;i<=15;i++){ while(i){ do{ printf("%d ",1); if(i>1) continue; }while(0); break; } } return 0; }
    • Discuss
    • 10. What will be output of following c code? void main() { struct bitfield { unsigned a:5; unsigned c:5; unsigned b:6; }bit; char *p; struct bitfield *ptr,bit1={1,3,3}; p=&bit1; p++; clrscr(); printf("%d",*p); getch(); }
    • Discuss


    Comments

    There are no comments.

Enter a new Comment