Embedded Data Structure
(Lecture Note 4)
Revisit C Programming (3)
2025. 3. 13
Prof. Wonjun Kim
School of Electrical and Electronics Engineering
Deep Computer Vision Lab.
Review of the Last Class
Content
Basic concept of advanced operations for pointers
• Relationship between array and pointer
• Pointer and function / void pointer (casting)
• Function pointer / pointer of pointer
※ Remember the definition of “pointer of pointer”
• Interpret the following statement :
int p; / int *p; / int **p;
Deep Computer Vision Lab.
1
Pointer and String (1/5)
Content
What is “string” ?
• String vs. Character (What is the main difference ?)
- String : a set of character ? (represented by double quotes “ “)
cf) Character is represented by single quotes ‘ ‘
Examples of string :
“a”, “ “, “#$%!”, “안녕하세요” // blank can be string
“Welcome to C programming !”
• How can we store string in C ?
- We can use char (array)
※ Size of array needs to be set as LENGTH + 1 due to NULL
H E L L 0 \0 Why do we need it ?
Deep Computer Vision Lab.
2
Pointer and String (2/5)
Content
What is “string” ? – cont’d
• There are various approaches for initializing strings
char str[4] = {‘a’, ‘b’, ‘c’, ‘\0’}; str a b c \0
char str[4] = “abc”; str a b c \0
char str[6] = “abc”; str a b c \0 \0 \0
char str[4] = “”; str \0 \0 \0 \0
char str[] = “abc”; str a b c \0
- String constant (e.g., “abc”) NULL is automatically set by compiler
- [] assigns automatically the size of the corresponding array
Deep Computer Vision Lab.
3
Pointer and String (3/5)
Content
String store by pointer
• String constant : string included in your source (e.g., “abc”)
※ String constant is stored in the text segment
read only ! (cannot modified)
char *p = “Hello”;
1 3 3 H e l l o \0
p
Data segment Text segment
char *p = “Hello”;
strcpy(p, “Goodbye”); // string copy Is it Ok ? (see the above)
Deep Computer Vision Lab.
4
Pointer and String (4/5)
Content
String store by pointer – cont’d
• String constant : a kind of address
char *p = “Hello”;
p = “Goodbye”; // copy address ! (not the context)
1 3 3 H e l l o \0
p
9 G o o d b y e \0
Data segment
Text segment
char p[] = “Hello”;
strcpy(p, “Goodbye”);
9 G o o d b y e \0 Data segment
Deep Computer Vision Lab.
5
Pointer and String (5/5)
Content
Example : string print-out
• Consider the following example with the call-by-reference
#include <stdio.h> #include <stdio.h>
void set_pointer(char *q); void set_pointer(char **q);
char *st = “Good !”; char *st = “Good !”;
void main() Your void main()
{ selection {
char *p = “aaa”; char *p = “aaa”;
set_pointer(p); set_pointer(&p);
printf(“%s\n”, p); printf(“%s\n”, p);
} }
void set_pointer(char *q) void set_pointer(char **q)
{ {
q = st; *q = st;
} }
Deep Computer Vision Lab.
6
Array of Pointers (1/3)
Content
Array containing pointers as elements
• Interpret the following declaration :
Contain int-type
pointers int *p[10];
p is an array ([ ] > *)
p // Initialize array
… 1 2 3 4 5 6 7 8 9 0 …
int a, b, c, d;
int *p[5] =
What do those mean ? {&a, &b, &c, &d};
Deep Computer Vision Lab.
7
Revisit: Function Pointer (1/2)
Content
Function includes the address
• Note : function pointer indicates function and its parameters
Return type
int (*fptr)(int);
Parameters
(integer)
fptr is a pointer
#include <stdio.h>
void sum(float a, float b)
{ * Name of function
void sum(float, float); pointer
float result;
result = a + b; : not limited
}
(Function pointer)
Deep Computer Vision Lab.
8
Revisit: Function Pointer (2/2)
Content
Implementation example
• Implement the following code (not function call, but function pointer)
#include <stdio.h> void main()
{
int add(int x, int y) int result;
{ Declaration
int result;
result = x + y; For add
return result; result = fptr(10, 20);
} printf(“%d\n”, result);
int sub(int x, int y) For sub
{ result = fptr(10, 20);
int result; printf(“%d\n”, result);
result = x – y; }
return result;
} Make complete your code
Deep Computer Vision Lab.
9
Array of Function Pointers
Content
Definition of array of function pointers
• Add [] to indicate the array !
int (*fptr[4])(int);
fptr is an array ([ ] > *) for what ?
fptr
fptr[0] int add(int, int); ※ Why do we use this ?
fptr[1] int sub(int, int); Routine for “menu”
fptr[2] int mul(int, int);
※ Compare it with array of
fptr[3] int div(int, int); pointers
Deep Computer Vision Lab.
10
File Open/Close (1/3)
Content
fopen()/fclose() functions
• We can read a specific file into our source code
FILE *fopen(const char *name, const char *mode)
int fclose(FILE *stream)
file name
FILE *fp; mode
fp = fopen(“test.txt”, “w”); [ Example of modes ]
fclose(fp);
Mode Description
If file does not exist, “r” Read file
“w” Write (generate) file
fopen() returns NULL
“rt” / “rb” Read file (text/binary)
“wt”/ ”wb” Write file (text/binary)
Deep Computer Vision Lab.
11
File Open/Close (2/3)
Content
Implementation examples
• Check the following code (file read)
#include <stdio.h>
void main()
[ Useful functions ]
{
FILE *fp; Mode Description
fp = fopen(“test.txt”, “r”); int foef(FILE Return true at the end
*strem)
if(fp == NULL)
int rename(const Change the file name
printf(“There is no file\n”); char *o, const
else char *n)
printf(“Success !\n”);
FILE *tmpefile() Temporary file gen.
}
int ferror(FILE If error occurs, return
※ remove(filename) *strem( true
- Return 0 when removing a file
- Return -1 otherwise
Deep Computer Vision Lab.
12
File Open/Close (3/3)
Content
Formatted read/write
• What is difference btw fprintf() and printf() ?
int fprintf(FILE *fp, const char *format, …);
int fscanf(FILE *fp, const char *format, …);
void main() if((fp = fopen(“score.txt”, “r”) != NULL)
{ {
FILE *fp; while(!feof(fp)){
fscanf(fp, “%d”, &score);
fp = fopen(“score.txt”, “w”); avg += score;
while(1){ num++;
if(score < 0) break; }
printf(“Input score: “); avg /= (float)num;
scanf(“%d”, &score); printf(“average = %f\n”, avg);
fprintf(fp, “%d\n”, score); }
}
fclose(fp); Complete code for average !
Deep Computer Vision Lab.
13
Summary
Content
Basic concept of advanced operations for pointers
• Pointer and string
- Inputs of functions related to string are pointers in general
- Remember the relation between pointer of pointer and string
• Pointer and its applications
- Array of pointers, function pointer, and array of function pointers
• Remember how to read/write a file
Deep Computer Vision Lab.
14