Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int* readToZero(int *size);
- void printArray(int size, int* array);
- int main()
- {
- int size=0;
- int* array=NULL;
- array=readToZero(&size);
- addElement(array, &size);
- printArray(size, array);
- }
- int* readToZero(int *size)
- {
- int* arr = NULL;
- int currentSize = 0;
- int value;
- while(1)
- {
- printf("Value: ");
- scanf("%d", &value);
- if(value==0)
- break;
- arr=(int*)realloc(arr, (currentSize+1)*sizeof(int));
- *(arr+currentSize) = value;
- currentSize++;
- }
- *size = currentSize;
- return arr;
- }
- void printArray(int size, int* array)
- {
- int i;
- for(i=0; i<size; i++)
- printf("[%d]=%d\n", i, *(array+i));
- }
- void addElement(int* array, int* size)
- {
- int value;
- printf("Value: ");
- scanf("%d", &value);
- array=(int*)realloc(array, (*size+1)*sizeof(int));
- *(array+*size) = value;
- *size+=1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement