Add entry to a dynamic array in C++

dynamic 1d array in c
dynamic array size in c
dynamic array of strings in c
how to take input in dynamic array in c
dynamic memory allocation in c for array of strings
c dynamic list
dynamically add elements to array c
realloc c

So I've just learned about pointers. I wrote a code to initiate a list and copy the elements of list to new_list and add new elements to new_list. However, when I run my code, my compiler says that the add_entry in main() and the function are not a match. Plus the line where I initialize number_list, it says "excess elements in scalar initializer". Can you please check for me where am I getting wrong at? I'm not allowed to use vectors btw. Thanks!

#include <iostream>
using namespace std;

typedef int T;

T* add_entry(T* list, const T& new_entry,int& size);


void print_list(T* list, int size);

int main() {
    T* number_list = new T[3];
    number_list = {1,2,3};
    number_list = add_entry(number_list, 3, 3);
    print_list(number_list, 4);

}

T* add_entry(T* list, const T& new_entry,int& size) {
    T* new_list;
    for (int i = 0; i < size; i++) {
        new_list[i] = list[i];
    }

    new_list[size] = new_entry;

    delete[] list;

    return new_list;

}

void print_list(T* list, int size){
    for (int i = 0; i < size; i++)
        cout << list[i] << endl;
}
edit: the capacity parameter is a part of something else in this code, which I have deleted for demonstration since it's not good to ask too much on this forum. My code doesn't run still.

Declaration and definition of add_entry() do indeed not match. The former is

T* add_entry(T* list, const T& new_entry,int& size);

while the latter is

T* add_entry(T* list, const T& new_entry,int& size, int& capacity)

As you seem to not use capacity inside the function, you should remove it from the parameter list.

append to dynamically allocated array in c, The problem is that sizeof does not tell you how many elements are in the array; it tells you how much space the pointer (which points to the first  Dynamic Arrays in C. If you’re from a dynamic programming language such as JavaScript or Ruby, working with Arrays in C might seem a little weird.

There's a lot wrong with this code, but to answer your question of compiler error, you didn't declare int& capacity in declaration, T* add_entry(T* list, const T& new_entry,int& size); but mentioned it in function implementation. So it's a mismatch in number of parameters passed from main and arguments received in T* add_entry(T* list, const T& new_entry,int& size, int& capacity)

Dynamic Arrays in C - Jacob Clark, In the above example, we created an array and added 3 numbers to it… array.c​:8:5: warning: array index 3 is past the end of the array (which  Features in C that enable you to implement your own dynamic array: Memory management functions: malloc( ) (we will use calloc() for arrays) free( )

The main problem is here

T* add_entry(T* list, const T& new_entry,int& size) {
    T* new_list;
    for (int i = 0; i < size; i++) {
        new_list[i] = list[i];
    }
    new_list[size] = new_entry;
    delete[] list;
    return new_list;
}

You haven't allocated any memory for new_list so you are dereferencing a garbage pointer. Not good, and likely to make your program crash. It should be

T* add_entry(T* list, const T& new_entry,int& size) {
    T* new_list = new T[size+1];
    ...

Also I suspect you are meant to increment the size inside this function (otherwise why would size be a reference) so the full function should look something like this

T* add_entry(T* list, const T& new_entry,int& size) {
    T* new_list = new T[size+1];
    for (int i = 0; i < size; i++) {
        new_list[i] = list[i];
    }
    new_list[size] = new_entry;
    delete[] list;
    ++size;
    return new_list;
}

Dynamic arrays in C, The C programming language does not have dynamic array as a language feature Allocate memory cells for the array elements and make p point to the first  Key Features of Dynamic Array. Add Element: Add element at the end if the array size is not enough then extend the size of the array and add an element at the end of the original array as well as given index. Doing all that copying takes O(n) time, where n is the number of elements in our array.

[Solved] Dynamic Array in c using realloc(), I am expecting the array to remain the same with ealloc, only adding the new part​, but for some reason, the original data is being manipulated. The dynamic array provides dynamic memory allocation, adding, searching, and sorting elements in the array. Dynamic array overcomes the disadvantage of the static array. In a static array, the size of the array is fixed but in a dynamic array, the size of the array is defined at run-time. List<T> is the dynamic arrays in C#.

How do Dynamic arrays work?, A Dynamic array (vector in C++, ArrayList in Java) automatically grows when we try Elements can be added at the end of a dynamic array in constant time by  Sometimes the size of the array you declared may be insufficient. To solve this issue, you can allocate memory manually during run-time. This is known as dynamic memory allocation in C programming. To allocate memory dynamically, library functions are malloc (), calloc (), realloc () and free () are used.

stackoverflow/appending-a-value-to-the-end-of-a-dynamic-array , Basic C/Alogs problems that I discuss in Stackoverflow.com - sangeeths/​stackoverflow. stackoverflow/appending-a-value-to-the-end-of-a-dynamic-array​-using-realloc-fibonacci.c. Find file Copy path. Fetching adding a comment */. Adding elements to dynamic array. aisgzdavinci. Hi, I want to add some elements to an n*n array that the user specifies at the beginning. However, I'm getting a C2540

Help with creating a dynamic array that can add and , Hey Im working on this program for school, pretty much I have to create an array of five strings and create and use two functions, addEntry and  Multi-dimensional arrays. C supports multidimensional arrays. The simplest form of the multidimensional array is the two-dimensional array. 2: Passing arrays to functions. You can pass to the function a pointer to an array by specifying the array's name without an index. 3: Return array from a function. C allows a function to return an array. 4

Comments
  • Dynamic array is spelled std::vector in C++.
  • We're not allowed to use vectors
  • You cannot assign initialiser lists to arrays (number_list = {1, 2, 3};), either assign each element separately (NL[0] = 1; NL[1] = 2; NL[2] = 3;) or use the initialiser list right at creation (T* NL = new T[3] { 1, 2, 3 };).
  • Maybe make size param of add_entry a const int& instead of just int&? I try compiling your program and gcc barfs on invalid initialization of nonconst reference of type 'int&' from an rvalue of type 'int' at the call to add_entry with an arrow pointing at the second numeral '3'.
  • @Aconcagua oh, right. I guess it doesn't make much sense to take reference to a literal since it cannot be changed anyway.
  • Sorry I edited your comment. I'm new here and accidentally made a wrong edit
  • @PemiNguyen Now, can you explain why are you receiving new_entry and size by address? & receives by address, you want the value (I believe). Try removing the & sign, only use it when you want to receive a pointer or address of some variable.
  • Also, I didn't really went through your code's logic, but you can't add more values in your dynamic array than the size it's declared for. You will have to use linked list or vector
  • @saadi T* list is an address. int& size is a reference.While unnecessary for size, it's certainly not problematic.
  • @PemiNguyen I suspect size is a reference because you are supposed to increment that value in the add_entry function.
  • @ThomasSablik I think the prototype has been designed by the professor (i.e. it can't be changed) and the intent is to pass a variable not a literal. The OP just wrote some test code which was incorrect (as he admits a couple of comments above).