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.

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)

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

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.

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).

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.

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.

Ankit verma said:   1 decade ago
I am sending answer to @Awanish.

If you initialized anything in string so it is not worry about ++. Because + sign is not a sign, it is a character at that time.

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.

Smruti said:   1 decade ago
Any one can help how the 1st printf statement work. I want to say that size(a) means length of a or sizeof datatype of a?

Awanish said:   1 decade ago
How you people are saying that a[] having 10 letters.

Here 8 letter are given including + sign.


Post your comments here:

Your comments will be displayed after verification.