0

I have been trying to figure out how double pointer works with char * and char []. What I want to do is to assign a double pointer to the char * or char [] and then change the content.

 #include <stdio.h>

int main()
{
    char * message2 = "bye world";
    char message[] = "hello world";
    
    char ** ptr_message2 = &message2;
    char ** ptr_message = (char*)&message;
    
    *ptr_message2 = "Bye";
    *ptr_message = "Hello";
    
    printf("%s \n", message2);
    printf("%s \n", message);
    return 0;
}

I want to use char[] and assign double pointer to it, and change the content but I have hard time understanding.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Lucky Im
  • 47
  • 7
  • The type of `&message` is `char (*)[12]`. It's not compatible with `char **`. Or `char *` as you cast it. – Some programmer dude Jun 19 '23 at 16:06
  • you're not doing it, but note that `message2` is a string literal, and any attempt to modify it [results in undefined behavior](https://stackoverflow.com/questions/5464183/modifying-string-literal) – yano Jun 19 '23 at 16:14
  • Should I just assign like char ** ptr_message2 = &message2 ? – Lucky Im Jun 19 '23 at 16:14
  • what I actually wanted is that to modify the string (which I assigned as char[] ) and then point with double pointer to modify the string with double pointer. :( so I was trying but my lack of knowledge is giving me obstacles – Lucky Im Jun 19 '23 at 16:16
  • 2
    To modify the string in `message` you have to use `strcpy()`. An array is not the same as a pointer, you can't modify the address it points to. – Barmar Jun 19 '23 at 16:16
  • Use a paper and a pen, or a program on a PC, write the memory content of your program like this: make fields (like in a table, one cell per byte/address/char). Fill the cells with the content. Place the strings you have somewhere. Think about which variable is where, use some cells for pointers, draw a arrow from the pointer to where the pointer points to (for example to a string). Think about which variable actually holds the strings, which variables only hold a pointer to it, ... If you still don't understand it afterwards, upload your drawing. – 12431234123412341234123 Jun 19 '23 at 17:13
  • ok! Thank you I will try to draw and upload it if I really cannot understand :3 – Lucky Im Jun 19 '23 at 17:15

1 Answers1

2

In this declaration

char * message2 = "bye world";

there is declared a pointer that points to the first character of the string literal "bye world". You may reassign the pointer to point to another string as for example

message2 = "Bye";

The string literals themselves are not changed. It is the pointer that is changed. At first it pointed to the first character of one string literal and then it was reassigned by the address of the first character of another string literal.

This statement

char ** ptr_message2 = &message2;

only adds one more indirection. So the above statement

message2 = "Bye";

can be rewritten like

*ptr_message2 = "Bye";

Arrays opposite to pointers do not support the assignment operator. If you want to change a content of an array you need to change its elements separately. For example to copy a string to a character array you can write

#include <string.h>

//...

char message[] = "hello world";

strcpy( message, "Hello" );

As for your code then after this declaration

char ** ptr_message = (char*)&message;

the pointer ptr_message stores the address of the extent of memory occupied by the array that is the same address as the address of the first character of the array.

Dereferencing the pointer

*ptr_message = "Hello";

the value stored in the first elements of the array is considered as a value of an address. As the stored string "hello world" does represent a valid address then the dereferenced pointer has an invalid value and the statement above invokes undefined behavior.

Using a pointer to pointer you could write for example by introducing an intermediate pointer the following way

#include <string.h>

//...

char message[] = "hello world";

//...

char *ptr = message;
char ** ptr_message = &ptr;

//...

strcpy( *ptr_message, "Hello" );

Now dereferencing the pointer ptr_message you get another pointer (instead of characters of a string literal) that points to the first character of the array and may use it to change elements of the array.

Pay attention to that in these declarations

char * message2 = "bye world";

and

char *ptr = message;

the arrays (string literals have types of character arrays) used as initializers are implicitly converted to pointers to their first elements.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335