Need assistance with Pointer

Hi, I had just started learning pointer and my lecturer want me to use pointer for my assignment.

basically I have to use pointer and its arithmetic to do it and not allowed to use
Eg. key[k]/ *(key+k) to access the k^th element.

I have the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

	char *pKey = &key[0];
				
	//int i = 0;
	//while ( i < strlen(pKey))
	  
        while ( *pKey != '\0')
	{
		
		*pKey = (char)tolower(*pKey);
		pKey++;
		
	//pKey[i] = (char)tolower(pKey[i]);
	//i++;
		
	}	 
	        cout << "pKey is: " << pKey;



as you can see I had replaced the commented statement with pointer way to convert the pointer array to lower case. The prob is when I cout the pKey,
it is empty.

Can anyone help identify the problem? thank you!
Last edited on
That's because if in the beginning pKey pointed to hello\0, after one ++ it will point to ello\0 and after the whole loop it will point just to \0. You need to make a copy of pKey pointer and do the operations with that copy. Then when you want to print pKey it will still point to the same place.
ahh didnt know it work this way. now it works, thanks!
Topic archived. No new replies allowed.