Embedded Data Structure
(Lecture Note 3)
Revisit C Programming (2)
2025. 3. 11
Prof. Wonjun Kim
School of Electrical and Electronics Engineering
Deep Computer Vision Lab.
Review of the Last Class
Content
Basic concept of array and pointer
• Array and pointer are same except for …
• Remember usage of array and pointer
• Remember the definition of pointer
: Pointer is a local variable containing the address of the variable
※ Please draw the procedure related to pointers !
Deep Computer Vision Lab.
1
Pointer and Array (1/2)
Content
The name of array is “pointer”
• Both of them are the same except for “static” vs. “dynamic”
Indicate the same memory space with two different ways
#include <stdio.h> #include <stdio.h> Memory
void main() void main()
{ { a[0] a
int a[] = {1, 2, 3, 4, 5}; int a[] = {1, 2, 3, 4, 5}; a[1]
printf(“%d\n”, &a[0]); printf(“%d\n”, a); a[2]
printf(“%d\n”, &a[1]); printf(“%d\n”, a+1); a[3] a+3
printf(“%d\n”, &a[2]); printf(“%d\n”, *a); a[4]
printf(“%d\n”, a); printf(“%d\n”, *(a+1));
} }
&a[i] = ? / a[i] = ? (think about the difference)
Deep Computer Vision Lab.
2
Pointer and Array (2/2)
Content
The name of array is “pointer” – cont’d
• You need to access memory with freely pointers
Indicate the same memory space with two different ways
#include <stdio.h>
void main()
{
What happens exactly ?
int a[] = {1, 2, 3, 4, 5};
int *p; Draw the procedure for this !
p = a;
printf(“%d %d %d\n”, a[1], a[2], a[3]);
printf(“%d %d %d\n”, p[1], p[2], p[3]);
p[1] = 7;
p[2] = 8;
} p[3] = 9;
Deep Computer Vision Lab.
3
Pointer and Function (1/4)
Content
Pointer also can be employed as a parameter
• We provide the “address” (not the “value”) for functions
Formulation is the same as the function prototype
void main() void compute_sum(int *score, int N)
{ {
… …
compute_sum(grade, int N); sum += score[ j];
… …
} }
[ [Link] ] [ [Link] ]
※ To do this, grade should be defined as a pointer, e.g., int *grade;
(Can you explain the procedure by arrows ?)
※ Once again, pointers can be directly represented using the array
Deep Computer Vision Lab.
4
Pointer and Function (2/4)
Content
Call-by-value vs. Call-by-reference
• Call-by-value : copy value (function inside)
※ Example : change two variables using a swap function defined as
a b (copy)
#include <stdio.h>
void swap(int x, int y)
void swap(int x, int y); {
int temp;
void main()
{ temp = x;
int a = 100, b = 200; x = y;
printf(“a = %d, b = %d\n”, a, b); y = temp;
swap(a, b); }
printf(“a = %d, b = %d\n”, a, b);
} [ swap function ]
What is the final result ? Is it changed ?
Deep Computer Vision Lab.
5
Pointer and Function (3/4)
Content
Call-by-value vs. Call-by-reference – cont’d
• Call-by-reference : copy address !
※ Example : change two variables using a swap function defined as
#include <stdio.h>
void swap(int *x, int *y)
void swap(int x, int y); {
int temp;
void main()
{ temp = x;
int a = 100, b = 200; x = y;
printf(“a = %d, b = %d\n”, a, b); y = temp;
swap( , ); } Modify it !
printf(“a = %d, b = %d\n”, a, b);
} [ swap function ]
What is the final result ? Is it changed ?
Deep Computer Vision Lab.
6
Pointer and Function (4/4)
Content
Some examples
• Try to apply pointers to your functions !
- Fill out blanks (understand the pointer operations)
#include <stdio.h> #include <stdio.h> void sub(int x[], int n)
{
int *add(int x, int y); void sub(int x[], int n); for(int j = 0; j<n; j++)
x[ j] -= 1;
void main() void main() }
{ { Array
int *result; int a[3] = {1, 2, 3};
result = add(5, 9);
} sub(a, 3); void sub( , int n)
{
int *add(int x, int y) printf(“%d %d %d\n”, for(int j = 0; j<n; j++)
{ a[0], a[1], a[2]); -= 1;
int *z; } }
*z = x + y;
return
Pointer
}
Deep Computer Vision Lab.
7
Function Pointer
Content
Function name as a pointer
• Function pointer : indicating the “code” not the ”data”
#include <stdio.h>
Consider following sentences :
int function_A(int N)
{ 1) fptr = function_A
int a;
for(a = 0; N > 0; N--) 2) printf(“%d\n”, function_A(100));
a += N; printf(“%d\n”, fptr(100));
return a;
}
This will be frequently employed with
What does this function do ?
void pointer
Function pointer : int (*fptr) (int);
Make a generalized version
※ Bracket is required
※ Usage : type (pointer) (type)
Deep Computer Vision Lab.
8
Void Pointer (1/2)
Content
Void : no type (empty)
• It does not indicate the data type Why do we use it ?
void *vptr;
int a; Key advantage of void pointers :
a = *vptr; It is useful for making the generalized function
a =*(int *)vptr;
(i.e., it works regardless of data types)
a = *(vptr + 1);
a= Fill out the blank using pointer casting
* Void pointer cannot use the dereference, why ?
* Void pointer cannot use the pointer operators, why ?
Type casting should be employed !
Deep Computer Vision Lab.
9
Void Pointer (2/2)
Content
Application : function generalization
• Make complete the generalized version of swap function
#include <stdio.h> void main()
#include <stdlib.h> {
char c1 = ‘A’, c2 = ‘B’;
void swap(void *a, void *b, int n) int i1 = 3, i2 = 9;
{ float f1 = 3.3, f2 = 9.9;
void *t;
t = malloc(n); // for char type
memcpy(t, a, n); swap( );
memcpy(a, b, n);
memcpy(b, t, n); // for int type
free(t); swap( );
}
// for float type
Why using “n” ? swap( );
}
Deep Computer Vision Lab.
10
Pointer of Pointer (1/4)
Content
Pointer of pointer is also an address
• Once again, interpret clearly the following statement :
int **q;
#include <stdio.h>
3 p ?
void main()
{ 4 q ?
int j = 9;
int *p = &j;
int **q = &p;
7 9 j ?
*p = 10; 8 39 ?
**q = 9;
}
Deep Computer Vision Lab.
11
Pointer of Pointer (2/4)
Content
Let’s consider the cartesian coordinate (or matrix)
• Example : think about scanning a given image
int S[3][5]; S[0][0] S[0][1] S[0][2] S[0][3] S[0][4]
S[1][0] S[1][1] S[1][2] S[1][3] S[1][4]
S[2][0] S[2][1] S[2][2] S[2][3] S[2][4]
How are such arrays assigned in the memory space (physically) ?
S[0][0] S[0][1] S[0][2] S[0][3] S[0][4] S[1][0] … S[2][0] S[2][1] S[2][2] S[2][3] S[2][4]
1st row 3rd row
This is because the structure of the memory is …
Deep Computer Vision Lab.
12
Pointer of Pointer (3/4)
Content
Initialization of the two-dimensional array
• Consider the following example :
int array[3][3] = int array[][3] =
{ {1, 2, 3}, { {1, 2, 3}, Which one is correct ? Why ?
{4, 5, 6}, {4, 5, 6}, What is array[2][1], array[1][1] ?
{7, 8, 9} }; {7, 8, 9} };
Starting address what is this ?
… 1 2 3 4 5 6 7 8 9 0 …
* Name “array” indicates three (int types) pointers in the memory space !
Where do array+1 and array+2 indicate in the memory ?
* Think about the relationship with the pointer of pointer
Deep Computer Vision Lab.
13
Pointer of Pointer (4/4)
Content
2-dimensional array
• Note : memory structure is still linear (this indexing is just for us !)
S S[0] S[0][0] S[0][1] S[0][2] S[0][3]
S[1] S[1][0] S[1][1] S[1][2] S[1][3]
S[2] S[2][0] S[2][1] S[2][2] S[2][3]
? Pointer
#include <stdio.h>
void main()
{ Fill out the blank only using a pointer !
int a[2][2] = { {1,2}, {3,4} };
printf(“%d\n”, ); // print out a value at (1,0) (or (1,1))
}
Deep Computer Vision Lab.
14
Practice Example
Content
Compute the mean of a specific row (or column)
• Why ? memory management in the embedded system
float get_row_avg(int m[][COLS], int r){
m[0][0] int *start, *end;
float avg = 0;
start = ;
end = ;
while(start <= end){
#define ROWS 4 avg += ;
#define COLS 3 ;
}
int m[ROWS][COLS] = {{1, 3, 5},
{9, 7, 3}, {3, 3, 9}, {2, 8, 6}}; avg /= COLS;
return avg;
}
Deep Computer Vision Lab.
15
Summary
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”
Deep Computer Vision Lab.
16