Hi, so ive been teaching my self pointers, and I just wanted to clarify what I assume so far.

You cant declare: int var1 = &var2;
You MUST use a pointer to store an address: int *var1 = &var2;

When assigning a pointer variable, it must always point to an address. So that means you cant go: int *var1 = var2;

So that means I can always assuming, when working with a pointer, the format when declaring one is ALWAYS :

if your initilizing it:
Datatype *varName = &varName;

if your assigning it:
varName(early declared as a pointer) = &varName;

Ive tested all this and it proves to be true, but I was wondering if you guys had any further input. Thanks =D

Dani AI

Generated

Good start — pointers are one of those topics that feel simple at first but bite you later. is right to flag null pointers: a pointer does not have to hold the address of a live object. The important distinction is between a pointer's type (it must be declared as a pointer type to hold an address) and the state that pointer holds at runtime (valid address, null, uninitialized, or dangling).

Key practical points to keep in mind: a pointer can legally hold a null value, but dereferencing a null, uninitialized, or dangling pointer is undefined behavior. Pointers should point only to objects of a compatible type unless you intentionally cast. Pointer arithmetic and comparisons are defined only within an array/object and one-past-the-end in specific ways. Prefer initializing pointers when declared, and avoid leaving them indeterminate. For owning relationships, prefer RAII/smart pointers (std::unique_ptr/std::shared_ptr) instead of raw owning pointers.

Quick, actionable tips: initialize pointers (use the null state if nothing valid exists yet), avoid casting integers to pointers, do not dereference freed or out-of-scope objects, and use compiler warnings plus sanitizers or tools (AddressSanitizer, Valgrind) to catch errors. For modern C++ use the explicit null pointer constant instead of older tricks; see the language notes on the null pointer and on undefined behavior for details and examples (nullptr, undefined behavior).

Recommended Answers

All 2 Replies

>>When assigning a pointer variable, it must always point to an address.

Thats incorrect. Hint what about null pointers?

ah, right.

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.