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
136
votes
4 answers

How to declare std::unique_ptr and what is the use of it?

I try to understand how std::unique_ptr works and for that I found this document. The author starts from the following example: #include //declarations of unique_ptr using std::unique_ptr; // default construction unique_ptr up;…
Roman
  • 124,451
  • 167
  • 349
  • 456
135
votes
5 answers

Can't understand this way to calculate the square of a number

I have found a function that calculates square of a number: int p(int n) { int a[n]; //works on C99 and above return (&a)[n] - a; } It returns value of n2. Question is, how does it do that? After a little testing, I found that between…
Emanuel
  • 1,333
  • 1
  • 10
  • 11
133
votes
21 answers

Why is address zero used for the null pointer?

In C (or C++ for that matter), pointers are special if they have the value zero: I am adviced to set pointers to zero after freeing their memory, because it means freeing the pointer again isn't dangerous; when I call malloc it returns a pointer…
Joel
  • 3,227
  • 5
  • 21
  • 16
132
votes
10 answers

Should I use char** argv or char* argv[]?

I'm just learning C and was wondering which one of these I should use in my main method. Is there any difference? Which one is more common?
Kredns
  • 36,461
  • 52
  • 152
  • 203
132
votes
8 answers

error: ‘NULL’ was not declared in this scope

I get this message when compiling C++ on gcc 4.3 error: ‘NULL’ was not declared in this scope It appears and disappears and I don't know why. Why? Thanks.
jackhab
  • 17,128
  • 37
  • 99
  • 136
132
votes
6 answers

What does Visual Studio do with a deleted pointer and why?

A C++ book I have been reading states that when a pointer is deleted using the delete operator the memory at the location it is pointing to is "freed" and it can be overwritten. It also states that the pointer will continue to point to the same…
131
votes
2 answers

Can I call memcpy() and memmove() with "number of bytes" set to zero?

Do I need to treat cases when I actully have nothing to move/copy with memmove()/memcpy() as edge cases int numberOfBytes = ... if( numberOfBytes != 0 ) { memmove( dest, source, numberOfBytes ); } or should I just call the function without…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
130
votes
3 answers

Why does this code segfault on 64-bit architecture but work fine on 32-bit?

I came across the following C puzzle: Q: Why does the following program segfault on IA-64, but work fine on IA-32? int main() { int* p; p = (int*)malloc(sizeof(int)); *p = 10; return 0; } I know that the size of int on…
user7
  • 2,339
  • 5
  • 25
  • 29
130
votes
15 answers

Placement of the asterisk in pointer declarations

I've recently decided that I just have to finally learn C/C++, and there is one thing I do not really understand about pointers or more precisely, their definition. How about these examples: int* test; int *test; int * test; int* test,test2; int…
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
129
votes
15 answers

Why aren't pointers initialized with NULL by default?

Can someone please explain why pointers aren't initialized to NULL? Example: void test(){ char *buf; if (!buf) // whatever } The program wouldn't step inside the if because buf is not null. I would like to know why, in what…
Jonathan
  • 4,724
  • 7
  • 45
  • 65
128
votes
5 answers

How to increment a pointer address and pointer's value?

Let us assume, int *p; int a = 100; p = &a; What will the following code actually do and how? p++; ++p; ++*p; ++(*p); ++*(p); *p++; (*p)++; *(p)++; *++p; *(++p); I know, this is kind of messy in terms of coding, but I want to know what will…
Dinesh
  • 16,014
  • 23
  • 80
  • 122
128
votes
5 answers

Pointers, smart pointers or shared pointers?

I am programming with normal pointers, but I have heard about libraries like Boost that implement smart pointers. I have also seen that in Ogre3D rendering engine there is a deep use of shared pointers. What exactly is the difference between the…
tunnuz
  • 23,338
  • 31
  • 90
  • 128
126
votes
11 answers

Why do linked lists use pointers instead of storing nodes inside of nodes

I've worked with linked lists before extensively in Java, but I'm very new to C++. I was using this node class that was given to me in a project just fine class Node { public: Node(int data); int m_data; Node *m_next; }; but I had one…
m0meni
  • 16,006
  • 16
  • 82
  • 141
126
votes
5 answers

What's the point of having pointers in Go?

I know that pointers in Go allow mutation of a function's arguments, but wouldn't it have been simpler if they adopted just references (with appropriate const or mutable qualifiers). Now we have pointers and for some built-in types like maps and…
anon
  • 1,287
  • 2
  • 9
  • 3
124
votes
5 answers

Difference between *ptr += 1 and *ptr++ in C

I just started to study C, and when doing one example about passing pointer to pointer as a function's parameter, I found a problem. This is my sample code : #include #include #include int* allocateIntArray(int*…
huy nguyen
  • 1,381
  • 2
  • 11
  • 18