C Programming - Strings - Discussion

Discussion Forum : Strings - Find Output of Program (Q.No. 17)
17.
If the size of pointer is 32 bits What will be the output of the program ?
#include<stdio.h>

int main()
{
    char a[] = "Visual C++";
    char *b = "Visual C++";
    printf("%d, %d\n", sizeof(a), sizeof(b));
    printf("%d, %d", sizeof(*a), sizeof(*b));
    return 0;
}
10, 2
2, 2
10, 4
1, 2
11, 4
1, 1
12, 2
2, 2
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
31 comments Page 1 of 4.

Vishwajeet Jadhav said:   11 months ago
I think the given answer is wrong.

The right answer should be 11, 8.
1,1
char a[] = "Visual C++"; initializes an array a with the string "Visual C++". The size of this array is the length of the string plus one for the null terminator, so sizeof(a) will be 11.

char *b = "Visual C++"; initializes a pointer b to point to the string "Visual C++". The size of a pointer on most systems is 8 bytes, so sizeof(b) will be 8.

sizeof(*a) gives the size of the first element of the array a, which is a char and is 1 byte.

sizeof(*b) gives the size of the first element pointed to by b, which is also a char and is 1 byte.
(3)

Ankith said:   4 years ago
Thank you @Pranay.
(1)

Kareena said:   5 years ago
@M.Guna:

Yes, during string length, we don't count null.
But in sizeof, we count null.
(1)

M.guna said:   6 years ago
While we finding the string length we didn't count null right?

Please anyone explain it.
(1)

Rekha said:   9 years ago
When I compile I got,

11,8
1,1.

Anyone clear my doubt.
(1)

New coder said:   1 decade ago
Can anyone explain why its giving error? If program is correct.

Gouri Suders said:   1 week ago
Here, char *b is a pointer variable, so the number of bytes depends on the bitness of the system.
So if the system is 32, then the answer is 4.
If it is 64 bit system, then it is 8.
So, that's why here 4 bit, here they considered a 32-bit system.

Aditya Verma said:   7 months ago
@Vishwajeet Jadhav.

Here it's mentioned 32 bits that means 4Byte of pointer size is correct.

Jeetu said:   5 years ago
Thanks all.

Saurabh said:   7 years ago
@Rekha.

Your system must be 64 bit. Hence the size of the pointer is 64 bit. That's why it is showing 11, 8 instead of 11, 4.


Post your comments here:

Your comments will be displayed after verification.