wat is the difference b/w ponters and strings? :sad:

Dani AI

Generated

raised the right question and several replies already covered the basics (see and ). ’s example started a useful thread of corrections from and , and clarified the address-of idea. The practical gaps left open are ownership, lifetime, and safe modern usage — those are what matter when you actually write C++ today.

Keep these simple rules in mind:

  • Prefer owning types for text: use std::string to hold text and avoid manual memory management when possible. For read-only, non-owning views use std::string_view so you avoid copies.

  • When passing text, use a const reference or a string view to avoid unnecessary copies:

    void print(const std::string& s);
    void inspect(std::string_view sv);
  • Raw pointers describe “where” something lives but usually do not own it. For ownership, prefer RAII: std::unique_ptr / std::shared_ptr and factory helpers like std::make_unique. That prevents leaks and double-deletes.

Common beginner traps and quick fixes:

  • Don’t leave pointers uninitialized; use nullptr and check before dereferencing.
  • If you need dynamic arrays, prefer std::vector over manual allocation — it correctly manages size and lifetime.
  • When interoperating with C APIs, convert with s.c_str() or construct std::string from a const char* as needed; be careful about null-termination for C-style buffers.
  • If you see crashes or memory issues, look for null or dangling pointers, double delete, or buffer overruns. Use tools like AddressSanitizer or valgrind to find them.

This thread gives good starter definitions; the above focuses on how to use strings and pointers safely and idiomatically in modern C++.

Recommended Answers

All 11 Replies

Member Avatar for Member #46692

wat is the difference b/w ponters and strings? :sad:

A ponter i don't no wat that is :sad:

A string is like a collection of chars

I hope this is helpful :cool:

A Pointer contains an address that points to data.
A string is any finite sequence of characters (i.e., letters, numerals, symbols and punctuation marks).

string ponter = new string;

Now we have a ponter that is a string :)

A string is any finite sequence of characters (i.e., letters, numerals, symbols and punctuation marks).

There are two types of strings -- C and C++.

A c-style string is a null-terminated sequence of characters -- certainly considerably less than infinite! That means that the string ends with the first byte that contains 0.

The C++ string is a class in std library. There are other c++ string classes too, but the standard one is in std namespace.

string ponter = new string;

Now we have a ponter that is a string :)

no its not -- it is still just a pointer, it just contains the address of a c++ class.

string ponter = new string;

Shouldn't that be

string* ponter = new string;

:?:

string ponter = new string;

Now we have a ponter that is a string :)

Sorry for dragging it...but its not even a valid code.

Shouldn't that be

string* ponter = new string;

:?:

Yup...though I have never seen or used it like this before.

Member Avatar for Member #46692

Yup...though I have never seen or used it like this before.

I think it was meant to be a joke :rolleyes:
BTW jwenting is more of a java person, so perhaps that's where the confusion came from.

no its not -- it is still just a pointer, it just contains the address of a c++ class.

In the interest of not confusing the other poster, I'd like to point out that A.D. means 'address of an object [of type string]'.

wat is the difference b/w ponters and strings? :sad:

Mr. taha umer your Question is very simple POINTER is spacial type variable it is contane memory address. And it is move memory to memory location.
your 2nd Question is about array, ARRAY is continous block of memery in cpu. the relation of pointer and arrays is. the starting indux of array have a pointer.

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.