포인터 변수의 선언 int *ptr; int *ptr1, *ptr2; char *ptr3;

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation
Advertisements

CSE 251 Dr. Charles B. Owen Programming in C1 Dynamic Memory Allocation Dynamic memory allocation – How to allocate memory for variables (esp. arrays/strings)
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Introduction to Pointers These Slides NOT From Text.
Fundamental data types
Tutorial #7 Summer sizeof int main() { char x; sizeof(x); sizeof(int); }
1 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
The fundamental data type 제 4 주 강의. Declarations, Expressions, and Assignments Declaring the type of a variable  set an appropriate amount of space in.
Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
1 Review of Chapter 6: The Fundamental Data Types.
What are Pointers? Different from other normal variables which can store values. pointers are special variables that can hold the address of a variable.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers, Arrays, Multidimensional Arrays Pointers versus arrays – Lots of similarities How to deal with.
CSE 425: Target Machine Architecture Target Machine Details Many architectures can be similar in overall structure –E.g., Von Neumann with CISC instruction.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
프로그래밍 기초와 실습 Chapter 6 The Fundamental Data Types.
ECE Application Programming
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
쉽게 풀어쓴 C언어 Express 제14장 포인터 활용 C Express.
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Array of pointers We can have an array whose members are pointers, in this example pointers-to-int. int* data[3]; int i; int x = 5; int y = 89; int z =
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers. Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Dynamic memory allocation and Intraprogram Communication.
Functions and Pointers
2016.
Functions and Pointers
14th September IIT Kanpur
Dynamic memory allocation and Intraprogram Communication
Introduction to the C Language
C Program Design Data Types
Pointers.
Programming and Data Structures
Pointers and dynamic memory
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Dynamic Memory Allocation
CS111 Computer Programming
CSC215 Homework Homework 04 Due date: Oct 14, 2016.
Dynamic Memory Allocation
Outline Defining and using Pointers Operations on pointers
Introduction to Problem Solving and Programming
CSCE 206 Lab Structured Programming in C
7. Pointers, Dynamic Memory
POINTER CONCEPT 4/15/2019.
CIS*2450 Advanced Programming Concepts
CSCE 206 Lab Structured Programming in C
POINTER CONCEPT 8/3/2019.
컴퓨터 프로그래밍 기초 - 13th : 마지막 수업 -
Presentation transcript:

포인터 변수의 선언 int *ptr; int *ptr1, *ptr2; char *ptr3;

포인터 변수의 크기(test1.c) #include <stdio.h> int main(void) { printf("char의 크기 : %d\n", sizeof(char)); printf("short의 크기 : %d\n", sizeof(short)); printf("long의 크기 : %d\n", sizeof(long)); printf("int의 크기 : %d\n", sizeof(int)); printf("float의 크기 : %d\n", sizeof(float)); printf("double의 크기 : %d\n", sizeof(double)); printf("\n"); printf("char형 포인터의 크기 : %d\n", sizeof(char*)); printf("short형 포인터의 크기 : %d\n", sizeof(short*)); printf("long형 포인터의 크기 : %d\n", sizeof(long*)); printf("int형 포인터의 크기 : %d\n", sizeof(int*)); printf("float형 포인터의 크기 : %d\n", sizeof(float*)); printf("double형 포인터의 크기 : %d\n", sizeof(double*)); return 0; }

포인터 변수의 사용 int *ptr = 메모리 주소; ptr[0] = 0; ptr[1] = 10; ... *(ptr+i), ptr[i] (ptr+i), &ptr[i] ptr ptr[0] ptr[1] ptr[2] ptr[3] ptr[4] ptr[5] ptr[6] ptr[7] ptr[8] ptr[9]

포인터 변수의 초기화 int *ptr = NULL;

메모리 할당과 해제 int *ptr = (int *)malloc(sizeof(int) * 10); ptr = (int *)calloc(10, sizeof(int)); ptr = (int *)realloc(ptr, 20*sizeof(int)); free(ptr); #include <malloc.h>

