반응형
// Pointers (Example 1.1)
/* Introduction Example */
#include <stdio.h>

int main() {
	int x;
	int* ptr_x;
	x = 100;
	ptr_x = &x;
	printf("x: %d, &x : %p\n", x, &x);
	printf("*ptr_x: %d, ptr_x: %p\n", *ptr_x, ptr_x);
	*ptr_x = 200;
	printf("x: %d, *ptr_x : %d\n", x, *ptr_x);

	return 0;
}
// Pointers (Example 1.2)
/* Address of Arrays */
#include <stdio.h>

int main() {
	int x = 1, y = 2, z[2];
	int* ptr;
	ptr = &x;
	printf("ptr: %p\n", ptr);
	y = *ptr;
	printf("y: %d\n", y);
	*ptr = 0;
	printf("ptr: %d\n", *ptr);
	ptr = &z[0];
	printf("ptr : %p\n", ptr);
	printf("z[0]: %d\n", z[0]);

	return 0;
}
// Pointers (Example 1.3)
/* Arithmetic 1 */
#include <stdio.h>

int main() {
	int x, y, sum;
	x = 100;
	y = 200;
	int* ptr_x, * ptr_y, * ptr_sum;
	ptr_x = &x;
	ptr_y = &y;
	ptr_sum = &sum;
	*ptr_sum = *ptr_x + *ptr_y;
	printf("ptr_x: %p, ptr_y: %p\n", ptr_x, ptr_y);
	printf("*ptr_x: %d, *ptr_y: %d\n", *ptr_x, *ptr_y);
	printf("*ptr sum : %d\n", *ptr_sum);

	return 0;
}
// Pointers (Example 1.4)
/* Arithmetic 2 */
#include <stdio.h>

int main() {
	int x = 100;
	int* ptr_x;
	ptr_x = &x;
	*ptr_x = *ptr_x + 200;
	printf("x: %d\n", x);
	printf("*ptr_x: %d, ptr_x: %p\n", *ptr_x, ptr_x);

	return 0;
}
// Pointers (Example 1.5)
/* Operator */
#include <stdio.h>

void main() {
	char c = 'A';
	printf("%p\n", &c);
	printf("%c %c\n", c, *&c);
}
 
/*
000000A7368FF744
A A
*/
// Pointers (Example 2.1)
/* Pointer and Memory (char) */
#include <stdio.h>

void main() {
	char c = 'A';
	printf("%ld\n", sizeof(c));
	printf("%c\n", c);
}
/*
1
A

It says sizeof(c) is 1.
It means "char" uses 1 Byte in memory.

c (char) // 65 (='A')
ASCII cod of "A"
Address (or Starting Address)
*/
// Pointers (Example 2.2)
/* Pointer and Memory (int) */
#include <stdio.h>

void main() {
	int a = 10;
	printf("%ld\n", sizeof(a));
	printf("%d\n", a);
}

/*
4
10

It says sizeof(a) is 4.
It means "int" uses 4 Bytes in memory.
*/
// Pointers (Example 2.3)
/* Pointer and Memory (double) */
#include <stdio.h>

void main() {
	double a = 10;
	printf("%ld\n", sizeof(a));
	printf("%g\n", a);
}

/*
8
10

It says sizeof(a) is 8.
It means "double" uses 8 Bytes in memory.
*/
// Pointers (Example 2.4)
/* Pointer and Memory (char) */
#include <stdio.h>

void main() {
	char a = 'A';
	char b = 'B';
	printf("%p\n", &a);
	printf("%p\n", &a + 1);
	printf("%p\n", &b);
	printf("%p\n", &b + 1);
}

/*
0000008EEB8FF564
0000008EEB8FF565
0000008EEB8FF584
0000008EEB8FF585
*/
// Pointers (Example 2.5)
/* Pointer and Memory (int) */
#include <stdio.h>

void main() {
	int a = 1;
	int b = 2;
	printf("%p\n", &a);
	printf("%p\n", &a + 1);
	printf("%p\n", &b);
	printf("%p\n", &b + 1);
}

/*
0000004E72BCF784
0000004E72BCF788
0000004E72BCF7A4
0000004E72BCF7A8
*/
// Pointers (Example 2.6)
/* Type Conversion in Address */
#include <stdio.h>

void main() {
	char c = 'A';
	printf("%p %p %p\n", &c, (char*)&c, (int*)&c);
	printf("%p %p %p\n", &c + 1, (char*)&c + 1, (int*)&c+ 1);
}

/*
000000B8210FF8F4 000000B8210FF8F4 000000B8210FF8F4
000000B8210FF8F5 000000B8210FF8F5 000000B8210FF8F8
*/
// Pointers (Example 2.7)
/* Sizes of Individual Types(char and int) 1 */
#include <stdio.h>

void main() {
	char c = 'A';
	char* cp = &c;
	int n = 10;
	int* np = &n;
	printf("%c %c\n", c, *cp);
	printf("%ld %ld\n", sizeof(char), sizeof(char*));
	printf("%ld %ld\n", sizeof(c), sizeof(np));
	printf("\n");
	printf("%d %d\n", n, *np);
	printf("%ld %ld\n", sizeof(int), sizeof(int*));
	printf("%ld %ld\n", sizeof(n), sizeof(np));
}

/*
A A
1 8
1 8

10 10
4 8
4 8
*/
// Pointers (Example 2.8)
/* Sizes of Individual Types(char and int) 2 */
#include <stdio.h>

