0

I read pointer as ornament of C that makes C a special language. However, i also used pointers in C++. I guess there are some limitations while using pointer in C++ than in C. As pointers are data types of C. If i am asked to write a program in pure C++, can i use pointers ?

nebula
  • 3,932
  • 13
  • 53
  • 82
  • 5
    C++ *is* an extension of C. It's even a superset of it for the most part (there are a few noticeable features C++ doesn't support, and a few more if you consider C99, but the vast majority of C works unchanged for C++, including pointers). Where did you get contrary information? –  Dec 01 '11 at 17:41
  • I mean to sequel version. Sorry for that. I will edit that one. – nebula Dec 01 '11 at 17:46
  • Actually, pointers came from indirect addressing in assembly language, so they are not really particular to just C. – kfmfe04 Dec 04 '11 at 15:14

2 Answers2

8

Ofcourse you can use pointers in C++. There are some instances where pointers are the only way out. For eg: to use dynamic memory allocations.

One special thing about pointers in C++ is C++ provides Smart pointers which can make your life easier. Prefer them over raw c like pointers.

Bottomline is:
Use what suits your implementation needs. Don't adhere to fixed rules there are none really.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • @Nim: Was just about adding that. – Alok Save Dec 01 '11 at 17:43
  • ...figured it was the first iteration of the answer... :) – Nim Dec 01 '11 at 17:45
  • *C++11* provides smart pointers. C++03 just provides `auto_ptr`. – dan04 Dec 01 '11 at 17:45
  • @Nim: Needed a wee bit to lookup the relevant link actually :) – Alok Save Dec 01 '11 at 17:45
  • 1
    @dan04: The discussion is not about which standard provides which smart pointer or the history of those, for a user migrating from C to C++ it is important to be made aware of the concept of Smart pointers and RAII, I would rather not confuse a new user with historic semantics.But the additional information added is welcome anyways. – Alok Save Dec 01 '11 at 17:47
0

Yep. In fact, for more complex programs, it's quite possible that there simply isn't any other way: if you need to do any kind of dynamic allocation (the kind you do with new), you'll get a pointer.

Also, C++ really is an extension of C. Most (if not all) valid C code is also valid C++ code. That's for example one of the reasons C++ has a struct keyword: it needs to be backwards compatible with C.

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • 1
    From where we are standing right now we ougth to say C++03 is similar to c89 with subtle differences. C99 have diverged a lot. – Abhijit Dec 01 '11 at 17:54