Attachment '23.0.2.c'
Download 1 #include <stdio.h>
2 #include <stdlib.h>
3 int main (int argc, char** argv )
4 {
5 int *p, *oldp;
6 int x;
7 int N = 5;
8 int n; // текущая длина массива (смещение первого свободного элемента)
9 p = malloc( N*sizeof(int) );
10 if ( p == NULL )
11 {
12 printf( "cannot allocate!\n" );
13 return 1;
14 }
15
16 scanf( "%d", &x );
17 n = 0;
18 while( x != 0 )
19 {
20 *(p+n) = x;
21 n = n + 1;
22 if( n == N ) {
23 p = realloc( p, N+5 );
24 if ( p == NULL )
25 {
26 printf( "cannot allocate more!\n" );
27 return 1;
28 }
29 N = N + 5;
30 }
31 scanf( "%d", &x );
32 }
33
34 printf( "%d elements entered\n", n );
35 printf( "%d elements allocated\n", N );
36
37 //free(p); // ТАК ДЕЛАТЬ НЕЛЬЗЯ!! (если только временно ;)
38 return 0;
39 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.- [get | view] (2011-09-26 08:35:26, 0.9 KB) [[attachment:23.0.2.c]]
- [get | view] (2011-09-26 08:35:26, 0.7 KB) [[attachment:23.0.3.c]]
You are not allowed to attach a file to this page.