30 Day Risk-Free Guarantee:
100% money back if you're unsatisfied.
Book (308 Pages):
  • 150 Programming Interview Questions and Solutions
  • Five Proven Approaches to Solving Tough Algorithm Questions
  • Ten Mistakes Candidates Make -- And How to Avoid Them
  • Steps to Prepare for Behavioral and Technical Questions
  • Interview War Stories: A View from the Interviewer's Side
  • Book Preview and More Info

Video (One Hour):
  • Watch CareerCup's founder perform a totally unscripted technical interview of a candidate.
  • Overview of what interviewers look for in software engineering jobs.
  • Technical questions with white-boarding coding where the candidate does well - and struggles.
  • Interviewer reviews with what went well and poorly.
  • Video Preview and More Info

Resume Review (24 - 48hr)
  • Get your resume reviewed by a trained engineering interviewer. More Info
All Products / Services


Express Prep Package (Book)
$29.99 for e-book | $32.45 for paperback
Buy E-Book Buy on Amazon


Standard Prep Package (E-Book & Video)
Emailed Instantly | $39.99
Buy Standard

Elite Prep Package (E-Book, Video & Resume)
Emailed Instantly | $99.99
Buy Elite
 



int x;
function()=x;

Is it legal or illegal code?Why?

16


RK on January 13, 2009 |Edit | Edit

illegal ... left side of the assignment operator expected to a variable.

GJ on January 14, 2009 |Edit | Edit

It is illegal. They are probably looking for a understanding of lvalue and rvalue (in essence what can and cannot appear on the left hand side of an assignment).

haoguohua on January 14, 2009 |Edit | Edit

It can be legal if 'function()' returns a reference to an integer

Anonymous on January 16, 2009 |Edit | Edit

And u shud hold the reference somewhere...

Aditya on January 15, 2009 |Edit | Edit

I agree with haoguohua

shoushou on January 15, 2009 |Edit | Edit

I tried to declare function as "int * function" or "int * *function", both don't work..

anyone knows it?

shoushou on January 15, 2009 |Edit | Edit

done, I get it:

int & function()
{
int * newint = (int *)malloc(sizeof(int));
return (int &) *newint;
}

this could through g++, and also can print out the number,

Anonymous on January 15, 2009 |Edit | Edit

Read

http://www.parashift.com/c++-faq-lite/references.html

section 8.3

Anonymous on January 21, 2009 |Edit | Edit

for sure it could be legal.
===================================
int gg=0;

int& function()
{
return gg;
}

int main()
{
function()=6;
std::cout<<gg<<endl;
return 0;
}

wihenao on August 13, 2009 |Edit | Edit

and it will print a 6

wolverine on October 07, 2009 |Edit | Edit

I don't think this could be done in C.References were introduced in C++ only,there were no references(&) in C.

kunalgaind on November 15, 2009 |Edit | Edit

I don't think, interviewer asked this question to check your understanding on reference......He seems to be concerned with lvalue and rvalue but its make sense to mention about reference after mentioned about lvalue and rvalue........

Mahesh on February 11, 2010 |Edit | Edit

It suffices to give a correct answer. You need not guess the concerns of the interviewer and give an appropriate answer.

It works in C as well :-) on November 30, 2009 |Edit | Edit

int global = 500;

int * function()
{
return &global;
}

int main()
{
*(function())= 1000;
printf("{%d}\n",*function());
getch();
return 0;
}

Anonymous on December 01, 2009 |Edit | Edit

The above program prints 1000 as expected.

abhimanipal on February 22, 2010 |Edit | Edit

You have changed the problem statement.
The original statement was function()=x. It makes a world of difference by putting the *

Add a Text Comment | Add Runnable Code
Name:
Comment:

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








Amazon (1033)Bloomberg LP (403)Qualcomm (117)Adobe (88)Goldman Sachs (78)NetApp (49)IBM (43)Morgan Stanley (33)CapitalIQ (30)Sophos (25)Achieve Internet (23)Electronic Arts (19)Motorola (18)Research In Motion (17)Flipkart (16)
Microsoft (867)Google (141)NVIDIA (98)Yahoo (82)Epic Systems (69)Expedia (47)VMWare Inc (43)Apple (32)Cisco Systems (28)Facebook (23)Infosys (22)Agilent Technologies (19)Sage Software (17)Deshaw Inc (16)FlexTrade (15)
More Companies »
Software Engineer / Dev... (1062)Financial Software Deve... (170)Testing / Quality Assur... (56)Analyst (35)Virus Researcher (25)Field Sales (15)Developer Program Engin... (9)Front-end Software Engi... (6)MyJoB (5)area sales manager (4)Assistant (3)Cabin crew (2)Accountant (1)personnel (1)Intern (1)
Software Engineer in Te... (288)Program Manager (65)Development Support Eng... (47)INTERN(MSIDC) (28)Web Developer (18)System Administrator (10)Consultant (10)Production Engineer (5)Associate Technology L2 (5)AcquireKnowledge (4)Product Security Engine... (3)Solutions Architect (2)Gamer (1)mts (1)Fresh graduate interview (0)
More Job Titles »
Algorithm (1073)Terminology & Trivia (294)C (166)Object Oriented Design (159)Java (121)Testing (114)Arrays (101)Operating System (78)Database (70)Linked List (62)String Manipulation (56)Networking / Web / Inte... (44)Threads (36)Linux Kernel (33)PHP (22)
Coding (511)C++ (204)Behavioral (159)Data Structures (155)Experience (116)Brain Teasers (111)Computer Architecture &... (79)General Questions and C... (73)Trees and Graphs (69)Math & Computation (57)Application / UI Design (45)Ideas (38)System Design (35)Puzzles (30)Bit Manipulation (20)
More Topics »
CareerCup Official Interview Book Daily Questions Requests for Help Mock Interviews Video for Cracking the Coding Interview Job Placement Service CareerCup Blog
My Profile Edit Profile & Email Settings Sign Out