{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Suppose we write a function which returns the name of

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: the value 'the address of the d' in the string. has Object Programming means ans points at the 'd' and the output means ans points 'd' and would be would def Thus the return value is a location within the Thus memory we informed the function about. memory (It could also be 0.) Object Programming But suppose we want to return something new? Suppose we write a function which returns the name of a month given the month number. name But suppose we want to return something new? Suppose we write a function which returns the name of a month given the month number. name char* MonthName(int m) char* MonthName(int { static char name[12][4] ={"Jan","Feb","Mar","Apr","May", "Jun","Jul","Aug","Sep", "Oct","Nov","Dec"}; return name[m]; return name[m } char* MonthName(int m) char* MonthName(int { static char name[12][4] ={"Jan","Feb","Mar","Apr","May", "Jun","Jul","Aug","Sep", "Oct","Nov","Dec"}; return name[m]; return name[m } Object Programming The static keyword is needed so that the static keyword pointer value we return points at memory that exists. exists. W ithout it, the array becomes automatic and the Without memory it uses will be given up when we return from the function. from That memory would be available for other uses. W e would then be returning a pointer to memory We not currently in use. not Object Programming Object Programming W e must ensure that the pointer value is for valid memory. memory. (Here's an alternative... void MonthName(int m, char** n) void MonthName(int { static char name.... *n = name[m]; *n name[m } still with the static keyword.) still static Object Programming To ensure that the memory referenced by a returned pointer is to fixed memory we must refer to either global variables or static variables. to These are allocated space at compilation time. The other way is to get some memory when we The need it – see in a few moments. W e mentioned earlier that pointers and references were similar. references Both are implemented as addresses – but Both but pointers need dereferencing. pointers We can also return references. Consider the following example using the Consider struct Stype as defined earlier. struct Object Programming Object Programming W e want a function to return an Stype value. Stype value. There are four ways. There W e want a function to return an Stype value. Stype value. There are four ways. There void func1(Stype& v) { // code setting members of v } Stype func2() { Stype v; // automatic variable // code setting members of v return v; } with a call such as with a call such as func1(s); s = func2(); The function directly refers to the variable s The aliased by v. aliased Object Programming This involves copying v to some location and This to then that value is copied to s. Object Programming W e want a function to return an Stype value. Stype value. There are four ways. There W e...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern