SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
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.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Programming in C Chapter 10 Structures and Unions
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
An introduction to pointers in 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.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Programming Languages and Paradigms The C Programming Language.
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.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 9 MEMORY MANAGEMENT.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
C Intro.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
C Programming - Lecture 5
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Kernighan/Ritchie: Kelley/Pohl:
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:
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Linked Structures, Project 1: Linked List Bryce Boe 2013/07/11 CS24, Summer 2013 C.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Chapter 6 Pointers C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Lecture Contents Arrays and Vectors: Concepts of pointers and references. Pointer declarations and initialization. Pointer Operators. Dynamic Memory Allocation.
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Dynamic memory allocation and Pointers Lecture 4.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Functions & Pointers in C Jordan Erenrich
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
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 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Object Oriented Programming Lecture 2: BallWorld.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSCI206 - Computer Organization & Programming
Programming Languages and Paradigms
פרטים נוספים בסילבוס של הקורס
Pointers, Dynamic Data, and Reference Types
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Local Variables, Global Variables and Variable Scope
Dynamic Memory A whole heap of fun….
Programming Language C Language.
C Programming Lecture-8 Pointers and Memory Management
C Programming - Lecture 5
CSCE 206 Lab Structured Programming in C
Presentation transcript:

SEE C GO Provisional Title

Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return, struct, for, if, const, static

Hello World! a + b = c #include int main() { /* This is a comment */ int a = 1; int b = 2; int c; c = a + b; printf(“%d + %d = %d\n”, a, b, c); return 0; }

More practical (but still useless) example #include int main() { /* Declare a bunch of variables */ int r = 0; int index = 0; /* Call a function to seed random number generator */ srand(10); for(index = 0; index < 10; index++) { r = rand() % 10; printf(“Random number %d is ”, r); if(r % 2) printf(“odd.\n”); else printf(“even.\n”); } return 0; }

A word on scope int index = 0; int outside = 0; for(index = 0; index < 10; index++) { int inside = index + 1; outside = inside; } printf(“%d\n”, inside); /* this will fail */ printf(“%d\n”, outside); /* this will work */

Your own (useless) function! int add(int a, int b) { int c = 0; c = a + b; return c; }

Now to use it #include int add(int a, int b) { int c = 0; c = a + b; return c; } int main() { /* Comments are your friend! */ int a,b,c = 0; a = rand(); b = rand(); c = add(a, b); printf(“%d + %d = %d\n”, a, b, c); return 0; }

A further note on functions #include int main() { /* Comments are your friend! */ int a,b,c = 0; a = rand(); b = rand(); c = add(a, b); /* Will this work? */ printf(“%d + %d = %d\n”, a, b, c); return 0; } int add(int a, int b) { int c = 0; c = a + b; return c; }

A further note on functions #include int add(int a, int b); int main() { /* Comments are your friend! */ int a,b,c = 0; a = rand(); b = rand(); c = add(a, b); /* Will this work? */ printf(“%d + %d = %d\n”, a, b, c); return 0; } int add(int a, int b) { int c = 0; c = a + b; return c; }

Arrays int numbers[10]; /* 10 ints */ int index = 0; for(index = 0; index < 10; index++) { numbers[index] = index; }

Pointer Purgatory Special meaning of * and & int a = 0; int * b; a = 10; b = &a; printf(“%d\n”, a); printf(“%d\n”, b); printf(“%d\n”, *b); printf(“%d\n”, &a);

More pointers! /* This is a single character */ char chVar = ‘a’; /* This is a string */ char * pCharVar = “This is a string”;

Pointer operations int main() { char chVar = ‘a’; char * pCharVar = “This is a string”; int index = 0; for(index = 0; index < strlen(pCharVar); index++) { printf(“%c\n”, pCharVar[index]); /* Pay attention here */ printf(“%s\n”, pCharVar + index); } return 0; }

Arrays and Pointers Pointers and arrays are often interchangeable char hello[6] = “Hello”; char * hello = “Hello”; char hello[] = “Hello”; hello[0] is H for all of them Don’t do hello[6] if you don’t want to crash your program

Structs (C89 style) typedef struct _foo { int someInt; float someFloat; double someDouble; } foo; foo foo1; foo1.someInt = 10; foo1.someFloat = 20.0; foo2.someDouble = 30.0;

Adder revisited typedef struct _input { int a; in b; } input; int add(input in) { int c = in.a + in.b; return c; } int main() { int c; input in1; in1.a = 5; in1.b = 10; c = add(in1); return c; }

When pointers aren’t arrays int add(input * in) { int c; c = in->a + in->b; return c; } int main() { int c; input in; in.a = 5; in.b = 10; c = add(&in); printf(“%d + %d = %d\n”, in.a, in.b, c); return 0; }

Another struct typedef struct _input { int a; int b; int c; } input; void add(input * inout) { inout->c = inout->a + inout->b; } int main() { input in; in.a = 5; in.b = 10; add(&in); printf(“%d\n”, in.c); }

Dynamic Memory malloc: allocate chunks of memory void * malloc(size_t bytes); input * in = (input*)malloc(sizeof(input)); free: free chunks of memory void free(void * pointer); free(in);

Final (maybe) version of add #include /* Using the input struct defined above */ int main() { input * in = NULL; int c; in = (input*)malloc(sizeof(input)); in->a = 5; in->b = 10; printf(“%d + %d = %d\n”, in->a, in->b, in->c); free(in); in = NULL; return 0; }

Common dynamic memory mistakes Forgetting to free allocated memory Using a pointer after the memory has been freed Double freeing a pointer Accessing a pointer that points to NULL

A more subtle pointer mistake void add(input * in) { in->a = 10; /* This is usually bad */ in->c = in->a + in->b; } /* const keyword will prevent anyone from modifying the contents of a pointer or value */ int add(const input * in) { int c; in->a = 10; /* this would not compile */ c = in->a + in->b; return c; }