Questions tagged [pointer-arithmetic]
You can perform a limited number of arithmetic operations on pointers. These operations are: increment, decrement, addition, subtraction, comparison and assignment.
653
questions
1
vote
1
answer
72
views
What's Difference Between p and p[x] (Pointers in C)
#include <stdio.h>
int main(){
int a[] = {1,2,3};
int *p = &a;
}
p = adress of a variable.
*p = The value in that adress.
I understand this but why p is memory adress but p[x] isn't?
For ...
0
votes
1
answer
54
views
What happens when we do (char *) casting to store integer data into char pointer?
int a;
(&a+1) -&a: 1
(char*)(&a+1) -(char*)&a: 4
Could you please explain why we got a different result when we did (char *) casting?
I compiled the code and found that the addresses ...
0
votes
4
answers
80
views
Why char pointer increments by one byte though its size is 8?
void main() {
char var = 10;
char *ptr = &var;
printf("Pointer address before increment:%p\n", ptr);
printf("sizeof(ptr):%d\n", sizeof(ptr));
ptr++;
...
0
votes
0
answers
42
views
Does reinterpret_cast together with binary input/output always leads to undefined behavior?
When working binary input/output, we often see/write code similar to this
in_file.read(reinterpret_cast<char *>(&obj), sizeof(obj))
out_file.write(reinterpret_cast<const char*>(&...
1
vote
4
answers
91
views
Is the use of (arr+2)[0] right?
I am a student who will be a freshman in an university after this summer vacation.I want to learn about computer programing in advance but I run into some problems.
Why when I run the program in devc++...
0
votes
1
answer
35
views
dereferencing 2D array using arithmetic
int main(void)
{
short arr[3][2]={3,5,11,14,17,20};
printf("%d %d",*(arr+1)[1],**(arr+2));
return 0;
}
Hi. In above code as per my understanding ,*(arr+1)[1] is equivalent to *...
2
votes
1
answer
129
views
Pointer-arithmetic on contiguous non-array objects
std::vector is generally considered to be unimplementable pre-C++20 (as discussed in P0593), as the elements cannot be placed in an internal array while adhering to the required performance ...
0
votes
1
answer
89
views
Subtract addresses – Warning: “differs in levels of indirection”
Why do I receive a warning? pch is already the pointer I got and when I want to subtract the addresses I use &Origi for that.
C4047 '-' : 'char*' differs in levels of indirection from 'char (*)[...
0
votes
3
answers
143
views
Remove subarray using pointer arithmetic
I need to make a function for removing subarray using pointer arithmetic in C. Function should return number of removed elements. Auxiliary arrays are not allowed.
#include <stdio.h>
int ...
-2
votes
3
answers
72
views
Understanding Pointer
I have this problem and I don't understand what this means. I ask my questions in the comments.
int x = 3;
int *y = &x;
int z = *y;
printf("%d", z); //why does this give me the ...
0
votes
1
answer
70
views
"C" Trying to understand **pointer and how to access all values of value[][]
#include "stdio.h"
int main()
{
int *pI, *values[2];
int i1[] = {1, 2}, i2[][2] = {{5, 10}, {20, 30}};
// SOME LINES ARE EDITED, AFTER READING COMMENTS
*values = i2[*i1]; ...
0
votes
0
answers
47
views
C Finding average of a cell's neighbors in a dynamic matrix
I need to write functions which receives a matrix applied as an array
static 2D and sizes. The function must assign a new two-dimensional array - a matrix B
which has a Fraction type and contains the ...
0
votes
1
answer
71
views
C++ structure reference from member reference
Given the following setup...
struct A {unsigned char _data;};
struct B {unsigned char _data;};
struct C {A a; B b;};
// in this context (ar) is known to be the "a" of some C instance
A& ...
4
votes
5
answers
118
views
How C/C++ compiler distinguish regular two dimensional array and array of pointers to arrays?
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]; // ...
2
votes
2
answers
50
views
Pointer seems not be sync with printf's output
I have trouble understanding what pointer2 contains. The second printf prints llo World, but the third one prints Hey you guys!. Why would it be like that if strcpy copies y you guys!\n into llo World....
1
vote
1
answer
59
views
Why does a printf statement in a for loop seem to depend on an unrelated previous printf outside that loop?
I was playing around in C to implement the "sieve of Eratosthenes" for finding primes. I came up with the following code:
#include <stdio.h>
#include <stdlib.h>
void ...
1
vote
0
answers
74
views
Understanding UB and pointer-arithmetic
I have a generic reference-counted heap-allocated wrapper class. So my class is basically just a pointer:
template <typename T>
class Refcounted {
struct model {
std::atomic<std::...
1
vote
1
answer
53
views
Remove all digits from array
Write a function remove_digits that receives two arrays of type int. The first array contains a number of integers, and the second array is an array of digits. It is necessary to remove all digits ...
0
votes
1
answer
78
views
Insert numbers to array
I need to insert for every number sum of his digits in array using pointer arithmetic. Auxiliary arrays are not allowed.
Example:
123, 456, 789
Output:
123, 6, 456, 15, 789, 24
Code:
#include <...
-2
votes
2
answers
184
views
Segmentation Fault (core Dumped) when working with pointers in C
So im currently trying to get my program to read a user input ( a double), and store said input in the heap using pointers in C. I allocate 40 bytes to the heap, and the program runs fine up until the ...
0
votes
2
answers
50
views
&q - &p, q and p pointing to non-initialised char array. And o/p is 1. How?
main(){
char a[20],*p,*q;
p=&a[0];
q=&a[10];
printf ("%d\n",&q - &p) }
This C program gives o/p as:
1
As I understand, the values stored at those addresses ...
0
votes
3
answers
45
views
Why do I have to use a two-dimensional array?
I'm reviewing this code which converts the capital letters to lower case and I don't understand why it declares char*argv[] and later inside the for loop it uses argv[1][i] as if it were a two-...
0
votes
1
answer
87
views
Remove duplicate array elements using pointer arithmetic
This is algorithm for removing duplicate elements from array:
#include <stdio.h>
int main() {
int i, n = 5, j, k, arr[5] = {1, 2, 1, 2, 4};
for (i = 0; i < n; i++) {
for (j = i + 1; j ...
0
votes
1
answer
53
views
Number of conjugate complex pairs
I need to write a function which fills array rez with the conjugate-complex pairs from the array bounded by p1 and p2. The function returns the number of conjugate-complex pairs placed in the array. ...
3
votes
2
answers
152
views
Is computing a pointer to uninitialized memory undefined behavior in C?
If I understand correctly, this programme has undefined behavior in C++ because the intermediate value p + 1 is a pointer to uninitialized memory:
int main () {
int x = 0;
int *p = &x;
...
1
vote
2
answers
30
views
Accessing a dynamically created multi dimensional array with pointers arithmetic
I would like to understand how does it works to scan a dynamically allocated two-dimensional array using pointers arithmetics?
I can't figurate why in my example pointer arithmetic isn't returning the ...
2
votes
0
answers
98
views
Object size from (begin, end) pointers in strictly conforming C
Suppose I have two pointers to char pointing to the beginning, begin, and just past the end, end, of an array of some kind (as is customary in C++ before ranges, for example). One would expect all I ...
0
votes
1
answer
157
views
print char pointer value in c
-: was trying to learn pointers in c :-
I couldn't print the value at the character pointer, rest of the program works fine.
It just prints a blank space, didn't get any error though.
#include<...
1
vote
1
answer
203
views
Check if string is reverse substring
I need to write function which will check if string s2 is reverse substring of string s1, and return 1 if the condition is true. Function should be made using pointer arithmetic.
For example:
char s1[]...
-2
votes
2
answers
104
views
Sum of array digits using pointer arithmetic
I need to write sum of digits for all array elements using pointer arithmetic.
Program should print: 0 1 2 5 8 16 14
#include <stdio.h>
void sumDigit(int arr[], int n) {
int *p = arr, m, sum;
...
0
votes
1
answer
354
views
Print prime numbers using pointer arithmetic
My function writePrime has to write all prime numbers from array using pointer arithmetic. I cannot use any other function except main and writePrime.
#include <stdio.h>
void writePrime(int arr[]...
0
votes
1
answer
28
views
Cutting a texture from a sprite sheet with pointer arithmetics
I am trying to get smaller textures from a sprite sheet using pointer arithmetic, but the results are not quite the same as I expect to see.
Basically, I am using this function to do such a thing:
...
2
votes
0
answers
80
views
Can an array of single-member structure objects be interpreted as an array of their subobjects?
In the following program, an array of objects of structure type, wherein each object comprises a sole subobject of type int, is initialized by a function which treats it as an array of ints:
#define N ...
1
vote
3
answers
52
views
which one is the correct type of an array variable? (for using with pointers in C)
If i want to initialize a pointer to an int to an array, i need to use this notation:
int data[] = {34,35,36};
int *ptr = &data[0];
because the type of the variable data is int[], that's the ...
1
vote
1
answer
304
views
c programming - working with BIGNUM in openssl
i am trying to add two big numbers using openssl library in c but i really don't know how to work with it.
The documentation of openssl - BN_add() is here https://www.openssl.org/docs/man3.0/man3/...
0
votes
1
answer
83
views
How does the expression *++ptr is different from ++*ptr? [duplicate]
Can somebody explain why in the output 3 4 4 is getting printed instead of 4 4 4?
#include <stdio.h>
int main(){
int a[] ={0,1,2,3,4};
int *p[] = {a,a+1,a+2,a+3,a+4};
int **ptr= p; ...
4
votes
1
answer
52
views
indexing array in function from the pointer of some element
Imagine some array
uint8_t var[5] = {1,2,3,4,5};
so var will be pointer to the first element of this array, and
uint 8_t* a=var;
b=a[3]
and
b=var[3]
will give the same result.
But will
a = &var[...
0
votes
1
answer
130
views
What purpose is the lookup table serving in this code?
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 ...
-4
votes
2
answers
93
views
Why can void pointers be subtracted but not added?
Why does
printf("%ld\n", (void *)0 - (void *)0);
compile, but
printf("%ld\n", (void *)0 + (void *)0);
does not?
1
vote
2
answers
115
views
How to Justify the Output of a Pointer To Pointer char array Program
#include <stdio.h>
#define STOP 0
void function(char**);
int main() {
char*arr[] = { "icsp","hw6","fall","spring","autumn","winter"...
2
votes
1
answer
104
views
Why difference between 2 address is not multiple of element size
I couldn't understand why var is coming 6, how is it calculating
#include <iostream>
using namespace std;
int main()
{
char *A[] = { "abyx", "dbta", "cccc"};
...
-1
votes
2
answers
242
views
C how to print index of array pointer
I have a function which contains for loop for printing pointer to array index and value
but my problem is that i cannot find on the internet how to print index by pointer to array
I have tried many ...
1
vote
1
answer
79
views
Using fgets for multiple lines with stdin
I'm new to C programming, and I'm trying to store multiple lines from stdin into an array. I succeeded in storing each line in the array, but it doesn't append the new lines but just store it over the ...
1
vote
4
answers
221
views
Confused with how strlen works in C
I have been studying C lately and i have came across a source code of strlen which really confused me and i had to look it up in other places but couldnt really understand it yet.
strlen:
#include <...
0
votes
1
answer
70
views
Char pointer seems to get 'stuck' at slash-zero? ('\0')
I'm solving a problem in C where I'm given a sequence of bytes in memory that represents game results between two teams, something like this format: "manchester 40 chelsea;chelsea 43 arsenal\0&...
0
votes
2
answers
108
views
Max value 2d array using pointer arithmetic
I'm trying to write a programm to find a maximum value in column in a initialized 5x5 matrix, and change it to -1. I found out the way to do it, but i want to find a better solution.
Input:
double ...
0
votes
0
answers
27
views
Why does the code output shows address of integer after performing pointer arithmetic? [duplicate]
#include<stdio.h>
int main(){
int i=30;
int *ptr=&i;
printf("Value of i is %d\n",*ptr);
ptr++;
printf("Value of i is %d\n",*ptr);
return 0;
}...
0
votes
1
answer
81
views
Adding number to pointer value
I am trying to add a number to a pointer value with the following expression:
&AddressHelper::getInstance().GetBaseAddress() + 0x39EA0;
The value for the &AddressHelper::getInstance()....
0
votes
0
answers
69
views
C pointer arithmetics and expression evaluation
I need help with evaluating this C code. In particular, I am not hundred percent sure about the last line:
#define uint unsigned int
#define ushort unsigned short
uint *contents; // 96 bytes long ...
5
votes
1
answer
215
views
Subtracting 1 vs decrementing an iterator
In the accepted answer to "Iterator to last element of std::vector using end()--" @barry states:
Note that if vector::iterator is just T* (which would be valid), the first form above is ill-...