Interview Question


Country: India




Comment hidden because of low score. Click to expand.
0
of 2 vote

You will get linker error!


Compiler only looked for names for binding not the type so int *a was binded to int a[] (basically that means both a have same address). If you try to print '&a' in file 1 and '&a' in file 2 you will get same address. so both the pointers are at same location.
So basically binding is some what like this
&a = &a; instead of a = a

Although the pointer addresses are same the content at the addresses ('a ' not *a )) is not. In first case it was the addresses of first element of array (const int*) , in second case it is NULL. Why it is happening is something , that can be attributed solely to compiler architecture . Here what is happening is , a const int* cannot be converted into int* so the compiler decides to Make it NIL.

This will work;
file 1: char* a =&('a')
fil2 2; extern int *a;
print *a; //off course with %c to avoid memory infringement
because char* can be type casted to int*

In your example even access to *a is in valid as it is essentially Null.

My final conclusion is may be on different compiler the results will be diffrent as it is clearly upto compiler how to handle.

- words&lyrics July 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is just not true. You can use an array as a pointer to the first item of it.

- Selmeczy, Péter July 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Selme... What is not true?? Better try the code and see for your self!

- words&lyrics July 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If it is as in the example then there is no problem at all.

IF int arr[10]; would be a const array than the write access could fail - based on the operating system and memory allocation of the C compiler.

- Selmeczy, Péter July 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is giving linker error. Please check it.

- Aashish July 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For me using gcc in ubuntu, this is giving seg fault. And when I comment arr[0]=10 in file2 & print arr in file1 & (&arr) in file2, I get the same address. So, I guess the *arr in file2 is linked to &arr in file1 though in file1, arr and &arr are same. I don't know if it is compiler dependent..

- coder August 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if file1 and file2 are compiled independently, there is no linker error. see <expert c programming> chapter 4.

- leilei September 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why is it giving segmentation fault??

- Rish October 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

compile failed, declaration mismatch and conflict definition.

- Bin December 12, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More