pointer-arithmetic Questions
6
Solved
It's fairly common knowledge that if you access an element of an array as arr[i] in C that you can also access the element as i[arr], because these just boil down to *(arr + i) and addition is comm...
Million asked 24/8, 2011 at 19:58
1
I'm aware that this question has already been asked and somewhat answered in the past.
However, I've a doubt on a detail that hasn't been cleared out yet (or at least for which I couldn't find a QA...
Primogeniture asked 9/6, 2021 at 15:34
20
Solved
As Joel points out in Stack Overflow podcast #34, in C Programming Language (aka: K & R), there is mention of this property of arrays in C: a[5] == 5[a]
Joel says that it's because of pointer ...
Akmolinsk asked 19/12, 2008 at 17:1
10
When a pointer to a particular type (say int, char, float, ..) is incremented, its value is increased by the size of that data type. If a void pointer which points to data of size x is incremented,...
Coulee asked 19/8, 2010 at 15:5
5
Solved
Regular static allocated array looks like this, and may be accessed using the following formulas:
const int N = 3;
const int M = 3;
int a1[N][M] = { {0,1,2}, {3,4,5}, {6,7,8} };
int x = a1[1][2];...
Harbour asked 12/4, 2022 at 11:20
1
Solved
Below I have adapted code from William Kahan and K.C. Ng (look at the comment block on the bottom) written in 1986 to produce an approximation of 1 / sqrt(x) where x is an IEEE-754 double precision...
Winston asked 16/1, 2022 at 22:6
7
Solved
I was reading a bit in Pointer Arithmetic, and I came upon 2 things I couldn't understand neither know it's use
address_expression - address_expression
and also
address_expression > address_...
Outcaste asked 29/7, 2012 at 23:47
2
Solved
Considering you can (can't think of a great way to put it, but) manipulate pointers in Go, is it possible to perform pointer arithmetic like you would in C, say for iterating over an array? I know ...
Crumpton asked 21/9, 2015 at 17:13
2
Solved
I noticed this warning from Clang:
warning: performing pointer arithmetic on a null pointer
has undefined behavior [-Wnull-pointer-arithmetic]
In details, it is this code which triggers this warni...
Olympe asked 17/1, 2019 at 9:37
4
Solved
I've been reading K & R's book on C, and found that pointer arithmetic in C allows access to one element beyond the end of an array. I know C allows to do almost anything with memory but I just...
Mucky asked 20/6, 2009 at 5:27
2
I know that pointers (to array element) and iterators can be incremented/decremented to walk a sequence of elements and can jump back-and-for elements in the sequence.
But what will happen if I inc...
Import asked 6/12, 2020 at 19:46
3
Solved
This question is based on code that I found that monitors possible memory leaks, so it contains some code that you probably don't want to see in regular programs like ordering pointers.
However, I ...
Glaze asked 7/12, 2020 at 16:40
5
According to the C standard:
When two pointers are subtracted, both shall point to elements of the
same array object, or one past the last element of the array object
(sect. 6.5.6 1173)
[Note: do...
Ihab asked 15/9, 2020 at 15:55
2
Solved
This is more of a curiosity question about C++ that came up after learning how to find the index referenced by an iterator.
Given two vector iterators, why can they be subtracted from each other bu...
Amoy asked 2/9, 2020 at 15:16
2
Here's an example:
#include <cstddef>
#include <iostream>
struct A
{
char padding[7];
int x;
};
constexpr int offset = offsetof(A, x);
int main()
{
A a;
a.x = 42;
char *ptr = (ch...
World asked 11/6, 2020 at 16:27
5
Solved
I'm quite new at working with C++ and haven't grasped all the intricacies and subtleties of the language.
What is the most portable, correct and safe way to add an arbitrary byte offset to a point...
Bisque asked 10/4, 2013 at 18:55
5
I'm practicing pointers and want to substitute pointer operations in place of the arrays to traverse through the elements of the array. I have read numerous articles and cannot grasp this concept. ...
Basie asked 30/9, 2014 at 16:32
2
Solved
It is said in C that when pointers refer to the same array or one element past the end of that array the arithmetics and comparisons are well defined. Then what about one before the first element o...
Roubaix asked 11/2, 2020 at 7:13
1
Solved
I know that pointer arithmetic is disallowed for null pointers. But imagine I have something like this:
class MyArray {
int *arrayBegin; // pointer to the first array item, NULL for an empty arra...
Burnedout asked 19/12, 2019 at 11:39
1
Solved
Is this valid C++?
int main()
{
int i = 0;
int* pi = &i;
++pi;
}
I know that one-past-end pointers are allowed for array types, but I'm not sure in this case. Does that code technically h...
Aerodyne asked 30/11, 2019 at 15:26
2
Solved
I have a code in which I do something that looks like:
double computeSometing(const double * parameters)
{
return useValues(parameters - 10);
// in this special case, useValues only uses values...
Jocelyn asked 22/11, 2019 at 8:17
4
Solved
If I have a simple tensor class like this
struct Tensor
{
double XX, XY, XZ;
double YX, YY, YZ;
double ZX, ZY, ZZ;
}
Is it undefined behavior to use pointer-arithmetic (see below) to access i...
Baskett asked 17/10, 2019 at 8:51
4
Solved
I was reading the following from the C standard:
(6.5.6 Additive operators)
9 When two pointers are subtracted, both shall point to elements of
the same array object, or one past the last el...
Perry asked 6/9, 2019 at 21:26
5
I understand how most of it works, except for the second line in the main function: int* end = array+5;. How does that line work?
#inlcude <iostream>
int main()
{
int array[] = {10, 20, 29,...
Sidwohl asked 6/7, 2019 at 23:23
3
Solved
Going through some C interview questions, I've found a question stating "How to find the size of an array in C without using the sizeof operator?", with the following solution. It works, but I cann...
Empathize asked 15/5, 2019 at 17:3
1 Next >
© 2022 - 2025 — McMap. All rights reserved.