250 questions
Advice
0
votes
3
replies
88
views
Restrict keyword and pointers to pointers
I'm in C and I have an int ** (2 dimensional array, it's basically a matrix, it's actually set up a little bit complicated but there's no internal aliasing, so it's not relevant).
is this int** ...
0
votes
3
answers
209
views
Realloc in function that deletes a row in a matrix
I have just encountered a case in this C program:
void row_del(int**& a, int& row, int col, int k) {
for (int j = 0; j < col; j++) {
for (int i = k; i < row - 1; i++) {
...
3
votes
4
answers
143
views
What's wrong with this code, which gets content (chars) from file and stores it in char**?
I have a small project (ed-like text editor for myself. Just want to edit some config files using it) and I have some problems with it.
workspace_file->flc = malloc(sizeof(char *));
...
1
vote
3
answers
225
views
Deleting one node of single linked list use double pointers in C language
I have been reading something related to C language's double pointers, and I have some doubts about the following piece of code. The following piece of code is about the operation of deleting a node ...
0
votes
3
answers
129
views
Accessing information on a 2d array with a double pointer in a struct (C)
In trying to understand pointers, I created a M[x][y] array, a pointer to said array *p_M[x] and a pointer to said pointer **d_p_M. The first pointer points to the first element of a row in the array ...
2
votes
3
answers
164
views
Converting to char*** from char* [2][2]
I have the following variable
char* a[2][2] = {{"123", "456"}, {"234", "567"}};
I wanted to refer it using another variable. While the cast works, accessing ...
0
votes
0
answers
65
views
Why do we use a single pointer rather than a double pointer to pass an array to a function? [duplicate]
Why not double pointer to take an array?
I'm having a bit of problem understanding why an array isn't passed as a double pointer (a pointer to a pointer) since the array name itself is a pointer (...
0
votes
4
answers
119
views
char * gives garbage value when pointing to a variable of a function in C
When i pass a pointer to a function and then change the pointer to a another location of the memory, I get SIGSEV or garbage values. Where is a code to demonstrate that:
#include <stdio.h>
void ...
2
votes
2
answers
132
views
why do I need to add an & sign while allocating a memory space inside a functions? [duplicate]
I wanted to try making an allocate function for a cell in a list, but when using it inside another functions, I need to add an "&" sign
I am aware of what "&" means in c (...
0
votes
2
answers
84
views
"Magically" avoiding segfault
I wrote a small demo program
#include <stdlib.h>
#include <stdio.h>
typedef struct tag_node {
struct tag_node *left, *right, *prev;
int value;
} node;
int main()
{
node *...
1
vote
0
answers
82
views
Marshalling double pointers in C#
I have to interact with a provided C library where one of the functions is declared as follows:
int GetFileList(struct spiflash_file **ptr_spiflash_filelist, int **file_num);
where **...
1
vote
2
answers
71
views
Using realloc with pointer to pointer in another function
I'm trying to use realloc in this program to increase an array's size, using pointer to pointer. But it doesn't end up doing the task:
#include <stdio.h>
void push(int** data)
{
*data = (...
1
vote
1
answer
256
views
Runtime error: `load of null pointer of type 'char'` when indexing an array
I'm trying to write a trim function, but when I try to use it the compiler is giving me a runtime error or load of null pointer of type 'char' when I try to run this code:
// Trim trailing whitespace
...
2
votes
1
answer
126
views
Necessity of double pointer when using realloc to manipulate array
I am new to C++ programming and want to try out manual memory management (I am aware that it is advised not to use manual memory management, but I still want to give it a try).
My goal was to write a ...
-3
votes
2
answers
128
views
Confusion about C pointers
I am sending this message to clear my confusion that I could not manage and handle.
The foo1 function code should work. I am giving the code details for you.
When I run the code, the result is a ...
0
votes
3
answers
102
views
Addresses in structures
The following is an abstract version of a problem I am currently having.
#include <stdio.h>
int main()
{
typedef struct {
char * bar
} struct_t;
struct_t foo = {};
...
0
votes
1
answer
586
views
double pointer to the char array
I have been trying to figure out how double pointer works with char * and char [].
What I want to do is to assign a double pointer to the char * or char [] and then change the content.
#include <...
1
vote
3
answers
65
views
How does this code allocate memory and why does it fail to write to the allocated memory?
Why does the code below compile successfully but fail to write memory other than address 0? (compiled in dev-cpp 6.3)
#include <stdio.h>
#include <stdlib.h>
void alloc_arr(int **d_ptr);
...
0
votes
2
answers
67
views
What does this double pointer do in c programming?
#include <stdio.h>
#define LENGTH 3
char *words[LENGTH];
int main( int argc, char *argv[] )
{
char *pc; // a pointer to a character
char **ppc; // a pointer to a pointer ...
0
votes
2
answers
98
views
How to deconstruct complex C/C++ statements/declarations?
Take the following snippet as an example.
char* const (*(* const bar)[5])(int)
Cannot seem to make sense of it or more so, cannot identify the initial point from where to begin making sense of it.
1
vote
2
answers
82
views
Why regular swap works with a pointer-to-pointer variable?
I know the difference between these two functions: Swap(int *x, int *y) vs Swap(int **x, int **y).
But, I'm not sure how this kind of code works.
How is it possible to swap the address which the ...
1
vote
1
answer
40
views
what is this casting (long*)x when x is void**
So I found the following code in a program:
static void* InterlockedExchangePtr(void** x, void* switchval)
{
return _InterlockedExchange((long*)(x), (long)(switchval));
}
(I know about atomics, ...
1
vote
1
answer
215
views
How to iterate through a dynamic, rectangular matrix in C?
I have to create a matrix with pointers in dynamic memory in C, fill it with random numbers then print it.
This is part of a larger assignment from college (I had to do a whole library of functions ...
0
votes
1
answer
84
views
Question about argument in a function as a pointer to a pointer
I am reading the book "C Programming: A Modern Approach" by KN King, where in chapter 17.5 on page 432 they define a function for deleting a node from a singly linked list:
The following ...
1
vote
1
answer
139
views
Is it better to store a 2D image in a flat array or in an array of arrays?
I have two options.
receiving and processing pixel data in a flat array.
If I choose it, I have to use single pointer.
I have to use this code in every repeated pixel data process.
int getIndex(int ...
0
votes
1
answer
55
views
Failed to manipulate the string passed as pointer to pointer inside the function
I am creating a removeNonAlphaCharacters function trying to remove all non alphabetic characters using C.
I could remove all non-alphabetic characters inside the function, but I can not pass the ...
0
votes
1
answer
91
views
Why does malloc(0) in C not produce an error when working with char pointer pointers
I'm trying to write a simple split function in c, where you supply a string and a char to split on, and it returns a list of split-strings:
#include <stdio.h>
#include <stdlib.h>
#include &...
0
votes
1
answer
60
views
Getting Null iterate through to next element of array using pointers
#import<stdio.h>
#import<stdlib.h>
typedef struct Video {
char *name;
int unique_views;
} Video;
typedef struct Viewer {
char *username;
Video *watched_videos;
int ...
0
votes
3
answers
136
views
How to understand secondary pointer?
i want to ask a question about pointer:
void fun(const char** a) {...}
1.
const char* b = nullptr;
fun(&b);
2.
const char** b = nullptr;
fun(b);
why use 1 but not 2?
1 is good, 2 donnot work
0
votes
1
answer
71
views
struct using a pointer, ... in something not a struct or union
I defined a structure called coordonnees declared as a pointer, I want to append values to the pointer a_visiter but it's not working. How can I fix that?
Here is the struct code:
typedef struct ...
0
votes
0
answers
28
views
Pointer to Pointer Memory-address [duplicate]
about pointers in C++
why we use ** in pointer to pointer
I just want to know the reason
why we use ** in pointer to pointer
what is the main reason of using double indirections ?what is the logic ...
0
votes
1
answer
100
views
how to dynamically allocate a 2d array with the help of a function in C [duplicate]
void alloc_matrix(int ***mat, int *m, int *n) {
mat = (int **)malloc(*m * sizeof(int *));
for(int i = 0; i < *m; i++)
mat[i] = (int *)malloc(*n * sizeof(int));
for(int i = 0; ...
2
votes
4
answers
934
views
Using double pointers in C
Am doing a task where am supposed to used getline function read input from a user from the terminal. Here is my code:
int main(int ac, char **av)
{
printf("Write something: \n");
...
0
votes
1
answer
73
views
How to update field of a pointer to a struct in C?
Im trying to update a field of a struct that's a pointer like so:
typedef struct Data *STRUCT_POINTER;
typedef struct Data
{
int element;
STRUCT_POINTER next;
} STRUCT_NODE;
void manipulate(...
0
votes
1
answer
142
views
inserting node at the beginning of a circular linked list using void function
I m trying to insert a node at the beginning of a circular linked list using a void function.
When i pass pointer to pointer head in the function because i have to change the head itself, it is ...
0
votes
2
answers
69
views
G++ responds differently to const int** and const int* as function parameters
I was writing a C++ function that takes int** as input and the function itself does not modify the data (i.e. matrix elements). So I decided to make the argument a const int** type (Is that an ...
0
votes
2
answers
555
views
Incompatible pointer type pointer to array and double pointer to array
So i am new to programming and i have this program that i have to write, it has an array of integers and i have to pass it to a function with a pointer and then with a double pointer.
After i wrote ...
1
vote
1
answer
55
views
Modify pointer value in procedure void in C
I am trying to sort three pointers in ascending order of their value y, I'm new using pointers and pointers to pointers so i can´t see the error that is causing a segmentation fault in the execution
...
0
votes
0
answers
142
views
Assigning a pointer to "member function pointer" to a pointer to "void"
#include <iostream>
struct Adder {
int a, b;
int operator()() { // member function.
return a+b;
}
};
int main() {
using MemFuncPtr = int(Adder::*)() ;
alignas(64) char buf[64]; ...
0
votes
1
answer
113
views
parse line from file to list in c c90
I'm working with c90 on linux.
I have a strange bug when I want to end a string,
let idx be the index, so when I get to the last index I want the list[idx] to be NULL.
example:
list[0] actually "...
0
votes
1
answer
54
views
segfault when setting character of malloced pointer-to-pointer string
In a bigger project i have been working on, i got an Segmentation fault (core dumped), so i managed to reproduce the problem like this:
#include <stdio.h>
#include <string.h>
#include <...
0
votes
2
answers
71
views
why can't i use casting to deal with pointer to pointer variable without using double asterisks? - This is purely academic
I was just trying something to understand pointer to pointer deeply. I write the code below
int x = 20;
int *p0 = &x;
int *p1 = &p0;
printf("*p0 = %d\n", *p0);
printf("**p1 = %d\...
1
vote
0
answers
54
views
How do I create a pointer-to-pointer type in Jython without getting a "pointer only supported for scalar types" error?
I am trying to call external functions inside of a DLL from a Jython script using ctypes.
The prototype is declared as:
extern cl_error_t funcname(const char **varname)
That's why I tried to create ...
0
votes
1
answer
81
views
Deleting head node of a linked list in C where every node knows its headlist
typedef struct node {
int x;
struct node *next;
struct node **head;
} node;
Considering this struct, I've implemented a push function:
node *push(node *nodo, node *top) {
nodo->...
1
vote
2
answers
80
views
Struggling to understand how to call a pointer to pointer in scanf
I'm building a program in C to run some simulations for my PhD research. Basically the program uses a function to read an input file with some important values, and then assign these values to ...
0
votes
1
answer
55
views
Searching for a string in BST fails
I am searching a string in a BST. If I found the string, I just return it. If not found, I print 3 suggestions:
the last traversed node before declaring that the node is not found
the inorder ...
0
votes
1
answer
713
views
I am trying to load words from a text file into a binary search tree
I am trying to load words from a text file into a binary search tree.
Each line of the file contains one word.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef ...
-1
votes
1
answer
61
views
C -- pointers to pointers VERSUS array of pointers
In C, why can't I write:
char **expectedPINs01 = { "0", "5", "7", "8", "9" };
Cause I got:
warning: initialization of ‘char **’ from incompatible ...
0
votes
0
answers
53
views
Why dereferenced pointer to pointer is not the same as pointer? [duplicate]
I recently wrote a MyStack class which would work just like STL stack. Now i decided to write MyStack struct, which I will work with using functions instead of methods
struct Stack
{
int _data;
...
0
votes
1
answer
2k
views
Getting "corrupted size vs. prev_size" in C++
I'm trying to create a program to print a 2D matrix after reading in data from a file.
My program works fine if the 2D matrix is a square with equal X & Y values, but I get the "corrupted ...