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

Are C functions guaranteed to have a fixed memory address?

If I store a pointer to a function, and then at some later point during my program's execution, compare it to the address of the same function, are the two addresses guaranteed to be equal. E.g. int foo(void){return 0;} int (*foo_p)(void) =…
j b
  • 5,147
  • 5
  • 41
  • 60
12
votes
4 answers

When printf is an address of a variable, why use void*?

I saw some usage of (void*) in printf(). If I want to print a variable's address, can I do it like this: int a = 19; printf("%d", &a); I think, &a is a's address which is just an integer, right? Many articles I read use something like…
Alcott
  • 17,905
  • 32
  • 116
  • 173
12
votes
4 answers

In PHP, what happens in memory when we use mysql_query

I used to fetch large amount of data using mysql_query then iterating through the result one by one to process the data. Ex: $mysql_result = mysql_query("select * from user"); while($row = mysql_fetch_array($mysql_result)){ echo $row['email'] .…
U0001
  • 575
  • 1
  • 6
  • 21
12
votes
3 answers

Is comparing pointers across allocations well-defined in Rust?

I have a pointer buf: *const T pointing to the start of an allocation of n elements of T, and I define the following check: let in_alloc = buf <= ptr && ptr < unsafe { buf.add(n) }; Is it guaranteed that in_alloc is true for any ptr that lies in…
orlp
  • 112,504
  • 36
  • 218
  • 315
12
votes
4 answers

C comparing pointers (with chars)

Good evening, I have 2 functions and each of them accepts as argument a pointer to char: char pointer[255]; func1(char* pointer) { ... memcpy(pointer,some_char,strlen(something)); return; } func2(char* pointer) { ... if (pointer==someother_char)…
cat9
  • 157
  • 1
  • 2
  • 8
12
votes
7 answers

Pointer math vs. Array index

I know this has been hashed over a number of time, but I have come across a case today that shook my understanding of the pointer math/ array index. As I have allways understood it, &mybuff[10] and (&mybuff+10) are equivilent ways of referancing…
Bill N.
  • 835
  • 1
  • 7
  • 13
12
votes
4 answers

Pointer vs array in C, non-trivial difference

I thought I really understood this, and re-reading the standard (ISO 9899:1990) just confirms my obviously wrong understanding, so now I ask here. The following program crashes: #include #include typedef struct { int…
hlovdal
  • 26,565
  • 10
  • 94
  • 165
12
votes
2 answers

Do reference invalidation guarantees automatically apply to pointers?

Consider the following code: std::map m; int &ref = m[0]; int *ptr = &m[0]; m.insert({1,2}); std::cout << ref; // #1 std::cout << *ptr; // #2 For an associative container like std::map, the standard says: The insert and emplace…
cigien
  • 57,834
  • 11
  • 73
  • 112
12
votes
3 answers

Java list : get next or previous element from an identifier

I want to navigate into a list by identifier. 1- I manage/create a list. 2- I create function to get next item of a identifier element from my list Can you help me to fix this code? Prepare the list List myList = new…
BasicCoder
  • 1,661
  • 10
  • 27
  • 42
12
votes
4 answers

Writing a double type value to a text file

The below code is writing unreadable characters to the text file: int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { double value = 11.23444556; char *conversion = (char *)&value; …
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
12
votes
6 answers

Are `x = &v` and `*x = v` equivalent?

int * x; int v = 7; Given this code, what is the difference between 1. x = &v , and 2. *x = v ? I understand that in both cases, *x contains 7 but does x contain memory location of v in both cases? If not, what does x contain in cases 1 and 2,…
12
votes
3 answers

Why does the variable in this loop point at the same memory location?

Consider the below C code. I would have thought that the variable bar was being instantiated every time and would thus point to different addresses in memory, but it doesn't. for (i = 2; i < 7; i++) { struct foo bar; printf("struct %u\n",…
Lucky
  • 627
  • 5
  • 15
12
votes
4 answers

Is the address of a reference to a dereferenced pointer the same as the address of the pointer?

In C++, is the address of a reference to a dereferenced pointer guaranteed to be the same as the address of the pointer? Or, written in code, is the following assertion guaranteed to always hold true? SomeType *ptr = someAddress; SomeType &ref =…
Jesper
  • 121
  • 4
12
votes
3 answers

What is the difference between garbage and dangling references?

What is the difference between garbage and dangling references?
user425243
12
votes
3 answers

Question about converting `void *` to `int` in C

I'm trying to pick my C skills again. I want to sum a sequence in different threads, each thread would return a pointer of the sum of a part of the sequence. However, when I tried to convert the void* type value local_sum to int, problem occurred.…
Frost-Lee
  • 420
  • 1
  • 5
  • 20