Questions tagged [pointers]

21964 questions
1

votes
2

answer
73

Views

Double pointer as function parameter

I was reading a page of 'Understanding and Using C Pointers' when this function appeared: void safeFree(void **pp) { if (pp != NULL && *pp!= NULL) { free(*pp); *pp = NULL; } } and an example code from it: int main(int argc, char **argv) { int* pi = (int*)malloc(sizeof(int)); *pi = 5; safeFree((void*...
Martín
0

votes
1

answer
35

Views

Linked lists and pointers confusion

I'm working on my final project and I was introduced to linked lists, which I must use. I'm incredibly frustrated after trying to understand how the code works. The concept to me makes complete sense. The code i'm given as an example though, doesn't. typedef struct node_s { char name[20]; int age;...
SteveSonOfJobs
0

votes
2

answer
39

Views

How can I use this pointer with pointer to member function

I have typedef for a function pointer: typedef bool(WlanApiWrapper::* (connect_func) )(WLAN_AVAILABLE_NETWORK, const char *, const char *); and have a method that returns a pointer to function: const auto connect_method = map_algorithm_to_method(*network) So I want to call that like that: (*this.*co...
Andriy
1

votes
0

answer
371

Views

Create UnsafePointer<UnsafeMutablePointer<Int8>> from [String]

growing up with swift, the concept of pointer and low-level c stuff is unfamiliar to me and, at least for me, hard to understand. I have an Array, which i need to create a Pointer to (thats how I understand pointer). The pointer of type UnsafePointer is required for the function AuthorizationExecute...
Christian
1

votes
2

answer
62

Views

pointer of a pointer and memory allocation

If I have int **array and want to place a series of numbers in it (I don't know its size), 5 3 4 0 or 9 1 5 8 3 0 as an example. As far as I know I should be using malloc So I did something like this int **array; int n = 1, inp = 0; while(n){ // scan till the input is 0 scanf('%d', &n); array = (int...
user9138715
1

votes
1

answer
40

Views

Accessing an integer within a structure which has Pointer to structure

I am new to pointers and trying to use a pointer to the structure.I need to access the structure parameter using pointer. Kindly assist me. Here is the program: #include typedef struct{ int a; int b; int c; }time; typedef struct{ int a; int b; time record; }myarray; typedef struct{ myarray *ptrtoarr...
Gopala Krishna
1

votes
0

answer
45

Views

How to handle implicit and explicit type conversions between const and non-const fancy pointers?

Given the type definitions using ConstHandle = MyEntity const*; using Handle = MyEntity*; it is possible to conduct the following type conversions: Handle → ConstHandle (implicit) ConstHandle → Handle (explicit via const_cast) Handle* → ConstHandle* (explicit via const_cast) ConstHandle& ...
precarious
1

votes
1

answer
198

Views

CTypes: errors passing pointer to array

I am passing an array of doubles to a DLL using CTypes. The code is below. I need to pass a pointer to the array. I have tried to pass it four different ways, as shown in the code. For simplicity, I am passing the same array three times. TestArrayType = ctypes.c_double * 1000 arrNew = TestArr...
RTC222
1

votes
1

answer
53

Views

Memory Leak from Referenced Pointer

I am learning about reference pointers and memory leaks. I have created a method that takes in a pointer to a NodeData, create a Node that contains NodeData, and a left pointer, and a right pointer, and insert it depending on its value. I am using a reference pointer to traverse my tree. I am gettin...
Emma
1

votes
0

answer
40

Views

Trying to use void pointers and random generate an enum array with distinct elements (C++)

I tried to use void pointers and random generate enum array with no repeated elements. But It still have duplicated elements. Which part of my codes is wrong? P.S I haven't learnt vectors, shuffle or iota, thus not allow to use it. This is my enum enum Fruit {Durian, Papaya, Apple, Orange, Mango, Ja...
skyzhuzhu
1

votes
0

answer
143

Views

a value of type Node * cannot be used to initialize an entity of type Node *

I'm writing a simple tree traversal procedure. I have a class for a node in the tree which holds the node's number, a vector of pointers to it's children and some data. I pass this a pointer to a Node to some function which does BFS from that node. class Node { public: int num; long long weight; l...
Yakov Dan
1

votes
2

answer
312

Views

Bad address received from recvfrom: why?

I'm writing a function that is supposed to do some operations and then return (using its arguments) the address of the device that is interacting with (i.e. that used sendto) the recvfrom inside the function. Here's how I call the function, instantiating cliaddr before. struct sockaddr_in cliaddr; m...
Robb1
1

votes
0

answer
59

Views

Microchip dsPIC33 C30 function pointer size?

The C30 user manual manual states that pointers near and far are 16bits wide. How then does this address the full code memory space which is 24bits wide? I am so confused as I have an assembler function (called from C) returning the program counter (from the stack) where a trap error occurred. I am...
berntd
1

votes
0

answer
43

Views

Default Implementation of Link List and Binary Tree using smart pointer

The link list is generally implemented as template struct ListNode { T data; shared_ptr next; //-->why not unique pointer here ? }; while for Binary Tree , template struct BinaryTreeNode { T data; unique_ptr left, right; //-->why not shared pointer here ? }; why can we not use unique_ptr for List...
Kishor Bhandari
1

votes
0

answer
218

Views

int overflow error in ctypes array pointer

So I am trying to use ctypes to use the functions defined in a .dll file for controlling a camera. I have something like this: import ctypes as C import numpy as np memoryid = C.c_int() memoryaddress = C.c_char_p() img = np.zeros((dy,dx),dtype=np.uint8) uc480.is_CopyImageMem(handle,memoryaddress,mem...
Physicist
1

votes
1

answer
49

Views

const pointer to const inside srtuct in function prototype

I have the following struct: struct foo { ... char* cp; }; And I want to pass a struct foo type pointer to a function, but I want the function to cast the pointer to const char* const cp, and I don't want the const qualifier as part of the definition of struct foo. Declaring the function as: void fu...
Aayla Secura
1

votes
3

answer
538

Views

Converting ppm file from P3 to P6 using C

I am trying to write a program that converts a p3 PPM file to P6 using C. But I am running into two problems. 1, I am getting a segmentation fault in my code. and 2, the header isn't being properly read into the converted p6 file. Here is my header file. #ifndef PPM_UTILS #define PPM_UTILS #include...
Harrison
10

votes
5

answer
378

Views

C's equality operator on converted pointers

Coming from Casting integer constant to a pointer type From that question, we know from 6.3.2.3p5 (C11) that we can convert any integer into a pointer (i.e. it is not UB on itself): An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined,...
Acorn
0

votes
1

answer
28

Views

Manipulating pointer to array of pointers

Trying to write a C snippet to store and retrieve an array of structure pointers. I have copied my snippet bellow. What I am trying to do here- method Xos_get_cb_info_from_handle() should return the pointer to the base address of the structure localclient.exit_cb. Which seem to work. After getting t...
Amit Kulkarni
1

votes
1

answer
241

Views

C - store different callback function adresses in an array

I'm trying to store different callback function addresses in an array. To be able to store function addresses with different signatures, I have defined a union and I use typecasting. This approach seems to work, but it is not very beautiful. Is there a better solution? typedef union { void (*callbac...
user1160713
1

votes
1

answer
186

Views

Problems with local pointer array

I've done some digging around but cant find the solution. In my code below, when I declare my float **array as a global variable my program runs as intended. However I'm attempting to declare it in main() and pass it into functions and I'm getting some nasty errors when I try to compile. Sample run...
Kyle
1

votes
0

answer
52

Views

C++ IS there a simplier way to see if a an address region is at least partially interesected with another?

In c++, say I have a ring buffer with addresses. If the consumer is too slow, I want to drop the oldest data. So I have a block of indexes since the production amount may not be equal to the consumption amount. Where A = the head pointer, B = head pointer + new input data size, C = tail pointer - c...
StephanieLoves
1

votes
1

answer
35

Views

How to print dereferenced value of field without explicitly specifying that field golang

package main import ( 'fmt' ) type outer struct { in *int } func main() { i := 4 o := outer{&i} fmt.Printf('%+v', o) } I'd like to see {in:4} at the end of this, not {in:0x......}, i.e. pretty print the data structure. I'd like to accomplish this in a similar manner to the code posted (e.g. with a f...
ryebread
1

votes
1

answer
43

Views

vector of boost::function not working with _placeholder

I need your help. I tried a lot to find a solution for the problem but failed so far. Idea: Create a vector of 'command tuples' like the following: typedef boost::tuple command_tuple; std::vector commands { boost::make_tuple('command1', boost::bind(&myclass::command1, this)) }; If the string 'comman...
drtyhlpr
1

votes
3

answer
71

Views

How to retrieve array from void function and pass to other functions in Main?

I am a beginner in C I have a problem in which I am passing an array of grades to main and will need to convert those grades to ints and find the min max and avg of them. The specifications are void getGrade(int argc, char *argv[]) int findMaximum(grade[]) int findMinimum(grade[]) float calculateAv...
mmajdalani
1

votes
1

answer
101

Views

Coupled fortran/c++ with pointer called in python gives a Segmentation fault (core dumped) error

I have a coupled code (fortran with c++) that I am now calling in python. The main.exe works fine but when I call the coupled version in python I get a Segmentation fault (core dumped) error. I identified the line of the code that gives the problem, and apparently it is cause by a pointer. Here is m...
RennyB
1

votes
1

answer
96

Views

Frama-c: how to access a __malloc* variable allocated by value plugin

I start with Frama-c, so I do not master it well enough. I would like to use Frama-c to implement a pointer aliasing analyzer. Unless I'm mistaken, it seems to me that value plugin does not give information about pointer aliasing. For a start, here is what I have: class vtest = object(self) inherit...
R. Fomba
1

votes
0

answer
232

Views

Converting array of bytes from big endian to little endian?

My program dumps an a bunch of bytes in a long array which is big endian, my goal is to split up the bytes into double words (groups of 4 bytes) and put them in little endian format. Here is the code that I have gotten thus far: int print_got_bytes(struct _section *got_section, Elf_Data *data) { siz...
nice_remark
1

votes
0

answer
107

Views

error: Process returned -1073741819 (0xC0000005) C something to do with pointers?

I'm fairly new to code::blocks and how it works but I'm using it for an assignment at uni, basically, its implementing dijkstra's algorithm using ADTs and adjacency lists, the code works and I get an output but I get the error Process returned -1073741819 (0xC0000005) code for the minimum heap: #in...
namarok
1

votes
2

answer
91

Views

Dynamically allocated array of strings memory doesn't seem to be working

I'm trying to have this function read a sentence and store each word into an array of strings. I store the sentence in line: char** storewords(char* line){ char** newArr = (char**) malloc(sizeof(char*)); free(word); return newArr; } I later call this function and try to print each word in the a...
novel
1

votes
1

answer
145

Views

Pass pointer of string to lua

I have long string in C and I want pass this to lua function. but beacause string is long I don't want pass it, I want pass pointer of string to lua. I do this by use lua_pushlightuserdata but in Lua I can't use this pointer. C code : char* st = 'abcdefghijklmnopqrstuvwxyz1234567890'; lua_getglobal(...
sharafi
1

votes
0

answer
163

Views

gfortran croaks with procedure pointer initially pointing to function with pointer result

I'm trying to set the initial target of procedure pointer in a module as a function within the module. When the result of the target function is a pointer, the code fails to compile with gfortran. Note that it compiles fine with the Intel compiler. I'm using gfortran 6.4.0. If I remove the pointe...
Sriram Swaminarayan
1

votes
3

answer
56

Views

Dynamic arrays (represented with pointers) in C++ - entering elements?

I have a homework task where I should create an array and implement the bubble sort algorithm with pointers only. I create the array, but if I want to input 3 elements, i.e if the user enters n = 3, the program does not allow me entering the third number. I want to ask why this happens? Thanks in ad...
A.Petrov
1

votes
3

answer
69

Views

Dealing with pointer arrays in C

I am trying to implement a convolution algorithm in C using pointers. I know my deconvolution.c code is correct. However, I struggle to call the function in Main to get desired results. Any help is much appreciated. // deconvolution.c #include 'deconvolution.h' #include 'math.h' void deconvolution...
Saif
1

votes
2

answer
92

Views

C Pointer Error

The program below is supposed to read a txt file and put the data in a struct. But it gives an pointer error. It gives an error in strcpy() about pointers. I'm new in C. Whats wrong? #include #include int main() { struct citydata { char city[20]; int temp; }; struct citydata values[15]; struct cit...
Tony
1

votes
1

answer
172

Views

Android null pointer in getfilesdir() [duplicate]

This question already has an answer here: What is a NullPointerException, and how do I fix it? 12 answers Trying to write a string to a file on Android app crashes on opening [duplicate] 1 answer I'm am getting a NULL pointer message on getfilesdir(). I'm sure it has something to do with my imple...
Kris Armstrong
1

votes
0

answer
127

Views

Reading the output of objdump on linux x86_64

I am trying to learn to exploit a sprintf vulnerability. I've gotten to: env -i ./addhost a a '$(python -c 'import sys; sys.stdout.write('sh;#AAAABBBB%037956x%10$hp%63387x%10$hp')')' But reading the objdump code on x86_64 I get addresses like: jmpq *0x20093a(%rip) Which would imply that to get to...
oxagast
1

votes
0

answer
130

Views

C program crashes after calling fclose()

I need to write some functions in order to save Data that is saved in a doubly linked list in a XML-File. The problem I've now is, that calling fclose() gives me a SIGARBT Error. void save() { FILE* file = fopen('/home/***/CLionProjects/fussballManager/teams.xml', 'wt'); if (!file) return; fprintf(...
Tin Tang
1

votes
0

answer
74

Views

nested macros in C _generic macro

I'm currently making a small SIMD vector/matrix library in C. I'm looking to use the _generic macro feature in C11 to make function calls always take vector(__m128) arguments as pointers. Is there any way to have the _generic macro call another macro if a non-pointer argument is given and then have...
shotgunbilly
1

votes
2

answer
51

Views

Copying Value of a pointer to a variable, Pointer is passed as an argument C Program

int NJOBS=0; struct Jobs{ char *jname; int btime; int priority; int order; }Job[10]; int cmd_run(int nargs, char **args) { NJOBS++; Job[NJOBS].order=NJOBS; int bt = atoi(args[2]); int pri = atoi(args[3]); Job[NJOBS].btime=bt; Job[NJOBS].priority=pri; Job[NJOBS].jname=args[1]; } I...
Prabhanjan

View additional questions