[beginner ask for help] on the problem of pointer operation in c language

I keep the address of another variable in the dynamically allocated memory, and then print the dynamically allocated memory and the address of another variable. I understand that the address of base and p3 in group2 is the same, but why is buf in group1 different from p?

-sharpinclude <stdio.h>

int main(int argc, char const *argv[])
{
    // group1
    char buf[]="hello";
    char *p=(char *)malloc(sizeof(char *));
    *p=buf;
    printf("buf=%p\n", buf);
    printf("*p=%p\n", *p);
    
    // group2
    int base=1;
    int *p3=(int *)malloc(sizeof(int *));
    *p3=&base;
    printf("base=%p\n", &base);
    printf("*p3=%p\n", *p3);
    
    return 0;
}
CPP c
Jul.27,2021

you have two choices:

1 uses the int * pointer
pointer is essentially a memory address value that can be represented as an "integer". So you need a pointer to int instead of a pointer to char. Strictly speaking, you should use the "uintptr_t" type, or "int *" here.

int *p = (int *)malloc(sizeof(int *));

* p is an integer that holds the value of the "char *" pointer. You use "char *" and the address value is truncated.

2 use the pointer of the pointer

char **p = (char **)malloc(sizeof(char **));

=
in fact, what you are doing here is "incorrect" (see the compilation warning), you should use the pointer of the pointer to do it.

The second line of

group2 is junk code. It doesn't work at all.

Menu