포인터와 배열의 비교(test2.c) #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX_INPUT 256 int main(void) { char input[MAX_INPUT]={0}; // 입력을 받기 위한 배열 char *output=NULL; // 출력을 저장하기 위한 포인터 int length, ch; fgets(input, MAX_INPUT-1, stdin); // 사용자로부터 문자열을 입력 받음 length = strlen(input)+1; // NULL을 포함한 문자열의 길이만큼 output = (char *)malloc(length); // 메모리를 할당 받음 for(ch=0 ; ch<length ; ch++) if(input[ch] >= 'a' && input[ch] <= 'z') output[ch] = input[ch] + 'A'-'a'; // 소문자면 대문자로 변환 else output[ch] = input[ch]; } printf("Input : %s", input); // 입력 값 표시 printf("Output: %s", output); // 출력 값 표시 free(output); // 메모리 반납 return 0;

포인터의 활용 (test3.c) #include <stdio.h> #include <malloc.h> int main(void) { int students, s, sum=0; int *score=NULL; printf(“Num. of students : "); scanf("%d", &students); score=(int *)malloc(sizeof(int)*students); printf(“Input student grade\n"); for(s=0 ; s<students ; s++) printf("%dNo.: ", s+1); scanf("%d", &score[s]); } printf("----------------------\n"); sum += score[s]; printf("%dNo.: %3d\n", s+1, score[s]); printf(“Total: %3d\n", sum); printf(“Grade: %3.2f\n", (double)sum/students); free(score); return 0;

& 연산자 int var; int *ptr; ptr = &var;

& 연산자 (test4.c) #include <stdio.h> int main(void) { int var = 100; int *ptr = NULL; ptr = &var; printf("var: %p\n", ptr); return 0; }

* 연산자 int result, var1=3, var2=5; result = var1 * var2; int *ptr; short var1=1, var2=2; short *ptr = NULL; ptr = &var1; var2 = *ptr; 1000 1001 var1=1 1002 1003 var2=1 1004 1005 1006 ptr=1000 1007 1008 1009 1010

포인터의 이동 int val = 5; int *ptr = NULL; ptr = &val; ptr = ptr+1;

포인터의 이동(test5.c) #include <stdio.h> int main(void) { char *ptrChar = NULL; short *ptrShort = NULL; int *ptrInt = NULL; printf("char pointer inc. : %d\n", ++ptrChar); printf("short pointer inc. : %d\n", ++ptrShort); printf("int pointer inc. : %d\n", ++ptrInt); return 0; }

[] 연산자 1000 1000 p[0] 1001 *ptr 1001 1002 1002 1003 p[1] *(ptr+1) 1003 1004 1004 1005 p[2] 1005 *(ptr+2) 1006 1006 1007 p[3] 1007 *(ptr+3) 1008 1008 1009 p[4] 1009 *(ptr+4) 1010 1010 1011 1011 1012 1012 1013 1013 1014 p=1000 1014 ptr=1000 1015 1015

배열과 포인터(test6.c) #include <stdio.h> int main(void) { short array[3]; short *ptr = array; ptr[0] = 0; ptr[1] = 10; ptr[2] = 20; printf("array reference:\t"); printf("%d, %d, %d\n", array[0], array[1], array[2]); printf("ptr reference:\t"); printf("%d, %d, %d\n\n", ptr[0], ptr[1], ptr[2]); printf("array address:\t %p\n", array); printf("ptr address:\t %p\n", ptr); return 0; }

포인터 연산 정리 변수에 & 연산자  포인터 포인터에 * 연산자  포인터가 가리키는 데이터 포인터에 * 연산자  포인터가 가리키는 데이터 포인터에 [] 연산자  포인터가 가리키는 위치부터 연속된 데이터 포인터의 덧셈, 뺄셈  포인터를 다음 데이터 위치로 이동

포인터에 익숙해지기 short array[3]; short *ptr = array; ptr[0] = 0; ptr[1] = 10; ptr[2] = 20; short array[3]; short *ptr = array; *ptr = 0; *(ptr+1) = 10; *(ptr+2) = 20; short array[3]; short *ptr = array; *ptr++ = 0; *ptr++ = 10; *ptr = 20; short array[3]; short *ptr = array; *ptr = 0; *(++ptr) = 10; *(++ptr) = 20; short array[3]; short *ptr = &array[0]; ptr[0] = 0; ptr[1] = 10; ptr[2] = 20;

실습과제 영어 단어를 입력 받아 A에 1점, B에 2점, C에 3점 이런 식으로 점수를 계산하는 프로그램을 작성하세요.