logo

CuriousTab

CuriousTab

Discussion


Home Technical Questions Programming Comments

  • Question
  • How would you dynamically allocate a 2-D array of integers?


  • Correct Answer
  • #include "alloch" #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"); } } 


  • Programming problems


    Search Results


    • 1. 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
    • 2. 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
    • 3. 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
    • 4. What would be the output of the following program ? main() { unsigned int a = oxffff; ~a; printf ("%x", a); }

    • Options
    • A. ffff
    • B. 0000
    • C. 00ff
    • D. None of the above
    • Discuss
    • 5. If the following program (myprog) is run from the command line as myprog 1 2 3 what would be the output? main(int argc, char *argv[]) { int i, j = 0; for (i = 0; i < argc ; i++) j = j + atoi ( argv[i]); printf ("%d", j); }

    • Options
    • A. 123
    • B. 6
    • C. Error
    • D. "123"
    • Discuss
    • 6. 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
    • 7. Can I increase the size of a dynamically allocated array? < Yes / No> if yes, how?
    • Discuss
    • 8. What is the difference between malloc() and calloc() functions?
    • Discuss
    • 9. If I use the following printf() to print a long int why I am not warned about the type mismatch? printf ("%d",num );
    • Discuss
    • 10. How would you use qsort() function to sort an array of structures?
    • Discuss


    Comments

    There are no comments.

Enter a new Comment