-7
char* a=(char*)malloc(10);

char* b="ddd";

 *a = *b;

printf("%x\n", a);//print the pointer's address(a) which is not pointer b's address 

printf("%x\n", *a);//what does the print-result mean??? is it the address that b point to?

As I know that a=b; means a point to the address of b.

DunkOnly
  • 1,682
  • 4
  • 17
  • 39
  • 1
    Use `%p` to print pointers: `printf("%p\n", a);` – Pascal Cuoq Oct 18 '14 at 12:23
  • `*a` means "what is pointed to by the pointer `a`". – Hot Licks Oct 18 '14 at 12:24
  • 2
    `*b` is the first `char` at the location `b` points to, which is a `'d'`. `*a` is the first byte that address pointer `a` points to. So `*a = *b` sets the first location pointed to by `a` to the value `'d'`. – lurker Oct 18 '14 at 12:24
  • @PascalCuoq how could I put the address-value of b(that %p print) into the address that a point to? – DunkOnly Oct 18 '14 at 13:59
  • [Don't cast the result of malloc (and friends)](http://stackoverflow.com/q/605845). – Deduplicator Oct 18 '14 at 14:02
  • @guotong1988 You need to stop using the comment system to ask the same question to everyone you can. You won't get any answer that you wouldn't have gotten by using comments as they are intended, and you will annoy people. – Pascal Cuoq Oct 19 '14 at 15:55

3 Answers3

1

The *a = *b, means that the content that is stored in the memory assinged to pointer b is going to be the content of pointer a.

1

a=b : it assigns the value of b to a

*a=*b: it assigns the content located in the address pointed by b to that pointed by a

A good use of what you ask can be illustrated in what follows:

#include<stdio.h>

    void swapping(int *ptr_c, int *ptr_d) {
        int tmp;

        tmp = *ptr_c;
        *ptr_c = *ptr_d;
        *ptr_d = tmp;
        printf("In function: %d %d\n", *ptr_c , *ptr_d);
    }

    int main(void) {
        int a,b;

        a=5;
        b=10;
        printf("input: %d %d\n", a, b);
        swapping(&a,&b);
        printf("output: %d %d\n", a, b);
                return 0;
    }
0
*a = *b;   

The above statement will simply do this: value(a) <-- value(b) (i.e. pointer a will contain value 'd' ).

printf("%x\n", a); 

This will print the pointer's address(a) in hexadecimal. However, always use the "%p" format identifier to print the addresses. "%x" is used to print the values in hexadecimal format.

printf("%x\n", *a);

Since the value(a) is 'd' whose ASCII value is 100 (hundred in decimal). Thus "%x" will print the hexadecimal value of 100 which is 64. Therefore this statement will print 64.

No the above statement will not print the pointer's address(b).

However, pointer's address(b) can be printed by following statement:

 printf("%p", b);
Amit Sharma
  • 1,987
  • 2
  • 18
  • 29
  • how could I put the address-value of b(that %p print) into the address that a point to? – DunkOnly Oct 18 '14 at 14:01
  • 1
    Please be more discriminatory in your terminology. All objects, pointer or not, contain a value. If you mean the pointee of a pointer-object, say that. Also, using `"%p"` for printing pointers is not a matter of preference, but of correctness. The most likely implementations on which this UB will break are common 64-bit systems, as there `sizeof(char*) != sizeof(int)`. Next, you might want to expand a bit on why the last statement prints `64`: See [variadic functions and default promotions](https://stackoverflow.com/questions/22844360/c-c-variadic-functions-and-default-promotions). – Deduplicator Oct 18 '14 at 14:16
  • Additional comment: Don't format your explanations as source-code, break the code-block and make it normal text. (If you want, ping me after doing those changes and I'll take a look whether it now merits further comments and/or an upvote.) – Deduplicator Oct 18 '14 at 14:18