Hey guys,
Im reading up on pointers and since i dont have a physical teacher, Its abit hard concept to grasp. Could anyone please explain in their own words shortly how they work, I understand that they carry the adress of another variable, and you'd assign this adress with an '&' sign, but I just cant understand once you have the *p=&b, does it mean that now you work with *p just like with b? and why down the road i see p without asterix sign before it., im confused, anyone could clearify in easiest possible way?
Any help would be greatly appreciated!!!
Rhanx a lot!

Cheers


andre

Dani AI

Generated

As observed, a pointer is just a variable that holds the address of some object. The address-of operator & produces that address; the indirection operator * accesses the object stored at that address. The key difference: p (no *) is the address itself; *p is an access to the memory at that address. If p is set to the address of b, writing to *p changes b because both refer to the same storage — but p remains a separate variable that only contains the address.

Pointer types matter: an int* points to int objects and pointer arithmetic is scaled by the pointee size. Arrays decay to a pointer to their first element, which is why array names and pointers are frequently used together. Example of moving through an array with a pointer:

int arr[3] = {10, 20, 30};
int *p = arr;   // points at arr[0]
p++;            // now points at arr[1]
printf("%d\n", *p); // prints 20

Common pitfalls and practical tips: always initialize pointers (use NULL or nullptr), never dereference uninitialized or freed pointers, and cast to (void*) when printing addresses with printf("%p", ...). In C++ prefer references when simple aliasing is needed and smart pointers (unique_ptr, shared_ptr) for ownership. s short demo shows dereference in action, and pointed to a longer tutorial; for a concise reference see C++ pointers tutorial.

Recommended Answers

All 2 Replies

int i;
int *p;

p = &i;   // p holds the address of i

*p = 10;

The last assignment says take the value in p, go to that address and put 10 there.
Therefore, i now holds 10.

To prove that you could print either of these because they are identical.

printf("i is %d", i);      // prints 10
printf("i is %d", *p);  // prints 10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.