Questions tagged [pointers]

A pointer is a data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.

A pointer is a language construct that allows a program to indirectly access memory: the pointer holds a memory address and provides operations to read and update that address.

The behavior of something called "pointer" depends on the programming language:

  • C provides pointers that may be used to access arbitrary memory locations. While pointers are typed, it is easy to use a pointer of one type to access memory of a different type. It's also possible to store an arbitrary value in a C-style pointer; such a value may or may not reference a valid memory address, and attempting to dereference such a pointer may cause a segmentation fault.
  • C++ provides C-style pointers, and also adds references. A C++ reference appears at the source level to be a simple variable (it does not require explicit dereferencing), but the generated code treats the variable as a pointer.
  • Pascal provides a typed pointer; pointer variables may only be assigned with (1) the result of calling New() with the specified base type, or (2) from another variable of the specified pointer type.
  • Java, does not define a "pointer," but does define a reference that behaves in much the way as a Pascal "pointer."

Example: a C program that creates a pointer to an int value, then accesses that value as if it were a float. This is only possible with languages that support unrestricted, arbitrary pointers:

#include <stdio.h>

int main() {
    int ival;
    float* fptr;

    ival = 1234567890;
    fptr = (float*)&ival;

    printf("%d as a double = %f\n", ival, *fptr);
}
141 questions
178
votes
22 answers

Are null references really a bad thing?

I've heard it said that the inclusion of null references in programming languages is the "billion dollar mistake". But why? Sure, they can cause NullReferenceExceptions, but so what? Any element of the language can be a source of errors if used…
Tim Goodman
  • 2,644
  • 2
  • 28
  • 25
104
votes
4 answers

How is a Java reference different from a C pointer?

C has pointers and Java has what is called references. They have some things in common in the sense that they all point to something. I know that pointers in C store the addresses they point to. Do reference also store the address? How they are…
Gnijuohz
  • 2,035
  • 4
  • 20
  • 18
92
votes
16 answers

Do the young minds need to learn the pointer concepts?

Why did the C master Dennis Ritchie introduce pointers in C? And why did the other programming languages like VB.NET or Java or C# eliminate them? I have found some points in Google, and I want to listen your comments too. Why are they eliminating…
niko
  • 2,119
  • 3
  • 22
  • 19
75
votes
32 answers

What's a nice explanation for pointers?

In your own studies (on your own, or for a class) did you have an "ah ha" moment when you finally, really understood pointers? Do you have an explanation you use for beginner programmers that seems particularly effective? For example, when beginners…
Macneil
  • 8,223
  • 4
  • 34
  • 68
63
votes
10 answers

I never use pointers in my C++ code. Am I coding C++ wrong?

This question may sound strange to you, but I am learning C++ all by myself. I have nobody whom I could ask for mentoring and I would be very glad for some advice. I have started recently to program in C++ (about 3 - 4 intensive months with about 6…
curiouscupcake
  • 627
  • 1
  • 5
  • 7
59
votes
17 answers

What programming problems are best solved by using pointers?

Well, I basically understand how to use pointers, but not how best to use them in order to do better programming. What are good projects or problems to resolve involving the use of pointers so I can understand them better?
dsocolobsky
  • 963
  • 9
  • 15
53
votes
9 answers

Why are pointers not recommended when coding with C++?

I read from somewhere that when using C++ it is recommended not to use pointers. Why is pointers such a bad idea when you are using C++. For C programmers that are used to using pointers, what is the better alternative and approach in C++?
Joshua Partogi
  • 3,845
  • 11
  • 34
  • 43
47
votes
4 answers

In C++ why and how are virtual functions slower?

Can anyone explain in detail, how exactly the virtual table works and what pointers are associated when virtual functions are called. If they are actually slower, can you show the time that the virtual function takes to execute is more than normal…
MdT
  • 589
  • 1
  • 5
  • 6
37
votes
12 answers

int* i; or int *i; or int * i;

What is your favorite method to declare a pointer? int* i; or int *i; or int * i; or int*i; Please explain why. see also: http://www.stroustrup.com/bs_faq2.html#whitespace
Lesmana
  • 1,559
  • 2
  • 15
  • 18
36
votes
1 answer

raw, weak_ptr, unique_ptr, shared_ptr etc... How to choose them wisely?

There is a lot of pointers in C++ but to be honest in 5 years or so in C++ programming (specifically with the Qt Framework) I only use the old raw pointer: SomeKindOfObject *someKindOfObject = new SomeKindOfObject(); I know there are a lot of…
CheshireChild
  • 499
  • 1
  • 5
  • 7
33
votes
19 answers

Will real world applications ever need a 128-bit flat address space?

This is a bit "one megabyte should be enough for anyone", but... A 64-bit flat address space allows up to 4.3ish billion times more space than a 32-bit address space. That's 17,179,869,184 GiB. Obviously, the transition from 8 bits to 16 bits was…
user8709
31
votes
10 answers

What is the "type" of data that pointers hold in the C language?

I know that pointers hold addresses. I know that pointers' types are "generally" known based on the "type" of data they point to. But, pointers are still variables and the addresses they hold must have a data "type". According to my info, the…
Gold_Sky
  • 435
  • 1
  • 4
  • 4
27
votes
11 answers

Isn't the use of pointer variables a memory overhead?

In languages like C and C++, while using pointers to variables we need one more memory location to store that address. So isn't this a memory overhead? How is this compensated? Are pointers used in time critical low memory applications?
Sudip Bhandari
  • 1,137
  • 1
  • 9
  • 14
27
votes
11 answers

Why does void in C mean not void?

In strongly-typed languages like Java and C#, void (or Void) as a return type for a method seem to mean: This method doesn't return anything. Nothing. No return. You will not receive anything from this method. What's really strange is that in C,…
Naftuli Kay
  • 1,601
  • 2
  • 16
  • 23
26
votes
3 answers

Why does a long int take 12 bytes on some machines?

I noticed something strange after compiling this code on my machine: #include int main() { printf("Hello, World!\n"); int a,b,c,d; int e,f,g; long int h; printf("The addresses are:\n %0x \n %0x \n %0x \n %0x \n %0x…
yoyo_fun
  • 2,267
  • 3
  • 17
  • 22
1
2 3
9 10