C Programming - Memory Allocation - Discussion

Discussion Forum : Memory Allocation - Find Output of Program (Q.No. 6)
6.
Assume integer is 2 bytes wide. What will be the output of the following code?
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int (*p)[MAXCOL];
    p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
    printf("%d, %d\n", sizeof(p), sizeof(*p));
    return 0;
}
2, 8
4, 16
8, 24
16, 32
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
24 comments Page 3 of 3.

Mohini said:   1 decade ago
in the program ,this
p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
has no effect in the output.
this will change the value of p,but it will have not change size of p.

Satish Naik said:   1 decade ago
p is a pointer pointing to array of intergers of size 4
As size of integer is 2 bytes then it becomes totally 2*4=8 bytes
i.e sizeof(*p) = 8 bytes

where as
sizeof(p) = 2 bytes as p is integer as holds the address in unsigned integer form.

John said:   1 decade ago
It depends upon the compiler

Shrinivas said:   1 decade ago
Can any one please explain how did we get the above answer ?


Post your comments here:

Your comments will be displayed after verification.