Programs char msg an error has occurredn char msg this

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: since we use them to store strings. (How do we print address of B?) Object Programming Here is a construct that is very common in C programs. programs. char* msg = "An error has occurred\n"; char* msg This creates a constant string, then declares a This char pointer and gives it the address of the char pointer constant string. constant The variable msg iis then used – quite correctly – The msg s quite just like a character array. We can do this in C++ as well. Object Programming BUT BUT A char pointer and a char array are not the char pointer char array same. same. char* msg = "An error has occurred\n"; char* msg allocates a memory location to store the address allocates as well as the memory needed for the string. as char msg2 = "A booboo has occurred\n"; jjust allocates the 23 bytes needed to store the ust string. string. Object Programming Thus the pointer version has extra memory needed to hold the pointer. needed The critical difference concerns what we can do with the two strings involved – and the value of and the variables. the A reminder also that sizeof(msg2) would yield reminder sizeof(msg2) would 23, but sizeof(msg) gives 4. sizeof(msg We can write W e said earlier that pointers could be treated like We arrays. So for arrays. char* msg = "An error has occurred\n"; char* msg *(msg+9) or msg[9] refer to the 'h' or msg[9] refer 'h' while msg2[9] can be referred to as *(msg2+9). *(msg2+9) while msg2[9] can Object Programming Many a novice programmer is surprised that we can't say can't msg2 = "A different message"; but we can pass msg2 to a function like but msg2 msg = msg2; and even msg = "A different message"; We are just changing the pointer's value. Programming Object Neither the value of the string, nor the value of the pointer variable declared as pointer char* msg3; are altered if we call void func(char m) void func(char { m = "def"; } void func(char* m) void func(char { m = "def"; } That's because the compiler treats char That's arguments exactly like char* arguments. Programming char* Object func(msg3); as, in the function, we are just changing the value as, of a local copy of the pointer variable. of BUT Object Programming W hat if, in the function, we had said something like like void func(char m) void func(char { m[2] = 'D'; } Then that letter would be altered, whether we Then passed the char array or a char pointer. char array char W hat if we wanted to change the string pointed What to by the function argument? W e would be required to pass the pointer by reference. Thus reference. void func(char*& m) void func(char or void func(char** m) void func(char or, more neatly, declared a char* type or, char* typedef char* charPtr; char* charPtr and then void func(charPtr& m) void func(charPtr Object Programming Object Programming const char* msg = "abc"; msg Note that, if we set Note we should not change any individual character of we the string pointed to by msg msg – iit is a constant string. t constant The keyword const iinforms the C++ compiler to The const nforms be on the lookout for attempts to change the value of a variable. value In this case, it allows us to write msg = "A new string"; In fact, if we want msg to be a 'pointer to a In msg to cons...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online