57,314 questions
-3
votes
4
answers
101
views
Can I declare a variable along with a pointer? [closed]
Can I declare a variable along with a pointer?
//like this
struct node n1,n2,n3,*start;
//or do i have to do it separately
struct node *start;struct node n1,n2,n3;
to declare variable alongside ...
4
votes
1
answer
124
views
Why don’t pointers appear in Python Tutor’s memory visualization in C?
I’m new to C and I’m trying to understand how pointers work. In this piece of code, when I run it on Python Tutor (pythontutor.com) to see how the stack and heap work, I never see the pointer being ...
5
votes
3
answers
300
views
The C 'Array-to-Pointer Decay' Paradox: Why do these function declarations behave identically? void func(char a[10]) vs void func(char *a)
I'm struggling to understand a core concept in C regarding Array-to-Pointer Decay when arrays are passed as function parameters.
It is well-known that when an array is passed to a function, it "...
0
votes
0
answers
138
views
Does accessing the contents of the string after calling reserve causes UB? [duplicate]
From another thread I found that
indeed allocates enough storage to hold at least n elements, but it doesn't actually fill the container with any elements
If elements are already allocated why ...
6
votes
1
answer
146
views
Is pointer arithmetic on a pointer that points to a destroyed array element well-defined?
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 ...
0
votes
1
answer
157
views
How to release dynamic memory on the heap in called function C++
#include <iostream>
using namespace std;
int* apply_all(int* arr1, int size1, int* arr2, int size2){
int* on_the_heap = new int(size1 * size2);
int temp = 0;
for(int i {0}...
0
votes
6
answers
258
views
Pointer to an array in a struct in C
I'm working (in C) with a struct that contains arrays of strings. How do I point to one of those strings?
To make it a bit clearer, consider:
struct Books {
char title[MAX1][MAX2];
char author[MAX1][...
3
votes
1
answer
138
views
Accessing a struct member from what is not the same struct type
The code below looks crazy, because I am interested in finding out the limits of what’s possible, and it isn’t exactly what I’m doing in my real code. It compiles without warnings and works as ...
1
vote
0
answers
94
views
Different pointer address inside called function [closed]
I have a set of functions:
void my_small_function(uchar *input_string){
//Does something without modifying the string (just checking)
}
int my_large_function(uchar *my_string){
//Does ...
5
votes
1
answer
224
views
How do strict aliasing rules apply to pointers-to-pointers-to-characters (and functions like `strtol`)?
How do strict aliasing rules apply to pointers-to-pointers-to-characters? For example, does the following contain undefined behavior? (godbolt)
#include <iostream>
#include <cstdlib>
long ...
2
votes
3
answers
175
views
What is the role of restrict here?
size_t hash(char const[restrict static 32]) [[reproducible]];
This is a function definition from Modern C by Jens Gustedt.
I understand that this is a more preferred alternate notation to a 32 char ...
-3
votes
1
answer
113
views
Array passed as an argument does not seem to be changing [closed]
I was tasked to write up a C program to sort an array using quicksort, without using recursion.
Original array:
-25, 87, 12, -6, 91, 3, -48, 70, 19, -33, 55, 2, -18, 99, 41, -72, 63, 15, -90, 27, 8, -...
3
votes
1
answer
84
views
Casting pointer-to-intptr_t to pointer-to-pointer
A pointer can be safely cast to intptr_t (or uintptr_t) and back (on platforms where those types exist), but what about casting a pointer-to-intptr_t into pointer-to-pointer and using that to modify ...
-2
votes
1
answer
101
views
Avoid mixing receiver types in generic constructor
Can the same -- object construction -- be achieved without mixing pointer and value receivers as per official docs ? https://go.dev/tour/methods/8 The problem is Error() cannot be just made with ...
0
votes
0
answers
82
views
Dodgy void pointer arithmetic in C [duplicate]
I want to write (in C) library routines which create and use arrays of items whose size is unknown at compile time. So I create an 'array':
void *item_array=malloc(arr_sz*item_sz);
and a function ...