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
4 answers

Why is this implicit conversion (between different pointer types) valid?

I found myself in the following situation: #include typedef struct T1 { int id; } T1; typedef struct T2 { int id; } T2; void f(T1 *ptr) { printf("f called\n"); } int main(void) { T2 obj; T2 *ptr = &obj; f(ptr); //…
Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
13
votes
3 answers

Usage of void pointers across different platforms

I have heard that pointers should first be cast to void to ensure consistency of values across different platforms and should use %p format specifier. Why is it and what exactly are the problems? int x=100; int *pi=&x; printf("value of pi is:…
Naseer
  • 4,041
  • 9
  • 36
  • 72
13
votes
3 answers

Call a void* as a function without declaring a function pointer

I've searched but couldn't find any results (my terminology may be off) so forgive me if this has been asked before. I was wondering if there is an easy way to call a void* as a function in C without first declaring a function pointer and then…
jay.lee
  • 19,388
  • 8
  • 39
  • 38
13
votes
3 answers

Casting pointer to object to void * in C++

I've been reading StackOverflow too much and started doubting all the code I've ever written, I keep thinking "Is that undefined behavour?" even in code that has been working for ages. So my question - Is it safe and well defined behavour to cast a…
jcoder
  • 29,554
  • 19
  • 87
  • 130
13
votes
6 answers

Passing pointers/references to structs into functions

This is going to sound like a silly question, but I'm still learning C, so please bear with me. :) I'm working on chapter 6 of K&R (structs), and thus far through the book have seen great success. I decided to work with structs pretty heavily, and…
John Rudy
  • 37,282
  • 14
  • 64
  • 100
13
votes
3 answers

C++ - Difference between (*). and ->?

Is there any difference in performance - or otherwise - between: ptr->a(); and (*ptr).a(); ?
Rachel
  • 2,181
  • 2
  • 18
  • 20
13
votes
2 answers

Is the strict aliasing rule really a "two-way street"?

In these comments user @Deduplicator insists that the strict aliasing rule permits access through an incompatible type if either of the aliased or the aliasing pointer is a pointer-to-character type (qualified or unqualified, signed or unsigned char…
13
votes
6 answers

Swift: Pass Uninitialized C Structure to Imported C function

I'm aware of this answer, but this is not the same thing - thats passing a pointer to be initialised with an allocation. I'm interfacing with a C library that has the following structure definition: typedef struct myStruct { unsigned char var [50];…
Dave Meehan
  • 3,133
  • 1
  • 17
  • 24
13
votes
3 answers

Swift use c struct

Sorry for the title, I can't find words to describe my question in few words. I already know that swift can use struct written in c. For example In Bridging-Header.h typedef struct { int x; int y; } Pointer; then I can use Pointer…
galilio
  • 307
  • 1
  • 3
  • 13
13
votes
1 answer

Assigning a type uintptr to uint64 in Golang

I'm trying to assign the value found in a variable of type uintptr to a uint64 variable in Go. Using myVar = valFromSystem gives me cannot use valFromSystem (type uintptr) as type uint64 in assignment And trying myVar = *valFromSystem gives…
Bart Silverstrim
  • 3,445
  • 6
  • 34
  • 44
13
votes
1 answer

ALLOCATABLE arrays or POINTER arrays?

I am writing a new code in Fortran and hesitating between using allocatable arrays or pointer arrays. I read somewhere that allocatable arrays have significant advantages over pointer arrays: 1) More efficient because they are always contiguous in…
13
votes
3 answers

What is the difference between int* x[n][m] and int (*x) [n][m]?

As I see it int *x[n][m] declares x to be a 2-d array of pointers to integers, so allocating memory should be as easy as x[i][j] = new int and as expected it works fine. Now if I change the declaration to: int (*x)[n][m] x[i][j] = new int no longer…
Rakib Ansary
  • 4,078
  • 2
  • 18
  • 29
13
votes
1 answer

How to fix warning 'no explicit ownership'

I have method that takes indirect pointer as argument and then, if error, set it to error object. I'm trying to turn on as many warning as possible. But one of them - Implicit ownership types on out parameters - generates warning in this method: -…
Ossir
  • 3,109
  • 1
  • 34
  • 52
13
votes
6 answers

Reference vs. pointer

What is the difference? Because this: int Value = 50; int *pValue = &Value; *pValue = 88; and ref version do the same: int Value = 50; int &rValue = Value; rValue = 88; Which one is better to use? Thanks.
Quest
  • 2,764
  • 1
  • 22
  • 44
13
votes
3 answers

Uninitialised value was created by a heap allocation

I have been chasing this bug around, and I just don't get it. Have I forgotten some basic C or something? ==28357== Conditional jump or move depends on uninitialised value(s) ==28357== at 0x4C261E8: strlen (mc_replace_strmem.c:275) ==28357== …
yavoh
  • 2,645
  • 5
  • 24
  • 21