Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*

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)
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
LIST PROCESSING.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
EASTERN MEDITERRANEAN UNIVERSITY EENG212 ALGORITHMS & DATA STRUCTURES Structures in C.
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.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
Programming in C Chapter 10 Structures and Unions
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Data Structure Lecture-5
C Language.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Programming and Data Structure
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.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
C Intro.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Programming Languages -1 (Introduction to C) structures Instructor: M.Fatih AMASYALI
Kernighan/Ritchie: Kelley/Pohl:
CSE1303 Part A Data Structures and Algorithms Lecture A6 – Dynamic Memory.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A6 – Dynamic Memory.
Structures and Lists (and maybe stacks) Chapters 9-10.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 6 : September 6.
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);
Week 9 Part 2 Kyle Dewey. Overview Announcement More with structs and memory Assertions Exam #2 Course review.
 Review structures  Program to demonstrate a structure containing a pointer.
13 주 강의 Structures and Unions. Structures Structure – 기본 자료형들로 구성된 새로운 자료형 예 ) struct card { intpips; char suit; };
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Multidimensional Arrays Lecture 20 4/3/2002.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Programming Languages -1 (Introduction to C) structures
5/3/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures Lecture
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.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Struct 1. Definition: Using struct to define a storage containing different types. For example it can contain int, char, float and array at the same time.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
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.
C Lecture Notes 1 Structures & Unions. C Lecture Notes Introduction Structures –Collections of related variables (aggregates) under one name Can.
Functions & Pointers in C Jordan Erenrich
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Arrays and Pointers.
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;
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
 2000 Prentice Hall, Inc. All rights reserved Introduction Structures –Collections of related variables (aggregates) under one name Can contain.
Programming Languages and Paradigms
Local Variables, Global Variables and Variable Scope
Introduction to Programming
Programming Language C Language.
C Programming Lecture-8 Pointers and Memory Management
ETE 132 Lecture 7 By Munirul Haque.
Structures.
Arrays, Pointers, and Strings
Presentation transcript:

Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /* last def. could also be typedef vector matrix[N]; */ void add( vector x, vector y, vector z ) { int i; for( i = 0; i < N; i++ ) { x[i] = y[i] + z[i]; }

Structures struct card_struct { int pips; char suit; }; typedef struct card_struct card; The structure mechanism allows us to aggregate variables of different types Your struct definition generally goes outside all functions struct card_struct { int pips; char suit; } c1, c2; typedef struct card_struct card; void some_function() { struct card_struct a; card b; /* a, b have same types */ b.pips = 3; }

Structures – accessing via pointer struct card_struct { int pips; char suit; }; typedef struct card_struct card; void main() { card a; set_card( &a ); } void set_card( card *c ) { c->pips = 3; c->suit = ‘A’; }

Structures – initialize #include struct address_struct { char *street; char *city_and_state; long zip_code; }; typedef struct address_struct address; void main() { address a = { "1449 Crosby Drive", "Fort Washington, PA", }; }

Unions union int_or_float { int i; float f; }; union int_or_float a; /* need to keep track of, on own, which type is held */ /* a.i always an int, a.f always a float */

Fishy. void main() { int *a; a = function_3(); printf( “%d\n”, *a ); } int *function_3() { int b; b = 3; return &b; }

Valid. #include int *function_3(); void main() { int *a; a = function_3(); printf( “%d\n”, *a ); free( a ); } int *function_3() { int *b; b = (int *) malloc( sizeof( int )); /* NULL? */ *b = 3; return b; }

Getting an array of numbers #include void main() { int *numbers, size, i, sum = 0; printf( "How many numbers? " ); scanf( "%d", &size ); numbers = malloc( size * sizeof( int )); for( i = 0; i < size; i++ ) { scanf( "%d", &numbers[i] ); } for( i = 0; i < size; i++ ) { sum += numbers[i]; } printf( "%d\n", sum ); free( numbers ); }