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
61
votes
5 answers

Function pointer as a member of a C struct

I have a struct as follows, with a pointer to a function called "length" that will return the length of the chars member. typedef struct pstring_t { char * chars; int (* length)(); } PString; I have a function to return the length of the…
Jonathan Sterling
  • 18,320
  • 12
  • 67
  • 79
59
votes
6 answers

What is the difference between far pointers and near pointers?

Can anybody tell me the difference between far pointers and near pointers in C?
Madhan
  • 2,609
  • 7
  • 26
  • 22
59
votes
9 answers

Address of a temporary in Go?

What's the cleanest way to handle a case such as this: func a() string { /* doesn't matter */ } b *string = &a() This generates the error: cannot take the address of a() My understanding is that Go automatically promotes a local variable to…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
58
votes
5 answers

Function pointers and address of a function

So I figured when making function pointers, you do not need the operator & to get the address of the initial function: #include double foo (double x){ return x*x; } int main () { double (*fun1)(double) = &foo; double…
mmirzadeh
  • 6,893
  • 8
  • 36
  • 47
58
votes
9 answers

Smart pointers/safe memory management for C?

I, and I think many others, have had great success using smart pointers to wrap up unsafe memory operations in C++, using things like RAII, et cetera. However, wrapping memory management is easier to implement when you have destructors, classes,…
mannicken
  • 6,885
  • 4
  • 31
  • 38
58
votes
5 answers

Increment void pointer by one byte? by two?

I have a void pointer called ptr. I want to increment this value by a number of bytes. Is there a way to do this? Please note that I want to do this in-place without creating any more variables. Could I do something like ptr = (void *)(++((char *)…
Adam S
  • 8,945
  • 17
  • 67
  • 103
58
votes
17 answers

Could I ever want to access the address zero?

The constant 0 is used as the null pointer in C and C++. But as in the question "Pointer to a specific fixed address" there seems to be some possible use of assigning fixed addresses. Is there ever any conceivable need, in any system, for whatever…
Joel
  • 3,227
  • 5
  • 21
  • 16
58
votes
3 answers

Reference to non-static member function must be called

I'm using C++ (not C++11). I need to make a pointer to a function inside a class. I try to do following: void MyClass::buttonClickedEvent( int buttonId ) { // I need to have an access to all members of MyClass's class } void MyClass::setEvent()…
JavaRunner
  • 2,455
  • 5
  • 38
  • 52
58
votes
12 answers

Does Java have pointers?

If Java does not have pointers, then what does the the new keyword do in Java?
aruna
  • 589
  • 1
  • 5
  • 3
58
votes
8 answers

Printing pointers in C

I was trying to understand something with pointers, so I wrote this code: #include int main(void) { char s[] = "asd"; char **p = &s; printf("The value of s is: %p\n", s); printf("The direction of s is: %p\n", &s); …
alcuadrado
  • 8,340
  • 3
  • 24
  • 25
58
votes
8 answers

Are all data pointers the same size in one platform for all data types?

Are char*, int*, long* or even long long* of same size (on a given platform)?
c-stranger
  • 691
  • 1
  • 7
  • 13
58
votes
3 answers

dereferencing a pointer when passing by reference

what happens when you dereference a pointer when passing by reference to a function? Here is a simple example int& returnSame( int &example ) { return example; } int main() { int inum = 3; int *pinum = & inum; std::cout << "inum: " << …
MWright
  • 1,681
  • 3
  • 19
  • 31
58
votes
2 answers

Arrow Operator vs. Dot Operator

It seems to me that C's arrow operator (->) is unnecessary. The dot operator (.) should be sufficient. Take the following code: typedef struct { int member; } my_type; my_type foo; my_type * bar; int val; val = foo.member; val =…
remline
  • 605
  • 1
  • 6
  • 3
57
votes
6 answers

NULL pointer with boost::shared_ptr?

What's the equivalent to the following: std::vector vec; vec.push_back(NULL); when dealing with boost::shared_ptr? Is it the following code? std::vector< boost::shared_ptr > vec; vec.push_back(boost::shared_ptr()); Note: I may push…
Frank
57
votes
5 answers

How to print variable addresses in C?

When i run this code. #include void moo(int a, int *b); int main() { int x; int *y; x = 1; y = &x; printf("Address of x = %d, value of x = %d\n", &x, x); printf("Address of y = &d, value of y = %d, value of *y…
nambvarun
  • 1,201
  • 4
  • 13
  • 14