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:
30 comments Page 1 of 3.

Aditya Verma said:   4 months ago
@Vishwajeet Jadhav.

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

Vishwajeet Jadhav said:   8 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.
(1)

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

Jeetu said:   5 years ago
Thanks all.

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:   5 years ago
While we finding the string length we didn't count null right?

Please anyone explain it.
(1)

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.

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

11,8
1,1.

Anyone clear my doubt.
(1)

Ziad said:   8 years ago
It's 10 with escape.

Shehnaz said:   8 years ago
Thanks for the given description.


Post your comments here:

Your comments will be displayed after verification.