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

Type casting char array to char **

If I do this: #include <stdio.h> typedef char **arr2D; char arr1D [5 * 10]; int main (void) { ((arr2D)arr1D)[0][0] = '_'; printf("%c", arr1D[0]); return(0); } I receive crash, which suggests undefined behavior. Why can't I d...
more »

2017-01-15 11:01 (1) Answers

Dynamically allocating array of structs C

I'm trying to make an array of structs in c, but I can't make it work. When I try to run it, the program crashes. typedef struct{ char name[20]; char manufacturer[20]; unsigned int price; } product; unsigned int stringToNr(char *numbers...
more »

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

Pointer to Array in C

How can you interpret the following line of code? int (*arrayABC)[10]; In my textbook, it says that we have a pointer to a pointer to the 0th element of an integer array. However, I don't quite understand this. My interpretation: We have some va...
more »

2017-01-04 22:01 (3) Answers

De-referencing char pointer run-time error

Why does the below code cause a run-time crash? The code itself is not very useful, but , by creating a pointer to a char pointer and pointing to string literals in main, passing this pointer to my function and trying to read the strings causes pro...
more »

2017-01-04 16:01 (1) Answers