What happens if you write following code?, C/C++ Programming

Assignment Help:

What happens if you write following code?

string& foo()                                                                                                        

{

return "Hello World";

}

cout << foo() << endl;

A: 1. Will give an error as Hello World is created as unnamed character pointer to const. this is being assigned to non-const reference that is not allowed. Could not alter '"Hello World"' to 'std::string&'

2. const string& foo1()

{

return "Hello World";

}

It will give a warning .As you are returning a reference to temporary, which will die instantly when the expression is completed?

classsize.C:7: warning: returning reference into temporary output : Aborted. Segment fault.

3. Char *foo1()

{

return "Hello World";

}

Returning the address of character literal that is created on the static memory.

In C++, the compiler let the use of string literals to initialize character arrays. A string literal contain zero or more characters surrounded through double quotation marks ("). A string represents a sequence of characters that, taken mutually, form a null-terminated string. The compiler developed static storage space for the string, null-terminates it, & puts the address of this space into the char* variable. The sort of a literal string is an array of const chars.

char* szMyString = "Hello world.";

szMyString[3] = 'q'; // undefined, altering static buffer!!!

 

In the following instance, the compiler puts a null-character automatically at the end of the literal string of characters "Hello world". Then it creates a storage space for the resulting string - it is an array of const chars. Then it puts the initial address of this array into the szMyString variable. We will attempt to change this string (wherever it is stored) by accessing it through an index into szMyString. It is a Bad Thing; the standard does not say where the compiler puts literal strings. They can go anywhere, perhaps in some place in memory that you shouldn't be altering.

 


Related Discussions:- What happens if you write following code?

C programming language, write an algorithm that reads a postive integer n a...

write an algorithm that reads a postive integer n and then finds and prints the sum of all integers between 1 and n (inclusive) that are disvisible by 7. please write this using c

Minimum shelf, #At a shop of marbles, packs of marbles are prepared. Packet...

#At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with the

Jewel polishing, Byteland county is very famous for luminous jewels. Lumino...

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

Linear search in array - c program, Linear search in array - C program: ...

Linear search in array - C program: Write a program in c to define a linear search in array. void main()                 {                 clrscr();

Minimum Shelf, At a shop of marbles, packs of marbles are prepared. Packets...

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

Assingment help, can you help with the assignment, I don''t want to upload ...

can you help with the assignment, I don''t want to upload the assignment but want somebody to work with, do you do that?

Explain the process of using the constructor, Using the Constructor The...

Using the Constructor There are basically three ways of creating and initializing the object. The first way to call the constructor is explicitly as :

Develop capturing printer output apps using raspberry, In POS system, cashi...

In POS system, cashier computer connect to printer using serial rs232 usb or cable. We need to capture data from that connection, and parsing the data to get total amount for every

Destruction of local object, Need assignment help What's the order that ...

Need assignment help What's the order that local objects are destructed?

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd