C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 9)
9.
What will be the output of the program in 16-bit platform (under DOS)?
#include<stdio.h>

int main()
{
    struct node
    {
        int data;
        struct node *link;
    };
    struct node *p, *q;
    p = (struct node *) malloc(sizeof(struct node));
    q = (struct node *) malloc(sizeof(struct node));
    printf("%d, %d\n", sizeof(p), sizeof(q));
    return 0;
}
2, 2
8, 8
5, 5
4, 4
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
36 comments Page 2 of 4.

Ramya said:   9 years ago
The program prints the sizeof(p) &sizeof(q),then wat is the use of this two.

p = (struct node *) malloc(sizeof(struct node));
q = (struct node *) malloc(sizeof(struct node));

Without this two the program will give the output, then what is the need to this two lines?

RAJI said:   9 years ago
Thank you so much @Chandresh.
(1)

K.eswar said:   10 years ago
If the question is printf(%d,%d\n",sizeof(struct node));

The answer would be 4.

But he is asking sizeof (p) which is a pointer.

Sizeof (pointer variable) is always 2 only irrespective of whether int* or struct node*.
(1)

Mayur Gaikwad said:   10 years ago
Hello friends.

The size of pointer depends upon the system with 16/32 bit.

So for 16 bit it will take 2 bytes.

For 32 bit system it will take 4 bytes.

Thank you.
(1)

Giriraj VIT Chennai said:   1 decade ago
For better understanding. Try this:

#include<stdio.h>
#include<string.h>
int main()
{
struct node
{
int data;
//struct node *link;
//Here size of embedded pointer doesn't matter in sizeof(strucutre)
};

struct node *p, *q;
p = (struct node *) malloc(sizeof(struct node));
q = (struct node *) malloc(sizeof(struct node));
printf("%d, %d\n", sizeof(p), sizeof(q));

return 0;
}

Parthasarathi Nayak said:   1 decade ago
#include<stdio.h>

int main()
{
struct node
{
int data;
struct node *link;
};
struct node *p, *q;
p = (struct node *) malloc(sizeof(struct node));
q = (struct node *) malloc(sizeof(struct node));
printf("%d, %d\n", sizeof(p), sizeof(q));
return 0;
}

As we know in GCC compiler integer takes 4 byte, but when we are finding out the sizeof structure internally structure padding occur and even though in structure only one member is integer type here in program another member is structure pointer it will not be accounted as when cause its structure type, it has a special function only holds the address not allocate any memory so that when we are finding out the sizeof structure it will give the first member size in this program.

Ans for GCC -p=4, p=4, better to try and compile by own you will understand.

Gunjan said:   1 decade ago
Actually here we are allocating a memory of 4 bytes 2 for int and 2 for pointer,

But sizeof(p) will give the size of p only which is 2 because p contains the address of that memory location not the memory itself.

PRITAM GORAIN said:   1 decade ago
The output of this question will be 4, 4 in gcc compiler. Since according to ANSI standard all pointers are of 4 bytes in size.

Manideep innamuri said:   1 decade ago
As pointer occupies 2 bytes in 16 bit platform the size will be returned as 2.

Yogeshwar Singh said:   1 decade ago
#include<stdio.h>
int main()
{
char *p;
printf("%d\n", sizeof(p)); //Prints the size of pointer.
printf("%d",sizeof(*p)); //Prints the size of datatype pointed by pointer.
return 0;
}


Post your comments here:

Your comments will be displayed after verification.