Qsort and Comparators weird behaviour. C

So, I am using qsort in my C program from C library. It works as expected so I decided to play around with comparators. Comparator 1 (I use this): int compare (const void * a, const void * b) { if (*(double*)a > *(double*)b) return 1; else ...
more »

2017-03-18 23:03 (1) Answers

Passing pointer to an array into a function (C++)

I'm trying to pass an array into my function calls for build_max_heap and max_heapify so I can modify the array after each call, but I receive an error saying "candidate function not viable: no known conversion from 'int [9]' to 'int *&'for 1st a...
more »

2017-03-18 03:03 (1) Answers

"Dynamic Inheritance" in C

I wrote the following code and it works, but I would like to know if a can be sure that it works all the time on all x86 machines. #include <stdio.h> #include <stdlib.h> typedef struct Base { int a; float b; } Base; typedef struct ...
more »

2017-03-15 23:03 (2) Answers

code crashes when accessing a predefined array

I'm having trouble understanding why my code crashes when accessing the array. When converting the function param to int[3][3] instead of int** I get no problems, but I can't understand why, since the int** I'm given as a argument is a valid pointer ...
more »

2017-03-15 08:03 (2) Answers

Using a pointer to a char array in C++

I'm relativly new to Coding and have some Problems understanding how pointers work in combination with arrays (whom on their own I i understand). I understood that it's possible to create an array of pointers like: #include <iostream> using n...
more »

2017-03-04 10:03 (4) Answers

Why can't we generalize the pointer declaration?

The size of pointer is same irrespective of datatype it is pointing to.Then why do we need to declare the datatype it points to? For example, int *p; //p will point to an integer data type. char *p; //p will point to a character Then,why can't ...
more »

2017-02-27 18:02 (6) Answers

pointer to array of ints c++

I wrote the following code to try to understand how we can use pointers to arrays and references to arrays. Here is my thought process of what is happening. arr is the same as the address of the first elements of the array. *arr gives me what is t...
more »

2017-02-25 15:02 (1) Answers

Printing int **x in GDB

I have an array (called board) of pointers which point to array. I want to print it in normal way (which is pretty hard in GDB). int col = 64, row = 2; int **board = new int*[col]; for(int i = 0; i < col; i++) board[i] = new int[row]; I hav...
more »

2017-02-20 20:02 (1) Answers

Cast raw pointer of array to unique_ptr

I am working against a blackbox framework (cdg), which fills an array of uint32_t with values. The call looks like that: std::size_t dataCount = 100; uint32_t* data = new uint32_t[dataCount]; cdg.generate(data); Unfortunately, the framework doesn...
more »

2017-02-16 11:02 (1) Answers

What's the meaning of "int * * k"?

I am beginner of C Programming language. I saw a code on the book: #include<stdio.h> int main(){ int * * k, *a, b=100; a = &b; k = &a; printf("%d\n",* * k); } I don't know the meaning of "int * *k". Is that a integer...
more »

2017-02-14 07:02 (5) Answers

Bi-dimensional array of pointers

I am currently writing a function which uses a bi-dimensional array of pointers to a structure cell. I'll have to update each cell with data read in a file. As I will need to access the various cells, I have to deal with dynamic allocation. But what ...
more »

2017-02-12 15:02 (3) Answers

Differences when using ** in C

I started learning C recently, and I'm having a problem understanding pointer syntax, for example when I write the following line: int ** arr = NULL; How can I know if: arr is a pointer to a pointer of an integer arr is a pointer to an array of ...
more »

2017-02-06 11:02 (11) Answers

Object methods in C: memory leaks

I have questions about how to properly implement objects in C. Is the fact that I return objects from methods more prone to memory leaks than, for example, never returning an object and doing it by reference in the argument list like this? extern ...
more »

2017-02-03 09:02 (3) Answers

array notation vs pointer notation C

Is there any advantage to using pointer notation over array notation? I realize that there may be some special cases where pointer notation is better, but it seems to me that array notation is clearer. My professor told us that he prefers pointer not...
more »

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

Is this an acceptable use of malloc and free? (C)

I am currently learning C. My lecturer gave this as a bad example of using malloc and free, but to me it seems okay. this is the code: int *p1,**p2; p1 = malloc(sizeof(int)); *p1 = 7; p2 = malloc(sizeof(int*)); *p2 = p1; free(p1); free(*p2); My le...
more »

2017-01-28 22:01 (1) Answers

increment of pointer to array

I'm trying to understand why the following C++ code does not compile int main () { int a[10]; int (*p)[10] = &a; int *q = static_cast<int *>(++p); } If it's not obvious, what I was trying to do is find a pointer to the end of the a...
more »

2017-01-28 19:01 (1) Answers

Why doesn't strcpy use a const pointer for dest?

Is there a reason strcpy's signature is this: char *strcpy(char *dest, const char *src); instead of this? char *strcpy(char *const dest, const char *src); As far as I know, the function will never change the pointer. Am I misunderstanding wha...
more »

2017-01-23 17:01 (5) Answers