Skip to main content
Stack Overflow for Teams is now Stack Internal: See how we’re powering the human intelligence layer of enterprise AI. Read more >
Filter by
Sorted by
Tagged with
0 votes
0 answers
52 views

I mean that i is incremented by 1 right, then how does ptr + i equals ith block of memory since int size is 4? int i, n; printf("Enter the number of Integers: "); scanf("%d&...
0x0pralad0x0's user avatar
6 votes
1 answer
165 views

In Cppreference the rules for pointer arithmetic include: If P points to the i-th element of an array object x with n elements, given the value of J as j, P is added or subtracted as follows: – P + J ...
Movoid's user avatar
  • 63
5 votes
5 answers
249 views

There are several source code files of a program. File file_a.c has an array in the global scope, and a function is provided that returns a pointer to its beginning: static int buffer[10]; int *...
Evgeny Ilyin's user avatar
3 votes
2 answers
152 views

I'm trying to understand how pointer arithmetic works in C. Say if I have two arrays, one static and the other dynamic like so, int a[10]; int * b = malloc(sizeof(int) * 10); And lets assume each ...
js980's user avatar
  • 35
-1 votes
2 answers
152 views

As far as I understand, pointer arithmetic is defined mainly in expr.add. But it seems defined only for static arrays, not dynamic ones: 4 When an expression J that has integral type is added to or ...
Oersted's user avatar
  • 3,654
3 votes
1 answer
148 views

No matter how hard I try, I can't understand these two quotes in C Standard: If the pointer operand is not null, and the pointer operand and result do not point to elements of the same array object ...
ALICEZzz's user avatar
  • 639
1 vote
1 answer
183 views

Most low-level allocation functions return void *. If I want to perform pointer arithmetic on the obtained memory and/or allocate an array, is this the correct way to do it without invoking undefined ...
Oersted's user avatar
  • 3,654
4 votes
1 answer
144 views

This is a follow-up on this question and my answer #include <new> struct A { int x{42}; int y{24}; }; static_assert(sizeof(A) == 2 * sizeof(int)); static_assert(alignof(A) == alignof(...
Oersted's user avatar
  • 3,654
1 vote
0 answers
90 views

I ran to an over-specialized container somewhere in production code. It was C++98 upgraded to C++11, but I wonder how many UBs (or implementation-defined behaviors) are in it. Basically, without any ...
Swift - Friday Pie's user avatar
1 vote
2 answers
126 views

I have a puzzling problem regarding optimization and pointer equality that seems to happen only in GCC from version 7.1 and higher until the latest GCC version. The Problem can be shown with one ...
S Schulze's user avatar
  • 139
2 votes
4 answers
299 views

I am currently learning about pointers in C programming as part of my training. I am particularly curious about using pointers directly within "for" loops. For example, in the following &...
Mehdi's user avatar
  • 21
2 votes
2 answers
116 views

When I compile this program: /* Create a 3 x 3 2D integer array */ int* matrix = calloc(3 * 3, sizeof(int)); /* This will cause an error since we * can't use double subscript on a * single pointer */...
sunflower's user avatar
3 votes
1 answer
173 views

Pointer arithmetic is only defined behavior for pointers to elements of the same array (or one past the array). Is there any defined behavior to get the distance in memory between two elements that ...
greenlagoon's user avatar
-4 votes
3 answers
242 views

I'm working on code examples that demonstrate how you can "shoot yourself in the foot" using pointers in C++. It's easy to create code that crashes. But now I'm trying to write code that ...
Community College Teacher's user avatar
2 votes
1 answer
143 views

This is in one sense an extension to Is it UB to access a subobject by adding byte offset to the address of the enclosing object? under the assumption that P1839 is adopted. Consider the following ...
LtSten's user avatar
  • 175
4 votes
1 answer
192 views

I wrote a library whose interface references coordinate data from client side. Clients are expected to provide an array of points that each contains 3 contiguous floats. I have decided to use a ...
drex's user avatar
  • 71
1 vote
2 answers
156 views

A pointer of arrays is indexed once to get the array, then indexed again to get the element. This C-style trick should work with {$POINTERMATH ON}, but it definitely doesn't: {$POINTERMATH ON} ... ...
Zoltán Bíró's user avatar
2 votes
2 answers
171 views

If I index an array with an unsigned int or a long, is it being implicitly cast into an int, or is the type somehow maintained? If I use large unsigned int as index, is it cast into a negative int? Is ...
StarEgg's user avatar
  • 51
1 vote
1 answer
83 views

#include <iostream> class ArrayClass { private: int *m_Arr; int i; public: int Size; ArrayClass() : Size(5), i(0), m_Arr(nullptr) { std::cout << "The ...
King Crows Schmétterling's user avatar
-1 votes
1 answer
105 views

Consider the following code: #include <iostream> struct B { char i; B(char i) : i(i) {}; void bar() {}; }; struct D : B { int y; D(char i, int y) : B(i), y(y) {}; }; void ...
tntnkn's user avatar
  • 143
3 votes
4 answers
234 views

Take the following snippet: #include <stddef.h> _Bool foo(char * p) { return (p - 1) == NULL; } Both GCC and LLVM optimize the result to false. What in the standard allows the compilers to ...
Noah's user avatar
  • 1,759
1 vote
2 answers
139 views

#include <stdio.h> int strend(char *s, char *t); int main() { char a[] = "hello world"; char b[] = "orld"; int i = strend(a,b); printf("%d",i); ...
Gary Ong's user avatar
  • 1,098
1 vote
2 answers
124 views

I have seen that in the C++ abstract machines (if the are different), the mere act of forming a point to an invalid point in memory is undefined behavior. For example int* arr = new int[10]; int* last ...
alfC's user avatar
  • 16.7k
0 votes
2 answers
100 views

I've recently been reading the C standard ISO/IEC 9899:2018 specification. Wherein, Section 6.5.6 (Additive operators) describes constraints on the + operator. Rule [8] says: When an expression that ...
Rayan's user avatar
  • 73
2 votes
1 answer
38 views

Initial pgm void assign( int **mat, int n, int m ) { int **p = mat; int **p_end = p + n; for ( ; p < p_end; ++p ) { int *q = *p; int *q_end = q + m; for ( ; q < q_end; ++q ) { printf( ...
hogar's user avatar
  • 33
0 votes
1 answer
74 views

The following code #include <stdio.h> int main() { long long data = 0xFFFEABCD11112345; char *pData = (char *)&data; printf("Value at address %p is %x\n", pData, *...
atta's user avatar
  • 3
1 vote
3 answers
215 views

In my C program book, I meet the question about (*p)[num2] there is a 2 dimension array named a[num1][num2] and a (*p)[num2] next,in the statement for(p=&a[0];p<&a[num1];p++) (*p)[i]=0; ...
阿卡丽's user avatar
0 votes
3 answers
85 views

#include <stdio.h> void main() { int arr[] = {10,20,30,40}; int *p1 = arr; int *p2 = &arr[2]; printf("%d",p2-p1); } Output : 2 I was suprised to see the output ...
Thushar D.M.'s user avatar
1 vote
2 answers
376 views

We have a problem with a firmware project in 8051 assembly. It is used in an embedded system and now needs to be adapted to changes in the hardware. It contains a subroutine that sequentially reads ...
Daniel Jorge's user avatar
4 votes
3 answers
102 views

I am aware that there are other similar questions. I have read through these and don’t think this question has been answered. Can I move between a consecutive (contiguous in the declaration sequence) ...
schuelermine's user avatar
  • 2,390
-1 votes
2 answers
182 views

I was looking for a way to find a given int in my array and i found this solution #include <algorithm> #include <iostream> using namespace std; int main() { int data[] = {23, 45, 56, 12,...
Pryhas's user avatar
  • 19
0 votes
2 answers
52 views

#include<stdio.h> void storeTables(int arr[][10] , int n ,int number); int main() { int tables[2][10]; storeTables(&tables[1],1,2); storeTables(tables,0,3); for(int i = 0 ;...
Tanishk Nagda's user avatar
1 vote
2 answers
333 views

I don't understand how pointers works exactly in arrays. #include <stdio.h> int main() { int x[5] = {1, 2, 3, 4, 5}; int* ptr; // ptr is assigned the address of the third element ptr = ...
Jackob2001's user avatar
2 votes
2 answers
153 views

If I allocated something like size_t n = ???; unsigned char* s = malloc(n); will it be perfectly defined behaviour to compare pointers to locations s + i for 0 <= i < n in the sense that s + ...
Sasha's user avatar
  • 371
0 votes
4 answers
107 views

To learn more of C, I'm trying to recreate basic data structures. Here's a minimal example of my attempt at an array, which compiles and runs but has a problem detected by valgrind: #include <...
Roger Dodger's user avatar
5 votes
2 answers
132 views

I have the following C++ code: void bar(int&); void baz(); void foo(std::vector<int>& v) { int* pointer_to_last = v.data() + (v.size() - 1); if (v.size() > 0 && *...
eyelash's user avatar
  • 3,986
0 votes
5 answers
138 views

Please can someone explain to me this strange pointer behaviour. Did I miss something?!? start first attempt int *foo=(int*)malloc(sizeof(int)*4);//allocates memory for 4 integer with size of sizeof(...
peks's user avatar
  • 27
1 vote
2 answers
336 views

My goal is to have a memory pool non-template class that is used to store arrays of objects. The same memory pool object must be reusable for a different array (difference size, different type and/or ...
Oersted's user avatar
  • 3,654
-4 votes
1 answer
71 views

enter image description here Shouldn't the answer be 2nd ques? ptr1 saves the address of arr[0] and ptr2 saves the address of arr[3]. So (ptr2-ptr1) should be ((34(size of float)) -(04)) i.e., ...
Padmesh Sharma's user avatar
3 votes
1 answer
849 views

Pointers to class member variables can be compared and the results depend on the declaration sequence. See this in the spec For instance this example in compiler explorer is valid and returns true (1):...
doug's user avatar
  • 4,319
0 votes
3 answers
682 views

In C, I have three memory areas that are several hundred bytes long. I want to take the ith pair of 32 bits of the two memory areas, add them as two unsigned 32-bit integers, and store the result in ...
Kevin Stefanov's user avatar
1 vote
3 answers
126 views

In this code, I don't understand why the input character value is being put into (ptr + i) but then we print out ptr[i]. I know they are different because I am printing out the values in the first for ...
TAJ-32's user avatar
  • 11
4 votes
3 answers
409 views

XOR linked lists use pointer arithmetic in a way that looks suspicious to me given the changes in semantics introduced in C++17 (and discussed e.g. in Is a pointer with the right address and type ...
ByteEater's user avatar
  • 1,201
3 votes
3 answers
274 views

So I know that a string is just an array of characters that are stored consecutively in a computer's memory. I also know that in order to find out the location of a string, you just have to go to the ...
King Brain's user avatar
0 votes
2 answers
88 views

im trying to learn pointers and im confused in second line, can someone explain how it works? if we suppose 'a' base address is 100 int a[3][3] = {6, 2, 5, 0, 1, 3, 4, 9, 8}; printf("%p \n&...
Tolga's user avatar
  • 3
1 vote
1 answer
122 views

I am attempting to copy the elements of one dynamically-allocated array (addVec) into another (array) in memory, after passing a pointer to the array from main to a function (changeArray) that assigns ...
Lucky Im's user avatar
2 votes
4 answers
215 views

Recently I learned from user "chux" that it is legal to add 1 to an address that doesn't represent an array element. Specifically, the following provision in the standard (C17 draft, 6.5.6 ¶...
Lover of Structure's user avatar
1 vote
2 answers
87 views

recently, i was curious on how qsort() sorts without passing specific type, thus i'm trying to reverse an array without knowing / caring it's type. i was told you can do pointer arithmetic if you cast ...
youkwhd's user avatar
  • 25
0 votes
1 answer
64 views

Basically, I'm saving some coordinates in 2 arrays, arrayX and arrayY. Then, I want to change the value of a "blank" matrix in the coordinates that are saved in the previously mentioned ...
ATSlooking4things's user avatar
0 votes
2 answers
73 views

What does [] do when called on an int* pointer? E.g. in this code: int* someIntPointer = 4000; //pointer points to byte 4000 in memory ++someIntPointer; //pointer points to byte 4004 in ...
someuser47's user avatar

1
2 3 4 5
16