| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Pointers and arrays
I am trying to create a dynamic array - one that increases in element size when I encounter a new thing to be added. It copies over the old elements in the new array, but I find that the returning function does not seem to actually return the correct array. Can you help? The code is listed below:
#include <stdio.h> void arraycopy(int oldarray[], int newarray[], int index); void NNListBuild(int lipidIndex, int *NNInput, int *NNList, int arrayindex); void arraycopy(int oldarray[], int newarray[], int index) { int i; for (i=0;i<index;i++) { newarray[i] = oldarray[i]; //printf("copying: %d\n", newarray[i]); } } void NNListBuild(int lipidIndex,int *NNInput, int *NNList, int arrayindex) { int *newarray = new int[arrayindex+1]; arraycopy(NNInput, newarray, arrayindex); NNInput = newarray; NNInput[arrayindex] = 110; int i; for (i=0;i<arrayindex+1;i++) { //printf("copying: %d\n", NNInput[i]); } NNList = &NNInput[0]; for (i=0;i<arrayindex+1;i++) { printf("after copying: %d\n", NNList[i]); } //delete [] newarray; } int main(void) { int arrayindex; arrayindex = 4; int *NNList = new int[arrayindex]; int *NNOutput; NNList[0] = 1; NNList[1] = 2; NNList[2] = 3; NNList[3] = 4; NNListBuild(7,NNList,NNOutput,arrayindex); //arraycopy(oldarray,newarray,arrayindex); int i; for (i=0;i<arrayindex+1;i++) { printf("final output: %d\n", NNOutput[i]); } delete [] NNList; //delete [] newarray; printf("hello"); return 0; } |
|
#2
|
|||
|
|||
|
void NNListBuild(int lipidIndex, int *NNInput, int *&NNList, int arrayindex)
{ NNList = new int[arrayindex+1]; arraycopy(NNInput, NNList, arrayindex); NNList[arrayindex] = 110; } int main(void) { int arrayindex=4; int *NNList = new int[arrayindex]; int *NNOutput = NULL; NNList[0] = 1; NNList[1] = 2; NNList[2] = 3; NNList[3] = 4; NNListBuild(7,NNList,NNOutput,arrayindex); delete [] NNList; delete[] NNOutput; } |
|
#3
|
|||
|
|||
|
Thanks for that - however it does not seem to work - the returning elemnts of the Output array cannot be displayed leading to a segmentation fault. The code is as you sugggsted.
|
|
#4
|
|||
|
|||
|
Quote:
Code:
void arraycopy(int oldarray[], int newarray[], int index);
void NNListBuild(int lipidIndex, int *NNInput, int *&NNList, int arrayindex);
int main(int argc, char* argv[])
{
int arrayindex=4;
int *NNList = new int[arrayindex];
int *NNOutput = NULL;
NNList[0] = 1;
NNList[1] = 2;
NNList[2] = 3;
NNList[3] = 4;
NNListBuild(7,NNList,NNOutput,arrayindex);
for (int i=0; i<5; i++)
{
printf("\n%d.) %d", i, NNOutput[i]);
}
delete[] NNList;
delete[] NNOutput;
getchar();
return 0;
}
void arraycopy(int oldarray[], int newarray[], int index)
{
for (int i=0; i<index; i++)
{
newarray[i] = oldarray[i];
}
}
void NNListBuild(int lipidIndex, int *NNInput, int *&NNList, int arrayindex)
{
NNList = new int[arrayindex+1];
arraycopy(NNInput, NNList, arrayindex);
NNList[arrayindex] = 110;
}
I do not receive no errors with this code. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Pointers and arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|