LECTURE 3-2
C PROGRAMMING IV
March 24, 2016
Outline
• Dynamic Memory Allocation
• Structure Variables
• Arguments of main() function: argc, argv
• File I/O
Dynamic Memory Allocation
• Using the malloc() function, you can allocate as many memories to a pointer
variable as you want.
• This is useful when you do not know required size of an array before actual run of
the code.
• Example:
#include “stdio.h”
#include “stdlib.h”
#include “malloc.h”
int main()
{
float *a;
int n=10;
a=(float *)malloc(n*sizeof(float));
}
This code prepares an array of ten float type memory space.
Multi-dimensional malloc
• If you want to prepare a m x n matrix using malloc function, you can do it as follows.
#include “stdio.h”
#include “stdlib.h”
#include “malloc.h”
int main()
{
float **a;
int m=5, n=10;
int i;
a=(float **)malloc(m*sizeof(float *));
for(i=0;i<m; i++) a[i]=(float *)malloc(n*sizeof(float));
}
Definition of a Structure
• Structure in C means a group of variables.
• It is convenient to manage related variables by grouping them.
• Typically it is declared in user’s header file.
// Coordinate // rectangle
struct point { struct rect {
int x; int x;
int y; int y;
}; int width;
int height;
};
// complex number
struct complex {
double real; // 실수부 // employee
double imag; // 허수부 struct employee {
}; char name[20];
int age;
// date int gender;
struct date { int salary;
int month; };
int day;
int year;
};
Declaration of Structure Variables
• Structure is defined somewhere, and then is used as a variable type.
struct student { 구조체 정의
int number;
char name[20];
double height;
};
int main(void){
struct student s1; 구조체 변수 선언
...
}
Initialization of a Structure Variable
• A structure variable can be initialized using { }, and listing corresponding type values
inside the { } in the same order, separated by commas.
struct student {
int number;
char name[10];
double height;
};
struct student s1 = { 24, "Kim", 178.9 };
Access to the Members of a Structure
• Use ‘.’ operator next to the structure variable’s name, followed by the member
name.
• When the structure variables is declared as a pointer, use ‘->’ operator instead of ‘.’
s1.number = 26; // the integer member
strcpy(s1.name, "Kim"); // the string member
s1.height = 183.2; // the floating point number member
. Symbol is used
.
to access each
member of a
structure-typed
variable
Example of using a Structure
#define SIZE 3
struct student {
int number;
char name[20];
double score;
};
int main(void)
{
struct student list[SIZE];
int i;
for(i = 0; i < SIZE; i++)
{
printf(”Input your Student ID number.");
scanf("%d", &list[i].number);
printf(”Your name?");
scanf("%s", list[i].name);
printf(”Your score");
scanf("%lf", &list[i].score);
}
for(i = 0; i< SIZE; i++)
printf(”Student Num: %d, Name: %s, Score: %f\n", list[i].number, list[i].name, list[i].score);
return 0;
}
Arguments of main() function
• Command line input of arguments can be done by using argc, *argv[].
• argc means the number of arguments, including the execution file itself.
• *argv[] stores actual arguments.
#include “stdio.h”
#include “stdlib.h”
main(int argc, char *argv[])
{
float x; int y;
printf(“Number of arguments =%d\n”,argc);
printf(“%s %s\n”,argv[1],argv[2]);
x=atof(argv[1]);
y=atoi(argv[2]);
printf(“%g %d\n”,x,y);
}
File I/O
• A file pointer should be declared by FILE type
• fopen(), and fclose() open and close a file.
• Most standard input/output functions fscanf(), and fprintf()
• fgets() reads a whole line.
#include “stdio.h”
#include “stdlib.h”
main()
{
char name[100]; FILE *in, *out; float x,y,z;
in = fopen(“inFile”,”r”);
fgets(name,100,in);
fscanf(in,“%g %g %g”,&x,&y,&z);
fclose(in);
out = fopen(“outFile”,”w”);
fprintf(out,”Square root of %s\n”,name);
fprintf(out,”%g \n”,x*x+y*y+z*z);
fclose(out);
}
Exercise
Exercise 1
Make a subroutine (i.e. a function), which gets a 3x3 2D array as its argument, and
assign 2*i+3*j to the ij element of the array. Call this function from main() and print
the results on the screen.
Homework
Question 1
Using scanf(), get two integers m and n. Dynamically allocate m x n memories to a
double pointer variable. Initialize it by 0.0 for all elements using a for-loop. Make a
function which gets the 2D array variable, m, and n as its arguments. In this
function, you assign i*i+j*j-i*j to ij-element. Let this function calculate the sum of
all elements and return it. In the main(), you open a file with any name you want,
and save the array elements and their sum in the file in the following form.
a12 a12 a13 … a1n
a21 a22 a23 … a2n
…………..
am1 am2 am3... Amn
Sum=…
Due 24:00 on March 31, 2016
END OF
LECTURE 3-2