When do you use * and & together?

Asked 2 years ago, Updated 2 years ago, 43 views

I'm learning pointers for the first time, but I'm still confused

I know that & refers to the address of the variable and * is attached to the variable to be used as a pointer

I don't know how to write different arrangements, strings, and pass them as a function ㅜ<

c pointer

2022-09-21 19:50

1 Answers

int* p; // p is a pointer to int
int i;

I'll give you an example of this.

inti2 = *p; // Assign the target pointed by p to i2
int* p2 = &i; // The target pointed by p2 changes to i (value of p2 = address value of i)

An array is not the same as a pointer, but it can be written like a pointer.

inta[2]; // array with int as elements
int i = *a; // the first element of a is stored in i
int i2 = a[0]; // another way to access the first element of a
inta[2]; // array with int as elements
inti = *(a + 1); // Access to second element
inti2 = a[1]; // another way to access the second element

//a[i] == *(a + i); think of


2022-09-21 19:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.