The title is pretty straight-forward. I want to access a char * string using pointer notation. I know I can print the string directly using the derefencing operator. For examsple, I have written the following program:
#include<stdio.h>
void printString(char * str){
while(*str){
printf("%c", *str);
str++;
}
}
int main(){
char myString[] = "This is a String.";
printString(myString);
return 0;
}
This program prints the string correctly. However, if I change my printString function to following I get garbage:
void printString(char * str){
int i = 0;
while(*str){
printf("%c", *(str+i));
i++; str++;
}
}
Why does this happen? How can I access the string using the array notation?