Bloomberg LP Interview Question
Software Engineer / DevelopersActually I was quite surprised at the answer I got for this one. I would have thought that the value returned would be the size of (char *) the pointer. However, I compiled the following program:
#include <stdio.h>
main () {
char a[] = " ";
char b[10];
printf ("Sizeof(a) = %d\n", sizeof(a) );
printf ("Sizeof(b) = %d\n", sizeof(b) );
}
The resulting output was as follows:
Sizeof(a) = 2
Sizeof(b) = 10
What can I say but... go figure
for array a, it contains two char, one is null , the other is '\0'
for array b, it only has ten characters' memory space.
The question was not:
How many characters are in the array?
it was:
What will sizeof(<array name>)?
In the example program I gave, sizeof(a) returns the value 2 while sizeof(b) returns the value 10
each char is 1 byte
so the first one is 2 byte; the second one is 10 byte
the trick here is you should know the first array allocates two characters space
@dfmcla...
if we define char a[], then 'a' would store the address of the string....but 'a' itself is an array...hence, sizeof(a) would give size of this array..
now if we define char* a=new char[10], then, sizeof(a) would give size of a char pointer(same size as int and float pointer)...coz here 'a' is a char pointer....
@dfmcla...
if we define char a[], then 'a' would store the address of the string....but 'a' itself is an array...hence, sizeof(a) would give size of this array..
now if we define char* a=new char[10], then, sizeof(a) would give size of a char pointer(same size as int and float pointer)...coz here 'a' is a char pointer....
Okay I think only pointer will be returning the sizeof(char *). Arrays will be just returning the size of the array. check the below code.
#include <stdio.h>
main () {
char a[] = " ";
char b[10];
char *c;
char *d = b;
printf ("Sizeof(a) = %d\n", sizeof(a) );
printf ("Sizeof(b) = %d\n", sizeof(b) );
printf ("Sizeof(c) = %d\n", sizeof(c) );
printf ("Sizeof(d) = %d\n", sizeof(d) );
}
Sizeof(a) = 2
Sizeof(b) = 10
Sizeof(c) = 4
Sizeof(d) = 4
char a[] = " "; // note there is a space between the double quotes
is interpreted by the compiler to be
char a[] = {' ', '\0'};
so sizeof(a) is 2 since there is 2 char in the array.
that is a special notation that work just for char. Obviously you cannot do
int i[] = " "; // will not compile
the right way is to do
int i[] = { 1, 2, 3 };
when you initialize a c style string the compiler will add a \0 character at the end.
totally agree. this type of question doesnt say anything about well a person can program.
You guys failed on Bloomberg interview? This shows ur understanding of pointers and memory allocations which in turn helps in programming...
Mahesh you are hired as expert c programmer !!, only Bloomberg uses FORTRAN, C pointers when everyone else is moving ahead
In char a[] = " ";
'a' is an empty string i.e., points to '\0' which is defined as integer 0
hence it is sizeof('\0') which is sizeof(0) which is sizeof(int) = 2
sorry but there needs to be some correction in your statement above.
sizeof(int).. is mostly 4 (for 4 byte).. and again.. depends on your machine platform.
If sizeof(int) is 2 for your computer, hope you get a new computer because thats from the old ages. sizeof(\0) also is not sizeof(0) because zero is an integer which should be 4.
The answer for sizeof(a) is true ..because for the declaration -> char a[] = " ", the answer is whatever is in the double quotes + NULL char.
For sizeof(p) where declaration is char *a, it will always be 4 for 32-bit machines or 8 for 64-bit machines, because the asterisk is really a pointer where as the [] symbol denotes and array with.
In order words there is a difference in sizeof for
char a[] = "hello";
and
char *p = "hello";
where
sizeof(a) = 6
sizeof(p) = 4
Read "Pointers on C" book if you want to know more as it depicts a diagram. You will not know your C well if you don't read that book.
Hello guys,
I don't think any of you is right!!!!
For the simple reason that you are arguing, and mahesh is too arrogant to explain why things are as they are!
I am new to C but study very hard!
No one pointed out that the answer depends on compiler.
I was surprised to see, my compiler "Miracle C" gives the following output after the program is ran:
program(same as bala's code here):
#include <stdio.h>
main () {
char a[] = " ";
char b[10];
char *c;
char *d = b;
printf ("Sizeof(a) = %d\n", sizeof(a) );
printf ("Sizeof(b) = %d\n", sizeof(b) );
printf ("Sizeof(c) = %d\n", sizeof(c) );
printf ("Sizeof(d) = %d\n", sizeof(d) );
getchar();
}
output:
Sizeof(a) = 0
Sizeof(a) = 10
Sizeof(a) = 2
Sizeof(a) = 2
Now, since mahesh is such an expert, can he explain the above!!!!
Also Miracle C will show:
char arr[]="Hello";
sizeof(arr) will show to be 0;
OK experts, please enlighten me, because I do not know why, but this is the output I get!!!!!!!!!
Thank you!
Also for the smart dude upstairs:
- Raul Gonzales March 21, 2010My computer is 1yr old, and has Windows Vista, 2GB RAM,
NO STONE AGES!!!!