Posts

Showing posts with the label pointers

Remove Consecutive Duplicates Recursively giving infinite recursion

Remove Consecutive Duplicates Recursively giving infinite recursion Not a homework question. I am self learning. I have to remove consecutive characters in a string by recursion. However the program I made is not working for inputs containing duplicates. It is goining in infinite recursion and hence gives segmentation fault. However it is working for inputs which doesn't have consecutive duplicates in them. I have tried debugging in Eclipse Ide but things get weird when I debug. (I know how to debug) but I can't figure out the things are different when I debug and when I run. I will give you example after my code. #include <iostream> #include <cstring> using namespace std; void removeConsecutiveDuplicates(char *input) { int l = strlen(input); if(l == 0) { return; } if(input[0] != input[1]) { removeConsecutiveDuplicates(input+1); return; } int i = 1; for(; input[i] != ''; ++i) { input[i-1] = input[i]; ...

How to create a matrix structure in C?

How to create a matrix structure in C? I'm a beginner in C, but I'm currently trying to create a matrix data structure that could be used in different functions without having to explicitly pass the number of columns and number of rows (example: matrixMult(matrix A, matrix B) instead of matrixMult(A, B, rowsA, columnsA, rowsB, columnsB) ). My approach so far has been to declare a struct such as matrixMult(matrix A, matrix B) matrixMult(A, B, rowsA, columnsA, rowsB, columnsB) typedef struct matrix{ int rows; int columns; int **data; }matrix; And then, to properly allocate the matrix, I'm trying to use the following function matrix startmatrix(matrix mat,int n_row, int n_col){ int i=0; mat.rows=n_row; mat.columns=n_col; mat.data=(int **) calloc(n_row,sizeof(int *)); for(i=0;i<n_row;i++){ mat.data[i]=(int *) calloc(n_col,sizeof(int)); } return mat; } Which (to my understanding) allocates the rows containing the columns, and ...

Transferring Pointers from Python to dll and get modified values back

Transferring Pointers from Python to dll and get modified values back I am struggeling with the following problem, which is, I guess, not that complicated for people with more advanced C skills. My intention is to access C++ funtions stored in an DLL from Python. I managed to get this running for simple functions as ADD and MULT. As the funtions I'd like to access (later) will return multiple float values I need tu use pointers. I'd like to do the following basic steps: -Create a variable in python, -Transfer the pointer to the dll (ctypes --> c_pointer(...)), -Update the value the pointer is referring to; -go back to python and use the modified value. My C++ funtion I used for dll creation is: #include <stdio.h> #include <wchar.h> #include <iostream> #include <vector> #include <math.h> #include <fstream> using namespace::std; extern "C" { __declspec(dllexport) int Kappel(double a, double *f){ *f = 25.5; return 0; } } // ext...