-7

I don't quite get this, here is a simple program I picked from the internet written in C.

EXAMPLE 1.

int main(void) {

        char * b;

        memmove((char *)&b, "saaaa", 6);
        puts((char *)&b);

        return 0;
}

and here is my own code, EXAMPLE 2.

int main(void) {


    char b[10];

    memmove(b, "baaaa", 6);
    printf("%s\n", b);

    return 0;

}

Why is it using & in EXAMPLE 1 ? I have mostly used & sign when I want to assign a value to the variable like in scanf(). But in EXAMPLE 1, I am not assigning it. The only thing I do is I move the string saaaato the variable b.

And why using (char *) method is required any way?

user3287975
  • 95
  • 1
  • 4

1 Answers1

1

The ampersand operator (&) is used in front of a variable to obtain the address of that variable. Doing &b will get the address in memory of b.

Here is a great SO post on when to use the ampersand : Pointers in C: when to use the ampersand and the asterisk?

Hope this helps.

EDIT : As FatalError has noted in the comments, your example seems broken. The best I can do is point you in the right direction on understanding these operators.

Community
  • 1
  • 1
Corb3nik
  • 1,177
  • 7
  • 26