C’ POINTERS Basic&Examples. Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++));

Slides:



Advertisements
Similar presentations
#include void main() { float x = 1.66, y = 1.75; printf(%f%f,ceil(x), floor(y)); }
Advertisements

Computer Programming Lecture 14 C/C++ Pointers
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Malloc Recitation By sseshadr. Agenda Macros in C Pointer declarations Casting and Pointer Arithmetic Malloc.
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Dynamic memory allocation
Data Structure Lecture-5
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Pointers. 2 A pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
CSC241 Object-Oriented Programming (OOP) Lecture No. 9.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
指標 Pointers.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Sort the given string, without using string handling functions.
Pointers in C Rohit Khokher
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Week #10 Kyle Dewey. Overview Meeting times Typos Project #3 tidbits Exam Review.
Pointer Review. Node { int value; Node * next; Node( int val =-1, Node * n = NULL) {value = val; next = n;} } Given a ordered linked list pointed to.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Plab 2003 Exercise 4. Pointers to pointers. Plab 2003 Exercise 4 2 Pointers to pointers (1)int i=3 (2)int j=4; (3)int k=5; 3 i: 4 j: 5 k: ip1: ip2: (4)int.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Tutorial #8 Summer strings #include int main() { char str1[] = {‘h’,’e’,’l’,’l’,’o’}; char str[] = {‘h’,’e’,’l’,’l’,’o’,’\0’}; char p[] = ”hello”;
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Summary. Data Types int A; I am declaring a variable A, which is an integer.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
Sahar Mosleh California State University San MarcosPage 1 Character String.
Charles Clute Tom Most Michael Hein. Strings in C  There is no String... But there’s hope! Strings are character arrays char volume[6]; char volume[6]
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
1 C Basics. 2 The C Language Spirit Made by professional programmers for professional programmers Very flexible, very efficient, very liberal Does not.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Pointers as arrays C++ Programming Technologies. Pointers vs. Arrays Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
Programming Languages and Paradigms
C++ Pointers and Strings
C++ Pointers and Strings
Presentation transcript:

C’ POINTERS Basic&Examples

Q:what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = array; printf(" first element: %i\n", *(array_ptr++)); printf("second element: %i\n", *(array_ptr++)); printf(" third element: %i\n", *array_ptr); Ans: first element: 45 second element: 67 third element: 89 P OINTER ARITHMETIC

Q: what’s the output? int array[] = { 45, 67, 89 }; int *array_ptr = &array[1]; printf("%i\n", array_ptr[1]); A: 89 I NDEXING

S TRUCT ’ S PTR struct foo { size_t size; char name[64]; }; struct foo my_foo; my_foo.size = sizeof(struct foo); struct foo *foo_ptr; (*foo_ptr).size = new_size; 或是 foo_ptr->size = new_size; struct foo **foo_ptr_ptr (*foo_ptr_ptr)->size = new_size; 或是 (**foo_ptr_ptr).size = new_size;

P OINTERS AND CONST const int *ptr_a; int const *ptr_b; int *const ptr_c; Q: true or false *ptr_a=42; *ptr_b=42; *ptr_c=42; ptr_c++; A: F T F

S TRINGS Q:what is the value of len and i? char str[] = "I am the Walrus"; size_t strlen(const char *str) { size_t len = 0; while(*(str++)) ++len; return len; } size_t strlen(const char *str) { size_t i; for(i = 0U; str[i]; ++i); return i; } A: both len and i is 15

WHAT IS THE ANSWER? #include int main(){ int a = 320; char *ptr; ptr =( char *)&a; printf("%d ",*ptr); getch(); return 0; } #include int main(){ int a = 320; char *ptr; ptr =( char *)&a; printf("%d ",*ptr); getch(); return 0; } 64

WHAT IS THE ANSWER? #include int main(){int i = 3; int *j; int **k; j=&i; k=&j; printf( “ %u %u %d ”,k,*k,**k); return 0; } #include int main(){int i = 3; int *j; int **k; j=&i; k=&j; printf( “ %u %u %d ”,k,*k,**k); return 0; } (A) j Address, i Address,3 (B) j Address, 3, 3 (C) 3, 3, 3 (D) Compilation error (E) None of above (A) j Address, i Address,3 (B) j Address, 3, 3 (C) 3, 3, 3 (D) Compilation error (E) None of above

WHAT IS THE ANSWER? #include void main(){ char *ptr1 = NULL; char *ptr2 = 0; strcpy(ptr1," c"); strcpy(ptr2,"questions"); printf("\n%s %s",ptr1,ptr2); getch(); } #include void main(){ char *ptr1 = NULL; char *ptr2 = 0; strcpy(ptr1," c"); strcpy(ptr2,"questions"); printf("\n%s %s",ptr1,ptr2); getch(); } (A) c questions (B) (null) (null) Run error

WHAT IS THE ANSWER? #include void main(){ char *p; printf("%d %d",sizeof(p), sizeof(*p) ); getch(); } #include void main(){ char *p; printf("%d %d",sizeof(p), sizeof(*p) ); getch(); } (A) 4 1 (B) Compile error

WHAT IS THE ANSWER? #include int main(){ void *k = "\0a\0\0c\0\0\0"; int *p = k; int i; for (i=0; i<2; i++) printf("%d ", *(p+i)); getch(); return 0; } #include int main(){ void *k = "\0a\0\0c\0\0\0"; int *p = k; int i; for (i=0; i<2; i++) printf("%d ", *(p+i)); getch(); return 0; } = 97 X 256

WHAT IS THE ANSWER? #include int main(){ void *k = "\0a\0\0c\0\0\0“; k++; printf("%d ", *k); getch(); return 0; } #include int main(){ void *k = "\0a\0\0c\0\0\0“; k++; printf("%d ", *k); getch(); return 0; } k++ and *k : warning printf("%d ", *k) : error

WHAT IS THE ANSWER? #include void main(){ char arr[10]; arr = "world"; printf("%s",arr); getch(); } #include void main(){ char arr[10]; arr = "world"; printf("%s",arr); getch(); } (A) world (B) Compile error

WHAT IS THE ANSWER? #include void main(){ int a,b,c,d; char *p = ( char *)0; int *q = ( int *)0; float *r = ( float *)0; double *s = 0; a = (int)(p+1); b = (int)(q+1); c = (int)(r+1); d = (int)(s+1); printf("%d %d %d %d",a,b,c,d); } #include void main(){ int a,b,c,d; char *p = ( char *)0; int *q = ( int *)0; float *r = ( float *)0; double *s = 0; a = (int)(p+1); b = (int)(q+1); c = (int)(r+1); d = (int)(s+1); printf("%d %d %d %d",a,b,c,d); }