I am trying to traverse an array of chars with pointers. Every x characters, i want to make it a capitalized character. I have a pointer to the array and the array. The pointer is set to a const char* but the array is set to a char array so that i can change the values in the array. When i compile i get an error, assignment of read only location. Here is the code that i have that gives me the error:
Code:
void
changeText(const char* text, int count){
int counter = 0;
for(int i=0; ;i++){
if(!strncmp((text+i), "\0", 1)) break; //break when read null
//check to see if its a character
if(((int)*(text+i)>=65 && (int)*(text+i)<=90) ||
((int)*(text+i)>=97 && (int)*(text+i)<=122)){
if((counter % count) == 0)
*(text+i) = toupper((int)(text+i));
puts("Do something\n");
counter++;
}
else{
//make character a space
}
}
puts("Called changeText()\n");
}
This is the line that gives me the error:
Code:
*(text+i) = toupper((int)(text+i));
Here is the function call if that helps
Code:
const char* ptext;
char text[MAX];
ptext = &text;
changeText(ptext, count);
I don't understand why i getting this error if i'm not trying to change the value of the pointer itself, just want it points to. Am i getting confused by how pointers work?