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

pointers - duplicate object instance

If I have Class *a1 = new Class(); Class *b1 = a1; delete b1; delete a1; //this will give a double free or corruption message; if I delete pointer b, it's the same as deleting pointer a right? Since the two are pointing at the same instance of…
tambalolo
  • 1,035
  • 3
  • 14
  • 30
13
votes
6 answers

Malloc compile error: a value of type "int" cannot be used to initialize an entity of type int (*)[30]

I must have tried 20 ways of doing this by now. I really need help, no matter what I do i get a error similar to this one. a value of type "int" cannot be used to initialize an entity of type "int (*)[30]" i.e. this will get me such an…
WIllJBD
  • 6,144
  • 3
  • 34
  • 44
13
votes
6 answers

Array are passed by value or by reference?

I know for sure that function(int *a); function(int a[]); in C are the same, function(int a[]) will be translated into function(int *a) int *a = malloc(20); int b[] = {1,2,3,4,5}; These two are not the same, the first is a pointer, the second is…
AR89
  • 3,548
  • 7
  • 31
  • 46
13
votes
3 answers

Size of pointers: Dependent factors

I am finding difficulties in understanding the factors on which the size of pointer variables in C is dependent on. I checked few references, the only information I got until now is pointer size is dependent on the processor architecture.I would…
Vivek Maran
  • 2,623
  • 5
  • 38
  • 52
13
votes
5 answers

Pointer to array of base class, populate with derived class

If I have a base class, with only virtual methods and 2 derived classes from the base class, with those virtual methods implemented. How do I: // causes C2259 BaseClass* base = new BaseClass[2]; BaseClass[0] = new FirstDerivedClass; …
Deukalion
  • 2,516
  • 9
  • 32
  • 50
13
votes
5 answers

Which is faster, pointer access or reference access?

In the example code below I allocate some instances of the struct Chunk. In the for loops I then iterate through the memory block and access the different instances by using either pointer or references, and assign them some random data. But which…
Michael Mancilla
  • 145
  • 1
  • 2
  • 8
13
votes
3 answers

cast char array to integer

#include int main(){ unsigned char a[4] = {1, 2, 3, 4}; int b = *(int *)&a[0]; printf("%d\n", b); return 0; } I just cannot understand why the result of b is 0x4030201. Could someone help me out?
Learner
  • 141
  • 1
  • 1
  • 6
13
votes
5 answers

delphi pointer address

In Delphi: How do I get the address (0x2384293) a pointer points to? var iValue := Integer; iptrValue := PInteger; implementation procedure TForm1.Button1Click(Sender: TObject); begin iptrValue := @iValue; iValue := 32342; //Should…
Acron
  • 1,378
  • 3
  • 20
  • 32
13
votes
7 answers

C++: member pointer initialised?

Code sample should explain things: class A { B* pB; C* pC; D d; public : A(int i, int j) : d(j) { pC = new C(i, "abc"); } // note pB is not initialised, e.g. pB(NULL) ... }; Obviously pB should be…
DDD
  • 1,462
  • 15
  • 19
13
votes
3 answers

std::vector of functions

I want a std::vector to contain some functions, and that more functions can be added to it in realtime. All the functions will have a prototype like this: void name(SDL_Event *event); I know how to make an array of functions, but how do I make a…
Ulrik
13
votes
1 answer

Passing a string to file.open();

I am used to higher level languages (java, python etc.), where this is dead obvious. I am trying to pass a string the user inputs to cin, the name of a file to open. There appears to be some sort of pointer madness error, and my code will not…
Muricula
  • 1,174
  • 3
  • 9
  • 16
13
votes
3 answers

dereferencing pointer to integer array

I have a pointer to integer array of 10. What should dereferencing this pointer give me? Eg: #include main() { int var[10] = {1,2,3,4,5,6,7,8,9,10}; int (*ptr) [10] = &var; printf("value = %u %u\n",*ptr,ptr); //both print…
chappar
  • 7,275
  • 12
  • 44
  • 57
12
votes
7 answers

C address of an address of a variable

Is the only way of getting the address of an address in C (opposite to double dereference to have an intermediate variable? e.g. I have: int a; int b; int *ptr_a; int *ptr_b; int **ptr_ptr_a; a = 1; ptr_a = &a; ptr_ptr_a = &(&a); <- compiler says…
bph
  • 10,728
  • 15
  • 60
  • 135
12
votes
3 answers

How to concat two char * in C?

I receive a char * buffer which have the lenght of 10. But I want to concat the whole content in my struct which have an variable char *. typedef struct{ char *buffer; //.. }file_entry; file_entry real[128]; int fs_write(char *buffer, int…
Valter Silva
  • 16,446
  • 52
  • 137
  • 218
12
votes
3 answers

Delphi passing parameters by reference or by value/copy

Context 1 var text:String; text:='hello'; myFunc(text); Context2 function myFunc(mytext:String); var textcopy:String; begin textcopy:=mytext; end; myFunc on the Context2 was called from the Context1, the local variable mytext is pointing…
Vitim.us
  • 20,746
  • 15
  • 92
  • 109