-2

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?

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
Gurkmeja101
  • 592
  • 2
  • 9
  • 24
  • 2
    The problem might just be that your pointer is a pointer to `int`? That means when you increment it, it will point to the next int in memory - and an int is bigger than a char. Why are you using an int pointer anyway? – sje397 Sep 20 '15 at 18:19
  • Why don't you use a pointer to `char` (`char*`) instead of a pointer to `int` (`int*`)? – fuz Sep 20 '15 at 18:21
  • @sje397 I thought that because the header for the print function takes an int as a parameter I had to use the and int pointer before it to reference the string? Is this unnecessary? – Gurkmeja101 Sep 20 '15 at 18:23
  • @Gurkmeja101 The print function you've provided seems broken...where is it from? – sje397 Sep 20 '15 at 18:27

1 Answers1

1

Why do you need to use int * to access char *? It is not correct and shouldn't be done so.

The main problem with it is that each time you increase you pointer ptr by 1 it is incremented by sizeof(int) bytes (which is platform dependent and varies between 2 and 4). While each character in the string is of size 1 byte. Also when you write *ptr you actually access sizeof(int) bytes which may result in segmentation fault if ptr points to the end part of the string.

If you have no option to change the function signature do it like this:

void print(const int *ptr){
    const char* char_ptr = (const char*)ptr;

    while(*char_ptr != 0){
        printf("%c", *char_ptr);
        ++char_ptr;
    }
}

If all you need is just to print the string to which (for some reason) const int* ptr is pointing then you can do something like that:

void print(const int *ptr)
{
    printf("%s", ptr);
}

printf won't check the type of the pointer, it will assume that the pointer is pointing to a buffer of chars and will print the whole buffer until it reachs '\0'.

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
  • Thank you Alex. Yes a problem is that I can't change the signature for the function. If I can't change anything inside the function either, but I have to define everything outside. Would I still define the `char ptr` the same way you've show? Because I would just like to call `print(char ptr)` and have it print the results. – Gurkmeja101 Sep 20 '15 at 18:29
  • @Gurkmeja101 Oh, I see, so you have this function `void print(const int *ptr)` and you know that the pointer is pointing to the string and you want to print that string? Did I understand you correctly? – Alex Lop. Sep 20 '15 at 18:32
  • Lop Kind of. I have that function yes, and I also have the `char *str = "Hello World"`. But I'm not sure how to go about linking these two with each other. How would I go about being able to call `print(*str)` and it will iterate over the `char *str = "Hello World"`? Because just calling the `print` function with `print(*str)` doesn't work since `print`expects a `const int *ptr` parameter. – Gurkmeja101 Sep 20 '15 at 18:38
  • @Gurkmeja101 You can call it `print((const int*)str);` but if the implementation of `print` function that you posted is the one which going to be executed then it won't work as you expect... For explanation why, see the beginning of my answer. (**NOTE** that casting `char*` to `int*` is very dangerous and may lead to undefined behavior, thus not recommended. Use it only if you know and understand what you are doing) – Alex Lop. Sep 20 '15 at 18:43