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
14
votes
1 answer

After p=new string[0] and p=new int[0], why the string version crashes when delete[] p?

I have two blocks of code about new[] and delete[]: 1) #include int main() { std::string *p = new std::string[0]; delete[] p; return 0; } 2) In this case, I merely change std::string to int int main() { int *p = new int[0]; …
Yishu Fang
  • 9,448
  • 21
  • 65
  • 102
14
votes
3 answers

Pass an Objective-C object to a function as a void * pointer

I have a function: myFunction (MyProc callback, void * ref) This function is called from within an Objective-C class. The function is passed a pointer to the callback (a function in the class) and a reference. The reference is necessary because…
James Andrews
  • 3,257
  • 3
  • 31
  • 45
14
votes
5 answers

char and char* (pointer)

I would like to understand how pointers work, so i created this small program. first of all i create a p pointer, which points to a char. The first question is at this point. If i create a pointer, the value of it is a memoryaddress (if i point it…
Iburidu
  • 450
  • 2
  • 5
  • 15
14
votes
6 answers

Memory location of enum value in C

I think I've read somewhere that it is illegal to take the address of an enum value in C (enum values not being lvalues; however, I can't find any information on this now). Is that correct and, if so, why? Edit: Here's an example that clarifies…
olovb
  • 2,164
  • 1
  • 17
  • 20
14
votes
13 answers

Why does C++ use pointers?

Why does C++ need and use pointers? I know they add power to the language but they make it a lot harder to understand for beginners. Languages like F#, Java, Ruby, Python, Lua, etc. get by just fine without them, and they're quite powerful.
RCIX
  • 38,647
  • 50
  • 150
  • 207
14
votes
3 answers

a pointer about *argv[]

This is my main.c ...... int main(int argc, char **argv) { init_arg(&argc, &argv); ...... } This is my init_arg.c ...... void init_arg(int *argc, char ***argv) { printf("%s\n", *argv[1]); ...... } I compiler it with no error and…
thlgood
  • 1,275
  • 3
  • 18
  • 36
13
votes
5 answers

Dereferencing a pointer to out-of-scope static data in C

Disclaimer: This question is strictly academic. The example I'm about to give is probably bad style. Suppose in C I write a subroutine of this form: char *foo(int x) { static char bar[9]; if(x == 0) strcpy(bar, "zero"); else …
user1311045
13
votes
7 answers

C++ strings and pointers

I'm learning C++ and currently I'm working with strings and pointers. I'm following an exercise book and for one of the questions I've created the following: #include #include using namespace std; int main(void){ string *…
Dan
  • 769
  • 2
  • 8
  • 23
13
votes
7 answers

Can we assign a value to a given memory location?

I want to assign some value (say 2345) to a memory location(say 0X12AED567). Can this be done? In other words, how can I implement the following function? void AssignValToPointer(uint32_t pointer, int value) { }
Gopinath
  • 241
  • 1
  • 2
  • 7
13
votes
5 answers

Implications of using an ampersand before a function name in C++?

Given the example: inline string &GetLabel( ) { return m_Label; }; Where m_Label is a private class member variable. The way I think I understand it, this function will return a reference to the variable m_Label. What would be the…
Cuthbert
  • 2,908
  • 5
  • 33
  • 60
13
votes
6 answers

Fastest Array addressing

I am running an image analysis code on an array storing information about the image. Unfortunately the code is very heavy and takes an average of 25s to run through a single frame. The main problem I see is the array addressing. Which is the fastest…
Max Z.
  • 801
  • 1
  • 9
  • 25
13
votes
8 answers

Cannot cast array to pointer

I have the following source: #include using namespace std; void main(int j) { char arr[10][10]; char** ptr; ptr = arr; } when I compile it using VS2010 I get this error: error : a value of type "char (*)[10]" cannot be…
atoMerz
  • 7,534
  • 16
  • 61
  • 101
13
votes
1 answer

Optimistic reads and Locking STM (Software Transactional Memory) with C/C++

I've been doing some research on STM (software transactional memory) implementations, specifically on algorithms that utilize locks and are not dependent on the presence of a garbage collector in order to maintain compatibility with non-managed…
Jason
  • 31,834
  • 7
  • 59
  • 78
13
votes
5 answers

Passing by pointer

I am confused between these two functions: void Swap_byPointer1(int *x, int *y){ int *temp=new int; temp=x; x=y; y=temp; } void Swap_byPointer2(int *x, int *y){ int *temp=new int; *temp=*x; *x=*y; *y=*temp; } Why…
Aan
  • 12,247
  • 36
  • 89
  • 150
13
votes
1 answer

How to recursively dereference pointer (C++03)?

I'm trying to recursively dereference a pointer in C++. If an object is passed that is not a pointer (this includes smart pointers), I just want to return the object itself, by reference if possible. I have this code: template static T…
user541686
  • 205,094
  • 128
  • 528
  • 886