Questions tagged [pointers]

Data types for "pointing" at other values: A pointer's value is a memory address where the pointed-to value is stored. This tag should be used for questions involving the use of pointers, not references. Common programming languages using pointers are C, C++, Go, and assembly and intermediate-representation languages; use a specific language tag. Other helpful tags should describe what is being pointed-to (e.g. a function, a struct etc.)

A pointer is a data type that "points to" another value stored in memory using its address. Using a pointer rather than the pointed-to entity often holds performance benefits in repetitive operations. For example, copying a pointer is very often "cheaper" than copying the value that it points to: The pointee may be a large data structure, or its copying may be non-trivial, involving memory-location dependent values, while with a pointer - only the address must be copied.

Pointers are an important concept in many high-level programming languages, including C and C++. The Wikipedia page on pointers has a fairly in-depth introduction to the concept.

Please consult some of the following content for a more in-depth explanation of pointers.

Books

See also

56155 questions
81
votes
3 answers

Why are NULL pointers defined differently in C and C++?

In C, NULL is defined as (void *)0 whereas in C++ it is 0. Why is it so? In C I can understand that if NULL is not typecast to (void *) then compilers may/may not generate warning. Other than this, is there any reason?
mohit
  • 1,087
  • 1
  • 9
  • 18
81
votes
8 answers

How to access the contents of a vector from a pointer to the vector in C++?

I have a pointer to a vector. Now, how can I read the contents of the vector through pointer?
Pavan Dittakavi
  • 3,013
  • 5
  • 27
  • 47
81
votes
7 answers

Can I use NULL as substitution for the value of 0?

Am I allowed to use the NULL pointer as replacement for the value of 0? Or is there anything wrong about that doing? Like, for example: int i = NULL; as replacement for: int i = 0; As experiment I compiled the following code: #include…
81
votes
13 answers

Is NULL always false?

Is it safe to assume that NULL always translates to false in C? void *somePtr = NULL; if (!somePtr) { /* This will always be executed? */ } Or should an explicit check against the value of NULL be made?
Sydius
  • 13,567
  • 17
  • 59
  • 76
81
votes
10 answers

Addresses of two char pointers to different string literals are same

#include #include int main() { char * p = "abc"; char * p1 = "abc"; printf("%d %d", p, p1); } When I print the values of the two pointers, it is printing the same address. Why?
seereddi sekhar
  • 881
  • 9
  • 20
81
votes
6 answers

Are there pointers in javascript?

I used C++ before and I realized that pointers were very helpful. Is there anything in javascript that acts like a pointer? Does javascript have pointers? I like to use pointers when I want to use something like: var a = 1; var b =…
Dylan Vander Berg
  • 1,809
  • 1
  • 22
  • 37
81
votes
7 answers

Meaning of "referencing" and "dereferencing" in C

I read different things on the Internet and got confused, because every website says different things. I read about * referencing operator and & dereferencing operator; or that referencing means making a pointer point to a variable and dereferencing…
Milkncookiez
  • 6,817
  • 10
  • 57
  • 96
80
votes
7 answers

When should I use raw pointers over smart pointers?

After reading this answer, it looks like it is a best practice to use smart pointers as much as possible, and to reduce the usage of "normal"/raw pointers to minimum. Is that true?
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
80
votes
3 answers

What does P::************ mean in Boost assert.hpp file?

In boost/mpl/assert.hpp, I saw something like this: template struct eval_assert { typedef typename extract_assert_pred::type P; typedef typename P::type p_type; typedef typename ::boost::mpl::if_c
stoneyan
  • 871
  • 6
  • 8
80
votes
13 answers

Reference type in C#

Consider this code: public class Program { private static void Main(string[] args) { var person1 = new Person { Name = "Test" }; Console.WriteLine(person1.Name); Person person2 = person1; person2.Name =…
user1968030
79
votes
3 answers

how does the ampersand(&) sign work in c++?

Possible Duplicate: What are the differences between pointer variable and reference variable in C++? This is confusing me: class CDummy { public: int isitme (CDummy& param); }; int CDummy::isitme (CDummy& param) { if (¶m == this) { …
infinitloop
  • 2,863
  • 7
  • 38
  • 55
79
votes
5 answers

When to use pointers in C#/.NET?

I know C# gives the programmer the ability to access, use pointers in an unsafe context. But When is this needed? At what circumstances, using pointers becomes inevitable? Is it only for performance reasons? Also why does C# expose this…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
78
votes
7 answers

Does const-correctness give the compiler more room for optimization?

I know that it improves readability and makes the program less error-prone, but how much does it improve the performance? And on a side note, what's the major difference between a reference and a const pointer? I would assume they're stored in the…
slartibartfast
  • 4,348
  • 5
  • 31
  • 46
78
votes
7 answers

Passing pointers between C and Java through JNI

At the moment, i'm trying to create a Java-application which uses CUDA-functionality. The connection between CUDA and Java works fine, but i've got another problem and wanted to ask, if my thoughts about it are correct. When i call a native function…
Volker
  • 783
  • 1
  • 6
  • 6
78
votes
7 answers

Difference between passing array, fixed-sized array and base address of array as a function parameter

I am confused about which syntax to use if I want to pass an array of known or unknown size as a function parameter. Suppose I have these variants for the purpose: void func1(char* str) { //print str } void func2(char str[]) { //print…
mr5
  • 3,438
  • 3
  • 40
  • 57