Heres w e can put the keyword const iin another we

Info iconThis 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: tant string' we should say constant changing what the variable points at, but not msg[2] = 'd'; char* msg = "An error has occurred\n"; char* msg const char* msg = "abc"; const msg Object Programming Here's a complication. Here's W e can put the keyword const iin another We const n position in our declaration. position char* const msg = "abc"; char* msg which is changing the contents of where it points. Object Programming char* m1; char* char m2 = "def\n"; m1 = m2; m1[0] = 'D'; This makes C++ ensure that we do not change This the value of the pointer – iit will always point at t the string "abc" This means msg then acts much the same as a This msg then char array. char Object Programming cout << m1; cout << m2; m1 points at the string contained in m2. points m2 If we want a copy of the string use strcpy. If strcpy BUT Object Programming char* m1; char* char m2 = "def\n"; Let's now look at structs. Consider struct strcpy(m1,m2); struct Stype { int member1; float member2; char member3[5]; }; m1 does not point anywhere. It has no space to does hold the string. Even if hold char* m1 = "abc"; we still shouldn't do the copy as • we are changing a constant string • we are copying 5 chars where there is space for 4 Object Programming Until we specify a value for SS, iit points at no SS t location. So location. Stype S; SS = &S; Stype* SS; or typedef Stype* Sptr; Sptr Sptr SS; Object Programming To reference these locations indirectly using the pointer SS, we must either dereference the pointer SS (*SS).member1 will have SS pointing at S's location. will SS pointing W e reference the members of the Stype We Stype as S.member1 S.member2 S.member3 S.member3[3] or use the operator -> or SS->member1 means the component(or member) member1 of means component(or member) member1 of what SS points at. SS Object Programming Object Programming A pointer requires a value which is the address of a data value. data W e have already seen functions which return pointer values although we probably haven't used them as such. them This may be the address of a constant, a global This variable, an automatic variable or a local variable. variable, For example It can be passed as an argument, both by It reference and by value. reference It can also be the return value of a function. Object Programming char* strcpy(char*, const char*); char* strcpy(char returns a pointer to the destination array. But other functions in the string library require us But to use the pointer value. to Object Programming char* ans; ans ans = strchr("abcdef",'d'); strchr("abcdef",'d cout << ans << endl; << ans << endl Consider Consider char* strchr(const char*,int); char* strchr(const which returns the location of the character which (second argument) in the string (first argument). (second That location is a pointer to the character. e.g. strchr("abcdef",'d') has...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online