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
12
votes
3 answers

C style strings, Pointers, arrays

I'm having trouble understanding what a C-style string is. Happy early New Year What I know: A pointer holds a memory address. Dereferencing the pointer will give you the data at that memory location. int x = 50; int* ptr = &x; //pointer to an…
DWade64
  • 427
  • 2
  • 4
  • 12
12
votes
4 answers

What's the difference between the types - int * and int *[100] in C?

Kinda of a noob so don't kill me here. What's the difference between the following codes? int *p; //As i understand, it creates a pointer to an variable of size int. int *p[100]; //Don't really know what this is. int (*p)[100]; // I have…
Blessen George
  • 270
  • 3
  • 18
12
votes
3 answers

Dereferencing a map index in Golang

I'm learning Go currently and I made this simple and crude inventory program just to tinker with structs and methods to understand how they work. In the driver file I try to call a method from and item type from the items map of the Cashier type. My…
user1721803
  • 1,125
  • 2
  • 14
  • 21
12
votes
2 answers

is it portable to treat std::vector like array

I have seen people in my team writing code like this. I personally think this is not portable since vector could be implemented in a totally different way. Am I right? vector a; a.push_back(1); a.push_back(2); a.push_back(3); int* b =…
Peaceful
  • 472
  • 5
  • 13
12
votes
6 answers

Where does java reference variable stored?

How does Java's reference variable stored? Is that work similar to C pointer? what I mean by reference variable is myDog in this code Dog myDog = new Dog(); I understood about C pointer, it stores in the heap if global variable, and it stores in…
Taewan
  • 1,167
  • 4
  • 15
  • 25
12
votes
7 answers

C++: auto_ptr + forward declaration?

I have a class like this: class Inner; class Cont { public: Cont(); virtual ~Cont(); private: Inner* m_inner; }; in the .cpp, the constructor creates an instance of Inner with new and the destructor deletes it. This is working pretty…
shoosh
  • 76,898
  • 55
  • 205
  • 325
12
votes
7 answers

C++ allocating dynamic memory in a function - newbie question

I'm investigating a memory leak and from what I see, the problem looks like this: int main(){ char *cp = 0; func(cp); //code delete[] cp; } void func(char *cp){ cp = new char[100]; } At the //code comment, I expected cp to…
user234885
12
votes
8 answers

What's the Difference Between func(int ¶m) and func(int *param)?

In the following code, both amp_swap() and star_swap() seems to be doing the same thing. So why will someone prefer to use one over the other? Which one is the preferred notation and why? Or is it just a matter of taste? #include using…
Srikanth
  • 11,780
  • 23
  • 72
  • 92
12
votes
2 answers

Returning stack variable?

So, I've always been a little fuzzy on C++ pointers vs. ... whatever the other one is called. Like, Object* pointer = new Object(); vs. Object notpointer(); I know that pointers are probably involved in the second one, but basically it's a…
Erhannis
  • 4,256
  • 4
  • 34
  • 48
12
votes
2 answers

arrays and pointer arithmetic ~ clarification needed

I'm doing some experiment about arrays and pointers: int a[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int i = 1, j = 1; int (*p)[3]; p = a; printf ("*(*(a + i) + j) = %d\n", *(*(a + i) + j)); printf ("*(a[i] + j) = %d\n", *(a[i] + j)); printf ("*(a +…
user159
  • 1,343
  • 2
  • 12
  • 20
12
votes
2 answers

`std::enable_if` is function pointer - how?

I want to use SFINAE to enable a particular template if the user passes a function pointer as a parameter. I have googled around but found nothing - I also tried looking at the documentation but couldn't find anything that resembled a…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
12
votes
4 answers

Unable to pass an address of array of type char *[2] to function taking char ***

My XCode (default compiler should be clang?) shows me on this code a warning: Incompatible pointer types passing 'char *(*)[2]' to parameter of type 'char ***' (when calling func) void func (char ***arrayref, register size_t size) { ///…
bwoebi
  • 23,637
  • 5
  • 58
  • 79
12
votes
6 answers

Why is the argument of the copy constructor a reference rather than a pointer?

Why is the argument of the copy constructor a reference rather than a pointer? Why can't we use the pointer instead?
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
12
votes
3 answers

why is array name a pointer to the first element of the array?

Is this always the case , i mean , that array name is always a pointer to the first element of the array.why is it so?is it something implementation kinda thing or a language feature?
chanzerre
  • 2,409
  • 5
  • 20
  • 28
12
votes
6 answers

What does 'return *this' mean in C++?

I'm converting a C++ program to C#, but this part has me confused. What does return *this mean? template< EDemoCommands msgType, typename PB_OBJECT_TYPE > class CDemoMessagePB : public IDemoMessage, public PB_OBJECT_TYPE { (...) virtual…
Kloar
  • 1,109
  • 2
  • 9
  • 25
1 2 3
99
100