void main() {
	char c = 'A';
	char* cp = &c;
	int n = 10;
	int* np = &n;
	printf("%ld %ld %ld\n", sizeof(c), sizeof(*cp), sizeof(cp));
	printf("%ld %ld %ld\n", sizeof(n), sizeof(*np), sizeof(np));
}

/*
1 1 8
4 4 8
*/
// Pointers (Example 3.1)
/* Double Pointer */
#include <stdio.h>

void main() {
	char c = 'A';
	char* cp;
	char** cpp;
	cp = &c;
	cpp = &cp;
	printf("%c %c %c\n", c, *cp, **cpp);
	printf("%p %p %p\n", &c, cp, *cpp);
	printf("\n");
	int n = 10;
	int* np;
	int** npp;
	np = &n;
	npp = &np;
	printf("%d %d %d\n", n, *np, **npp);
	printf("%p %p %p\n", &n, np, *npp);
}

/*
A A A
0000006C438FF984 0000006C438FF984 0000006C438FF984

10 10 10
0000006C438FF9E4 0000006C438FF9E4 0000006C438FF9E4
*/
// Pointers (Example 3.2)
/* Multiple Pointer */
#include <stdio.h>

void main() {
	int n = 10;
	int* np;
	int** npp;
	int*** nppp;
	np = &n;
	npp = &np;
	nppp = &npp;
	printf("%d %d %d %d\n", n, *np, **npp, ***nppp);
	printf("%ld %ld %ld %ld\n", sizeof(int), sizeof(int*), sizeof(int**), sizeof(int***));
	printf("%ld %ld %ld %ld\n", sizeof(n), sizeof(np), sizeof(npp), sizeof(nppp));
}

/*
10 10 10 10
4 8 8 8
4 8 8 8
*/
/* Pointers and Arrays (Introduction) */
#include <stdio.h>

void main() {
	int x[5] = { 1, 2, 3, 4, 5 };
	int* p;
	p = &x;

	printf("p addr : %p\n", p);
	printf("p addr : %p\n", &x[0]);
	printf("p addr : %p\n", p + 1);
	printf("p addr : %p\n", &x[1]);
	printf("p addr : %p\n", p + 2);
	printf("p addr : %p\n", &x[2]);
	printf("p addr : %p\n", p + 3);
	printf("p addr : %p\n", &x[3]);
	printf("p addr : %p\n", p + 4);
	printf("p addr : %p\n", &x[4]);
}

/*
p addr : 0000002D79AFFA98
p addr : 0000002D79AFFA98
p addr : 0000002D79AFFA9C
p addr : 0000002D79AFFA9C
p addr : 0000002D79AFFAA0
p addr : 0000002D79AFFAA0
p addr : 0000002D79AFFAA4
p addr : 0000002D79AFFAA4
p addr : 0000002D79AFFAA8
p addr : 0000002D79AFFAA8

- x : constant pointer
	  pointing the first element x[0]
- Thus, the value of x is 1000, i.e., x = &x[0] = 1000
- If we declare p as an integer pointer (int* p;)
  the pointer p can point to the array x by p = x; (equivalent to p = &x[0])
  *(p+3) gives the value of x[3], i.e., 4.
 */
// Pointers (Example 4.1)
/* Pointers and Arrays */
#include <stdio.h>

int main() {
	int* p, sum = 0, i;
	int x[5] = { 5, 9, 6, 3, 7 };
	i = 0;
	p = x;
	while (i < 5) {
		printf("x[%d], %d, %u\n", i, *p, p);
		sum += *p;
		i++, p++;
	}
	printf("Sum : %d, &x[0]: %u, p: %u\n", sum, &x[0], p);

	return 0;
}

/*
x[0], 5, 1626338792
x[1], 9, 1626338796
x[2], 6, 1626338800
x[3], 3, 1626338804
x[4], 7, 1626338808
Sum : 30, &x[0]: 1626338792, p: 1626338812
*/
// Pointers (Example 4.1)
/* Pointers and Arrays (Character Strings) */
#include <stdio.h>

void main() {
	char x[5] = "good";
	char* y = "good";
}

/*
It is more convenient to declare the elements of 
an array as pointer variables than to count them one by one.
*/
/* Pointers and Functions (Example 5.1) */
#include <stdio.h>

void mathoperation(int a, int b, int* sum, int* dif);

int main() {
	int x = 20, y = 10, s, d;
	mathoperation(x, y, &s, &d);
	printf("s : %d, d: %d\n", s, d);
	return 0;
}

void mathoperation(int a, int b, int* sum, int* dif) {
	*sum = a + b;
	*dif = a - b;
}

/*
s : 30, d: 10
*/
/* Pointers and Functions (Example 5.2) */
#include <stdio.h>

void exchange(int*, int*);

int main()
{
	int x, y;
	x = 10, y = 20;
	printf("x: %d, y: %d\n", x, y);
	exchange(&x, &y);
	printf("x: %d, y: %d\n", x, y);
	return 0;
}

void exchange(int* a, int* b) {
	int t;
	t = *a;
	*a = *b;
	*b = t;
}

/*
x: 10, y: 20
x: 20, y: 10
*/
반응형

'Language > C & C++' 카테고리의 다른 글

BankingSystemver01.cpp  (0) 2022.09.26
C++  (0) 2022.09.24
Transfer an array to a function's arguments  (0) 2022.09.09
User-Defined Type  (0) 2022.09.08
Number guessing game in Python 3 and C  (0) 2022.08.31

+ Recent posts