new malloc . , new, (""). , new, , :
int_ptr = new int(3);
new meset(), <mem.h>. 3 : , , .
#include <string.h>
#include <stdio.h>
#include <mem.h>
int main(void) {
char buffer[] = "Hello world\n";
printf(" memset: %s\n", buffer);
memset(buffer, '*', strlen(buffer) - 1);
printf(" memset: %s\n", buffer);
return 0;
}
1). - , .
2). - .
3). . - .
1:
int *a1, *a2;
a1 = new int[1000]; //
... //-
a1 = a2; // - 1
// -
2:
void func(void)
{
int * a1;
a1 = new int[1000];
...
} // -
// a1, , ,
// .
:
int * c;
void func1(void) {
int * a1;
a1 = new int[1000];
c = a1; // a1 func1
... //
}
void func2(void)
{
...
delete []c;
}
, 1 :
int *a = new int[1000];
int *a1 = a;
...
delete []a;
d = *(a1+50); // - a1
//!
...
, , ( ), . , , .
. . .
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main(void) {
int i,j,n;
float varsum=0;
struct stud { char fio[15]; char name[10];
struct exam { int val1; int val2;
int val3; int val4;}estimate;
float midle;
|
|
};
puts( - );
cin>>n;
struct stud *pgroup=new struct stud[n];
for(i=0; i<n; i++) {
printf( %d \n,i+1);
cout<< :;
gets(pgroup->fio);
cout<< :;
gets(pgroup->name);
cout<< 1- :;
gets(pgroup->estimate.val1);
cout<< 2- :;
gets(pgroup->estimate.val2);
cout<< 3- :;
gets(pgroup->estimate.val3);
cout<< 4- :;
gets(pgroup->estimate.val4);
pgroup->middle = (pgroup->estimate.val1+pgroup->estimate.val2+ pgroup->estimate.val3+ pgroup->estimate.val4)/4;
cout<< << pgroup->fio<< pgroup->name<< pgroup->midle<<endl;
varsum+= pgroup->midle;
}
cout<< <<varsum/n<<endl;
delete[] pgroup;
}
"" . stadio.h . #defile FILE struct iobuf
FILE . typedef .
typedef struct iobuf FILE
fopen()
.
FILE *in; //
fopen(), <stdio.h>.
in = fopen("test", "r");
1 -
2 -
"r"- "r+"-
"w"- "w+"- , ,
"a"- "a+"- , ,
"b"-
"t"-
in "test". .
( , ), 0.
if((in=fopen("test", "r"))==0)
puts(" ");
in=fopen("test", "r");
if (!in)
puts(" ");
fclose()
fclose(FILE *stream); // , 0 -1.
19.3 / fgetc(), fputc()
1.
int fgetc(FILE *stream);
2.
int fputc(int c, FILE *stream);
.
ch=fgetc(in);
fputc(ch,out);
# include <stadio.h>
void main(void){
FILE *in,*out;
char ch;
if((in=fopen("prog1", "r"))==0)
fputs(" prog1");
if((out=fopen("prog2", "w"))==0)
fputs(" prog2);
while((ch=getc(in))!=EOF) //End f File dos.h
fputc(ch, out);
fclose(in);
fclose(out);
}
19.4 /
1.
int fprintf(FILE *stream,.c,arg1,)
|
|
.
2.
int fscanf(FILE *stream,.c,&arg1,)
.
# include<stadio.h>
void main(void){
FILE*in;
int age;
in=fopen("prog1", "r");
fscanf(in,"%d",&age);
fclose(in);
in=fopen("prog2", "w");
fprintf(in," %d\n",age);
fclose(in);
}
, , printf().
19.5 /
1. .
char* fgets(char *str, int n, FILE *stream);
n-1 0 , \n.
void main(void){
FILE*in;
char string[80];
in=fopen("story", "r");
while(fgets(string,80, in)!=0)
puts(string);
}
'\n' 80-1 . EOF 0.
2.
int fputs(char *str,FILE *stream);
y=fputs( ,in);
y- , EOF, fputs() - EOF . fputs '\n' e .
. .
.