I have a string
char *str = "hello world";
I also have a int pointer
int *ptr;
How can I use these two in a function that loops over the string and prints all the chars in it? The header function would be something like this:
void print(const int *ptr){
while(*ptr != 0){
printf("%c", (char)*ptr);
++ptr;
}
}
Now I know that I want to use the ptr to somehow reference the char ptr. But how would I do this? I've tried doing just
ptr = str;
And tried a whole bunch of different combinations of
ptr=*str;
ptr=&str;
And so on.
I know I can iterate over the string just doing
while(*str != 0){
printf("%c",*str)
str++;
}
And that I can also do it using index elements like str[0]. But how can I use a pointer to act as the index element for the char string?