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.

7
  • The type of &message is char (*)[12]. It's not compatible with char **. Or char * as you cast it. Commented Jun 19, 2023 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 Commented Jun 19, 2023 at 16:14
  • Should I just assign like char ** ptr_message2 = &message2 ? Commented Jun 19, 2023 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 Commented Jun 19, 2023 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. Commented Jun 19, 2023 at 16:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.