C Programming - Memory Allocation - Discussion

Discussion Forum : Memory Allocation - Point Out Correct Statements (Q.No. 1)
1.
Point out the correct statement will let you access the elements of the array using 'p' in the following program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i, j;
    int(*p)[3];
    p = (int(*)[3])malloc(3*sizeof(*p));
    return 0;
}
for(i=0; i<3; i++)
{
    for(j=0; j<3; j++)
        printf("%d", p[i+j]);
}
for(i=0; i<3; i++)
    printf("%d", p[i]);
for(i=0; i<3; i++)
{
    for(j=0; j<3; j++)
        printf("%d", p[i][j]);
}
for(j=0; j<3; j++)
    printf("%d", p[i][j]);
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
17 comments Page 2 of 2.

Teju said:   1 decade ago
Does

p = (int(*)[3])malloc(3*sizeof(*p));

allocates 54 bytes of memory ? Anyone please explain.

Saksham said:   1 decade ago
@pragya Type casting (int(*)[3]) will not increase the size. So it's just 18 bytes!

Vikrant lakhera said:   1 decade ago
@Vikrant.

There is total memory allocated is 18 bytes.

Ajith said:   8 years ago
As it is 3x3 matrix have created the block b is used.

Bhavi said:   1 decade ago
Please give explanation to this program.

Rishi said:   1 decade ago
Total memory allocated is 18 bytes.

Faizan said:   1 decade ago
@Sharadha it allocates 18 bytes.


Post your comments here:

Your comments will be displayed after verification.