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

What does "void *(*)(void *)" mean in C++?

It's the parameter in pthread_create(). I think each part means: void *: The return value is a void pointer. (*): It's a pointer to a function. (void *): It takes an untyped pointer as a parameter. Is that correct?
Marty
  • 5,926
  • 9
  • 53
  • 91
12
votes
4 answers

32-bit pointers with the x86-64 ISA: why not?

The x86-64 instruction set adds more registers and other improvements to help streamline executable code. However, in many applications the increased pointer size is a burden. The extra, unused bytes in every pointer clog up the cache and might even…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
12
votes
1 answer

How to initialize a vector of pointers

I am working on a C++ program, and I need to initialize a vector of pointers. I know how to initialize a vector, but if someone could show me how to initialize it as a vector filled with pointers that would be great!
Joshua Vaughan
  • 347
  • 3
  • 7
  • 16
12
votes
5 answers

Reference to pointers and C++ polymorphism

does anyone know why this gives a compiler error ? I tried VS 2005 and Codewarrior: class Parent { protected: int m_Var; public: Parent() : m_Var(0) {} virtual ~Parent() {} void PubFunc(); }; class Child : public…
UbiGeek
12
votes
2 answers

Pointer to string array in C

Playing with pointers in C is fun (not really). I have several arrays of strings I want to declare in an easy way, preferably something like: arrayOfStrings1 = {"word1", "word2", etc. }; arrayOfStrings2 = {"anotherword1", "anotherword2", etc.…
Woodgnome
  • 2,281
  • 5
  • 28
  • 52
12
votes
4 answers

Callback function pointers C++ with/without classes

I got stuck. I am trying to form a function that will eat classless function pointers and ones from objects. Here is my current code that hopefully explains more. (It should run on a Arduino, so I cannot use big libraries.) First off, I am using…
Aduen
  • 421
  • 1
  • 4
  • 9
12
votes
2 answers

c++/cli caret^ Vs. Pointer*?

I've programming for a long time in C and in C#, I thought moving to C++ would be smooth... anyway, I've seen the use of caret but I don't understand the meaning of it. what's the different between: std::string *st1; to String::string…
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
12
votes
6 answers

What makes a char * an array of chars?

Normally, if you do the following: int * i = &someint; It's just a pointer to a variable. But, when you do char * str = "somestring"; it automatically turns it into an array. Is it the pointer which is doing this, or is it just syntactic sugar for…
zeboidlund
  • 9,731
  • 31
  • 118
  • 180
12
votes
1 answer

Why do I have to cast to a specific pointer type before calling Dispose?

Let's suppose I have an instance of the TList class (BDS 2006, so this is a list of pointer types). Each pointer I put into the list references memory allocated by the New() function. So when I want to clear the list, I have to iterate through it…
Mariusz Schimke
  • 3,185
  • 8
  • 45
  • 63
12
votes
4 answers

Can I pass constant pointers disguised as arrays?

void foo(const char *s); is equivalent to: void foo(const char s[]); Are there similar equivalents to the following two? void foo(char * const s); void foo(const char * const s);
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
12
votes
5 answers

How can I cast a char to an unsigned int?

I have a char array that is really used as a byte array and not for storing text. In the array, there are two specific bytes that represent a numeric value that I need to store into an unsigned int value. The code below explains the setup. char*…
RLH
  • 15,230
  • 22
  • 98
  • 182
12
votes
3 answers

C++, Need Reason for error : cannot convert parameter 1 from 'char *' to 'const char *&'

Whey we cannot Convert pointer to a character ->TO-> a reference to a pointer to a constant character I am interested in knowing the reason of syntax error when we call foo_ptr. When foo_char is allowed why not foo_ptr. [Update 1.] I would be happy…
anubhav16
  • 238
  • 3
  • 11
12
votes
2 answers

Pointer Arithmetic In C

Consider the following code fragment: int (*p)[3]; int (*q)[3]; q = p; q++; printf("%d, %d\n", q, p); printf("%d\n", q-p); I know that pointer arithmetic is intelligent, meaning that the operation q++ advances q enough bytes ahead to point to a…
Ori Popowski
  • 10,432
  • 15
  • 57
  • 79
12
votes
3 answers

C++: Should I initialize pointer members that are assigned to in the constructor body to NULL?

Suppose I have: // MyClass.h class MyClass { public: MyClass(); private: Something *something_; } // MyClass.cpp MyClass::MyClass() { something_ = new Something(); } Should I initialize something_ to NULL (or 0) in the constructor…
User
  • 62,498
  • 72
  • 186
  • 247
12
votes
5 answers

Pointer dereferencing overhead vs branching / conditional statements

In heavy loops, such as ones found in game applications, there could be many factors that decide what part of the loop body is executed (for example, a character object will be updated differently depending on its current state) and so instead of…
amireh
  • 700
  • 4
  • 13