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

assignment discards 'volatile' qualifier from pointer target type

I have been working with a microprocessor to read the temperature from a sensor and have run into the following warning regarding a volatile declaration. "assignment discards 'volatile' qualifier from pointer target type" I was receiving a single…
rayray
  • 301
  • 2
  • 3
  • 8
14
votes
12 answers

Pointer to [-1]th index of array

How does a pointer points to [-1]th index of the array produce legal output everytime. What is actually happening in the pointer assignment? #include int main() { int realarray[10]; int *array = &realarray[-1]; …
manav m-n
  • 11,136
  • 23
  • 74
  • 97
14
votes
1 answer

64 bit function returns 32 bit pointer

This function is buried in a complex nest so actually finding the cause is probably beyond anything I can ask, but I'm wondering if anyone might be able to give some tips on how I might go about debugging this. Here is the gist of the code I'm…
DavidG
  • 203
  • 1
  • 3
  • 6
14
votes
1 answer

malloc: *** error for object: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug

Can someone help me figure out where I'm getting this error. I know it's probably a double deletion or something like this. For the background this is an implementation of the huffman's tree as you can easily realize on wikipedia. CharCountNode…
waldyr.ar
  • 14,424
  • 6
  • 33
  • 64
14
votes
6 answers

In C, does the meaning of A[i][j] depend on how A is declared?

Suppose I have a two-dimensional array grid declared as double grid[5][5]. It is my understanding that the following statements are true: when grid is declared, a contiguous block of memory is allocated for 5*5 doubles, no more no less; when an…
sp00n
  • 181
  • 1
  • 11
14
votes
1 answer

What does typedef void* key_type mean in C?

I am a little confused with the following line of code in C: typedef void* key_type; does it mean that key_type is a void pointer? I guess it's a more intuitive naming than void* in C programming. - Thank you
TonyW
  • 18,375
  • 42
  • 110
  • 183
14
votes
2 answers

"current" in Linux kernel code

As I was going through the below chunk of Linux char driver code, I found the structure pointer current in printk. I want to know what structure the current is pointing to and its complete elements. What purpose does this structure serve? ssize_t…
Sagar Jain
  • 7,475
  • 12
  • 47
  • 83
14
votes
1 answer

time.Time: pointer or value

The Go docs say (emphasis added): Programs using times should typically store and pass them as values, not pointers. That is, time variables and struct fields should be of type time.Time, not *time.Time. A Time value can be used by multiple…
Mzzzzzz
  • 4,770
  • 7
  • 30
  • 47
14
votes
2 answers

Most efficient pointer arithmetic type in c

I assume that an internal casting happens when we write: arr[i] (which is equivalent to *(arr+i)). Because i can for example be a short, int or long or the unsigned variant of any of these three. So my question is simple: which type should i be so…
makhlaghi
  • 3,856
  • 6
  • 27
  • 34
14
votes
5 answers

Triple pointers in C: is it a matter of style?

I feel like triple pointers in C are looked at as "bad". For me, it makes sense to use them at times. Starting from the basics, the single pointer has two purposes: to create an array, and to allow a function to change its contents (pass by…
darda
  • 3,597
  • 6
  • 36
  • 49
14
votes
1 answer

C++: Correctly overriding virtual function in template class

Consider following code: template struct MyTempl { virtual void doStuff(const T &value) = 0; }; struct MyImpl : MyTempl { void doStuff(const int &value) {} }; struct MyPtrImpl : MyTempl { void doStuff(const int*…
Ancurio
  • 1,698
  • 1
  • 17
  • 32
14
votes
4 answers

what does compiler do with a[i] which a is array? And what if a is a pointer?

I was told by c-faq that compiler do different things to deal with a[i] while a is an array or a pointer. Here's an example from c-faq: char a[] = "hello"; char *p = "world"; Given the declarations above, when the compiler sees the expression…
ibread
  • 1,165
  • 1
  • 10
  • 18
14
votes
2 answers

Why is h_addr_list in struct hostent a char ** instead of struct in_addr **?

I am new to network programming. The following structure definitions are quite confusing to me. Here h_addr_list is a defined as string array, but it is used to store array of in_addr structures. Why didn't it define as struct in_addr **h_addr_list…
noufal
  • 940
  • 3
  • 15
  • 32
14
votes
8 answers

Will GCC inline a function that takes a pointer?

I have a function which operates on piece of data (let's say, an int), and I want to change it in place by passing a reference to the valule. As such, I have the function: void myFunction(int *thing) { ... }. When I use it I call it thus:…
Joe
  • 46,419
  • 33
  • 155
  • 245
14
votes
5 answers

Creating weak_ptr<> from raw pointer

I'd like to wrap raw pointer member to some smart pointer to prevent deleting inside a developing class. Owner of the object under pointer is outside of class. So, looks like boost::shared_ptr and std::auto_ptr does not fit. The following is a…
Loom
  • 9,768
  • 22
  • 60
  • 112