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.

Pranay airan said:   1 decade ago
Char a[] has 10 letters and 1 escape character /0 so size of will give 11, size of pointer is 32 bit = 4 byte (1 byte = 8 bits), *a points to char V so sizeof(V) = 1 similarly *b also points to V.

Sivaram bvrice said:   1 decade ago
Thank you.

Sonu said:   1 decade ago
A is an array of character. And its length is 11 including ("\0") null character, so its size is 11 bytes.

*a and *b are pointer variables and point to char V that's size is 1 byte.

Yogesh said:   1 decade ago
char a[] has 10 characters + one escape character= 10+1=11 bytes

pointer *b have size 32 bits= 4 byte

*a points to first character of word(i.e. 'V')= 1 byte
*b points to first character of word(i.e 'V')= 1 byte

So answer prints:
11 4
1 1

Uttam raj said:   1 decade ago
@Pranay Airan:

But the size of int pointer is 4 bytes, size of char pointer is 2 bytes but here how it became 4 bytes i didn't understand could you please explain me.

Vivek said:   1 decade ago
@Uttam

Since b is a pointer, it holds a size of 4 bytes. As given in the question.

Shibu said:   1 decade ago
The size of b is 4 bytes, however *b is pointing to 1st character, so its size is 1.

Madhumita said:   1 decade ago
Size of every pointer variable is 2 byte then how the size of b is 4byte ?

Chaitanya said:   1 decade ago
Thank you. Yogesh garu.

Neeraj said:   1 decade ago
Since all types of pointers i.e. int pointer, char pointer, float pointer contains an address, so the size of each pointer is 2 bytes (for 16 bit compiler) and 4 bytes (for 32 bit compiler).


Post your comments here:

Your comments will be displayed after verification.