I have a simple array for math functions, but when adding larger numbers, i will need to add a column to the array. i know you can remove a slot/column using a[i]=NULL, but is there a way to add spaces to an array without re-initializing the array?
I have a simple array for math functions, but when adding larger numbers, i will need to add a column to the array. i know you can remove a slot/column using a[i]=NULL, but is there a way to add spaces to an array without re-initializing the array?
I think the things you know aren't necessarily so. The number of slots in an array is fixed when you declare it, and cannot be adjusted in either direction (up or down).
If you need dynamic memory management, then you'll have to use dynamic memory management (malloc/realloc/free).
heres the code sample I've drawn up, showing you can easily add numbers to an array, but you have to tell the program that you added numbers. Is there any function to tell toe program you expanded the array, or should i just code in another variable counter?
CODE:
OUTPUT:Code:#include<conio.h> #include<stdio.h> int main() { int arry[10]; //create arry and counter int i; printf("The origional \n"); //display and fill the origional array for(i=0; i<10; i++) { arry[i]=i; } for(i=0; i<10; i++) printf("%d ",arry[i]); arry[9]=12; //add more to the array arry[10]=15; arry[11]=18; printf("\n\nThe new array \n"); //display the new, longer array with exact numbers for(i=0; i<12; i++) printf("%d ",arry[i]); printf("\n\nThe new array using size testing \n"); //display new array, but let the program decide its length for(i=0; i<(sizeof(arry)/sizeof(arry[0])); i++) printf("%d ",arry[i]); getch(); }
Code:The origional 0 1 2 3 4 5 6 7 8 9 The new array 0 1 2 3 4 5 6 7 8 12 15 18 The new array using size testing 0 1 2 3 4 5 6 7 8 12
Except arry[10]=15 doesn't work. You got "lucky", in that array[10] didn't cause a segfault, or memory access error, or similar. It "should" do so. (By "should" I mean morally, not that it actually will.)
"arry[10] = 15" gives undefined behaviour (as does "arry[11] = 18"). Undefined behaviour means anything is allowed to happen. "Anything" includes many possibilities, including the possibility that the program might appear to behave correctly. The catch is, the actual behaviour can change (eg if you feed your code to a different compiler).
The basic rule is that array elements should only be accessed for valid elements (in your case, arry[i] for i in the range 0 to 9 only). If you need to access other elements (i.e. indices outside the range 0 to 9) you need to specifically ensure the array is large enough to allow that or, alternatively, dynamically create the array of required size at run time BEFORE accessing it's elements.
As such, use say, malloc to create the array, and realloc to expand it.Originally Posted by SlyVixsky
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
In C, that never occurs automatically. If you want an array to expand, you need to implement the process by which expansion occurs. One way is that, when a user attempts to modify a non-existent element, that you create a new (bigger) array, copy the elements across to the new array, and destroy the original. Another (assuming your arrays are dynamically created with malloc/realloc/calloc) to do that is with realloc() - but you still need to implement the logic by which realloc() is called.
In C++, you can use standard containers to simplify some of that. However, the C++ standard containers need to be used in a particular manner - they still don't resize themselves simply through an attempt to modify or access an element.