Storing pointers to C structures in R as integers

I've been working on using a C library from R by writing custom C-functions using the library's functionality, and then accessing these C-functions from R using the .C-Interface. In some of the C-code, I allocate space for some custom structures and...
more »

2017-10-16 13:10 (1) Answers

Accessing 2D array values using a pointer

I need to take a 2d array (Grid) from stdin, do some manupulation to the chars and print a new grid with the changes. My strategy was to make a Struc with a Grid grid[LINES][COLUMNS] then use getChar() to push each char into grid using a pointer. I...
more »

2017-10-14 22:10 (1) Answers

function pointers in a struct

I was trying to figure out function pointers. My code looks like this I have a file.h where I have a struct that hast 2 members typedef struct _node_ { char* string; int (*compare)(int a, int b); } node in that same file, I have a prototyp...
more »

2017-10-14 09:10 (1) Answers

Input string without knowing the size

What's the way when i want to store string that i don't know the size. I do like this: #include <stdio.h> #include <conio.h> int main () { char * str; str = (char *)malloc(sizeof(char) + 1); str[1] = '\0'; int i = 0...
more »

2017-10-12 11:10 (4) Answers

Retrieving 2D array from pointer in C

I tried the following code char arr[5] = {'A', 'E', 'I', 'O', 'U'}; char (*p_arr)[1] = &arr; printf("%c\n", p_arr[0][4]); //returns 'U' On this stackoverflow thread, char (*p_arr2D)[m] = &arr2D //or arr2D is also a synt...
more »

2017-10-11 12:10 (4) Answers

Dynamic array of pointers from user input

I need some help regarding dynamic allocation of arrays of pointers in C. I am trying to create a program that reads a sentence of words from user input, and stores the words in character array strings. I then want to save the pointers char *word to ...
more »

2017-10-07 21:10 (2) Answers

How does ** work in a 2 dimensional array?

int main(void) { unsigned int x[4][3] = {(1,2,3),(4,5,6),(7,8,9),(17,11,12)}; printf("%d, %u, %u, %u \n",**x, **(x+1), **(x+2), **(x+3)); return 0; } The output of the above code is 3, 12, 0, 0. Shouldn't it be 1,4,7,17 as x stores the ...
more »

2017-10-07 12:10 (3) Answers

Find words and put spaces after them

Once I was asked a question in an interview and I am still not clear how to solve it. Or couldn't even get close to it. Q. Given is a of series of characters eg."thisisachallengingquestion", Write a program to put space after every word except the ...
more »

2017-10-02 23:10 (2) Answers

Adding 2 matrices using pointers

I can acheive it by passing (c,a,b) to add_mat function, where result of a,b is stored in c like, void add_mat(int c[][3], int a[][3], int b[][3], int m, int n) What should be the return type of add_mat if I want to construct the funtion in this w...
more »

2017-10-01 15:10 (3) Answers

Passing pointers as argument in C functions

I read all the answered questions on this topic I could find, but none answer my question... I am still reading about pointers in C and now I am trying to understand how pointers can be passed through a function. However, there is something I dont u...
more »

2017-10-01 00:10 (2) Answers

When do I exactly need a pointer with structs

I really do not understand if and when do I need to declare a pointer to my data struct. I have two programs and both works the same, but I cant figure out where should I need a pointer or not. Here are the programs: #include <stdio.h> #incl...
more »

2017-09-29 12:09 (1) Answers

Dereferencing of multiple bytes variable in C

Can someone explain how dereferencing works in case of variables with multiple bytes? How does it pick up multiple bytes, does it have some loop to go over sizeof(int)? int num1, num2; int* ptr; num1 = 8; ptr = &num1; num2 = *ptr; //What 'mini ...
more »

2017-09-28 16:09 (0) Answers

void* function pointer array cast

I have an array which looks like this: void* functions[]; // pointer to functions, each function returns an int and has int parameters A and B I would like to cast this into the following: int (*F)(int a, int b) = ((CAST HERE) functions)[0]; int ...
more »

2017-09-24 07:09 (3) Answers

Towards understanding void pointers

In my answer I mention that dereferencing a void pointer is a bad idea. However, what happens when I do this? #include <stdlib.h> int main (void) { void* c = malloc(4); *c; &c[0]; } Compilation: gcc prog.c -Wall -Wextra prog.c: In fu...
more »

2017-09-15 15:09 (4) Answers

Pointer points to uninitialized variable

In the following program, ptr points to uninitialized variable x. Before printing ptr, I have assigned 10 to ptr and print it. #include <stdio.h> int main() { int *ptr; int x; ptr = &x; *ptr = 10; printf(" x = %d\n...
more »

2017-09-11 07:09 (3) Answers

How to make gcc warn undefined struct?

I have a struct defined in .h struct buf_stats { // *** }; then in .c file struct buf_stats *bs = malloc(sizeof(struct buf_states*)) ; where buf_states is a typo. but gcc does not warn me, although I used -Wall and this bug/typo cost me 3 ...
more »

2017-09-05 09:09 (2) Answers