C++ Dynamic Array Initialization with Declaration

Dynamic array initialization in c

can we do this?

No. But you can do this:

int arr[n];
memset(arr, 0, sizeof(arr));

You lose the syntactic sugar for initialization, but you get the functionality.
   

Initialize values of dynamic array in a function (C language)

You have the address of a pointer passed to the function:

array

You want to dereference that to get the pointer:

*array

Then apply the array subscript operator to the result:

(*array)[2]

Or equivalently:

*((*array) + 2)

The parenthesis are required because the array subscript operator [] has higher precedence than the dereference operator *.

Generally speaking, you should use the array subscript operator whenever possible, as it tends to be easier to read.

c++ dynamic array initialization with declaration

Technically, a 2D array is an array of 1D arrays. So it cannot convert into pointer to pointer. It can convert into pointer to array, though.

So this should work:

void findScarf1(bool (*matrix)[7], int m, int n, int radius, int connectivity); 

Here bool (*matrix)[7] declares a pointer to array of 7 bool.

Hope that helps.

Initialize a dynamic array in O(1) time in C/C++

No, there is not, not in c++, nor in c, not in any language or CPU I know of. But it can be done with 1 line of code.

in C: for a array of char:

char a[N];
memset(a, '1', N); // only works for data 1 byte long

in C++

std::vector<T> v;
std::fill(v.begin(), v.end(), value);

How do you initialise a dynamic array in C++?

char* c = new char[length]();

Initializing and using dynamic arrays in C++

I see two problems:

  1. the function accepts a pointer. When you write A = ... you're only changing the copy of the pointer that gets passed to you by value. You could use void initArray(int* &A, int size) instead, or have the function return a pointer.

  2. if that's the full code, you might need a forward declaration of the initArray function.

Declare Dynamic Array in main, malloc and initialize in function, print array in main

First a word about terminology, your variable array is technically not an array, it's a pointer. It can be made to point to memory that can be used as an array though.

Now for your problem: It's that when you pass arguments to functions those are passed by value, meaning they are copied. So in the function func1 the argument array is just a copy, and changing a copy will not change the original. Since the original is not changed, you will then in the main function dereference an uninitialized pointer, leading to undefined behavior and the crash.

To solve this you have to emulate pass by reference. This is done by passing the argument using a pointer. In this case you have to pass a pointer to the pointer.

First you of course need to change the function:

void func1(int **array) {

*array = malloc(10 * sizeof(int));
printf("Enter an int: ");
scanf("%d", &(*array)[1]);
printf("Func1: %d\n", (*array)[1]);
}

Then you need to use the address-of operator & when calling the function

int *array;
func1(&array);

An important note: The malloc function does not initialize the memory it allocates, which means that the contents of the memory is indeterminate. Only the second int element of the allocated memory is initialized and can be properly referenced, reading all other elements will also lead to undefined behavior.


Another thing, that also leads to undefined behavior, and as noted by Namfuak, is that in your original function func1 you declared it as returning an integer, but you don't actually return anything. That's why I changed the function here in my answer to return void.

Declare and Initialize dynamic array of head nodes of linked lists

Based on what I understood, you want to declare an array of head nodes. After that, you want to initialize them to NULL:

//declare array of head nodes statically
Node * table[x]; // x value provided at runtime
// Or dynamically
Node ** table = (Node **) malloc(sizeof(Node *) * x);
// Or use calloc directly wich will initialize your pointers to 0
Node ** table = (Node **) calloc(x, sizeof(Node *));

// table is an array of pointers. Each pointer references a linked list.
// Now you have just to put NULL value in each element of table
int i;
for(i = 0; i < x; i++)
table[i] = 0;


Related Topics



Leave a reply



Submit