structs & typedef · 2014-06-17 · typedef 5 • synonyms for ... e.g. changing numbers in a...
TRANSCRIPT
![Page 1: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/1.jpg)
structs & typedef
![Page 2: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/2.jpg)
Structs
2
Contiguously-allocated region of memory
Refer to members within structure by
names
Members may be of different types
Example: struct rec
{
int i;
int a[3];
int *p;
};
Possible Memory Layout
i a p
0 4 16
![Page 3: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/3.jpg)
Struct initialization
3
structs can be initialized in a way similar to arrays:
struct rec
{
int i;
int a[3];
int *p;
};
...
int k;
struct rec r = { 5, { 0,1,2}, &k };
r.i=1;
r.a[0]=5;
r.p=&k;
![Page 4: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/4.jpg)
Struct initialization
4
structs can be initialized in a way similar to arrays:
struct rec
{
int i;
int a[3];
int *p;
};
...
int k;
struct rec r = { 5, { 0,1,2}, &k };
r.i=1;
r.a[0]=5; Do we really need to write struct rec???
r.p=&k;
![Page 5: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/5.jpg)
typedef
5
• Synonyms for variable types – make your program more readable
• Syntax:
• Example:
typedef <existing_type_name> <new_type_name>;
typedef unsigned int size_t;
![Page 6: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/6.jpg)
typedef
6
• Synonyms for variable types – make your
program more readable
typedef struct _Complex
{
double _real, _imag;
} Complex;
Complex addComplex(Complex, Complex);
Complex subComplex(Complex, Complex);
complex.h
![Page 7: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/7.jpg)
typedef- why?
Readibilty: shorter names or dedicated names
7
typedef unsigned int size_t; typedef struct _Complex Complex;
![Page 8: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/8.jpg)
typedef- why?
Portability:
8
#ifdef INT_4_BYTES
typedef int int32;
typedef short int16;
#else
typedef long int32;
typedef int int16;
#endif
![Page 9: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/9.jpg)
typedef- why?
Generic code:
e.g. changing numbers in a data structure from char to int
easily
Not always recommended
9
![Page 10: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/10.jpg)
Pointers to structs
10
• You can have a pointer to structs the same way you
have pointers to built in type.
![Page 11: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/11.jpg)
Access to struct members via pointers
11
struct MyStr
{
int _a[10];
};
main()
{
MyStr x;
MyStr *p_x = &x;
x._a[2] = 3;
(*p_x)._a[3] = 5;
p_x->_a[4] = 6;
}
![Page 12: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/12.jpg)
Structs - Code
12
Offset of each structure member determined
at compile time
struct rec
{
int i;
int a[3];
int *p;
};
i a p
0 4 16
r + 4 + 4*idx
r
int *find_a(struct rec *r, int idx)
{
return &(r->a[idx]);
}
![Page 13: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/13.jpg)
Alignment in memory
Compiler specific. In MSVC 2012 default params:
Note: MSVC compiler has flags to control this !
struct S1
{
char c;
int i[2];
double v;
};
13
v i[0] c i[1]
P+0 P+4 P+16 P+24
![Page 14: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/14.jpg)
struct
• Sequential in memory, but may have gaps: sizeof(S1) != sizeof(char)+sizeof(double)+sizeof(int)*2
• Member offset determined in compile time
• Access member with "." e.g. a.i[0], a.v.
• Access member of struct pointer contents:
(*p). or p->
• As always, pass by value!
struct S1 { char c; int i[2]; double v; }; struct S1 a,*p;
v i[0] c i[1]
P+0 P+4 P+16
14
P+24
![Page 15: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/15.jpg)
15
Structs – old object oriented design
15
Complex.c
struct Complex
{
double _real, _imag;
};
struct Complex addComplex(struct Complex, struct Complex);
Complex.h
#include "Complex.h"
// implementation
struct Complex addComplex(struct Complex a, struct Complex b)
{
Complex.c
#include "Complex.h"
int main()
{
struct Complex c;
...
MyProg.c
![Page 16: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/16.jpg)
16
Structs – old object oriented design –
Better design – more on this later
16
Complex.c
struct _Complex;
typedef struct _Complex Complex;
Complex* Complex_alloc();
Complex.h
#include "Complex.h"
struct _Complex {double _real, _imag}
Complex* Complex_alloc(double real, double imag) {...
Complex.c
#include "Complex.h"
int main()
{
Complex* c_ptr= Complex_alloc(3.0, -1.2);
...
MyProg.c
![Page 17: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/17.jpg)
#if – header safety
17
Complex.h:
struct Complex
{
...
MyStuff.h:
#include "Complex.h"
Main.c:
#include "MyStuff.h"
#include "Complex.h"
Error:
Complex.h:1: redefinition
of `struct Complex'
![Page 18: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/18.jpg)
#if – header safety
18
Complex.h (revised):
#ifndef COMPLEX_H
#define COMPLEX_H
struct Complex
{
...
#endif
Main.c:
#include "MyStuff.h"
#include "Complex.h" // no error this time
![Page 19: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/19.jpg)
#pragma once – header safety
19
Complex.h (revised):
#pragma once
struct Complex
{
...
Main.c:
#include "MyStuff.h"
#include "Complex.h" // no error this time
![Page 20: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/20.jpg)
structs copying
20
Copy structs using ‘=‘:
copy just struct values!!!
Complex a,b;
a._real = 5;
a._imag = 3;
b = a;
_real = 5
_imag = 3
a:
_real = ?
_imag = ?
b:
![Page 21: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/21.jpg)
structs copying
21
Copy structs using ‘=‘:
copy just struct values!!!
Complex a,b;
a._real = 5;
a._imag = 3;
b = a;
_real = 5
_imag = 3
a:
_real = 5
_imag = 3
b:
![Page 22: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/22.jpg)
Arrays in structs copying
22
struct definition:
typedef struct Vec
{
double _arr [MAX_SIZE];
}
Vec;
Vec addVec(Vec, Vec);
...
vec.h
![Page 23: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/23.jpg)
Arrays in structs copying
143
copy struct using ‘=‘:
Vec a,b;
a._arr[0] = 5;
a._arr[1] = 3;
b = a;
_arr =
{5,3,?,…}
a:
_arr =
{?,?,?,…}
b:
![Page 24: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/24.jpg)
Arrays in structs copying
24
Copy struct using ‘=‘:
copy just struct values!!!
Vec a,b;
a._arr[0] = 5;
a._arr[1] = 3;
b = a;
_arr =
{5,3,?,…}
a:
_arr =
{5,3,?,…}
b:
![Page 25: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/25.jpg)
25
![Page 26: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/26.jpg)
Pointers in structs copying
26
struct definition:
typedef struct Vec
{
double _arr [MAX_SIZE];
double * _p_arr;
}
Vec;
Vec addVec(Vec, Vec);
...
vec.h
![Page 27: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/27.jpg)
Pointers in structs copying
27
Copy structs using ‘=‘:
copy just struct values!!!
Vec a,b;
a._arr[0] = 5;
a._arr[1] = 3;
a._p_arr =
a._arr;
b = a;
_arr = {5,3,?,…}
_p_arr = 0x55
a: _arr = {?,?,?,…}
_p_arr = ?
b:
![Page 28: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/28.jpg)
Pointers in structs copying
28
Copy structs using ‘=‘:
copy just struct values!!!
Vec a,b;
a._arr[0] = 5;
a._arr[1] = 3;
a._p_arr =
a._arr;
b = a;
_arr = {5,3,?,…}
_p_arr = 0x55
a: _arr = {5,3,?,…}
_p_arr = 0x55
b:
Pointers copied by value!!!
![Page 29: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/29.jpg)
Pointers in structs copying
29
The result:
Vec a,b;
a._arr[0] = 5;
a._arr[1] = 3;
a._p_arr = a._arr;
b = a;
*(b._p_arr) = 8;
printf ("%f", a._arr[0]);
// output
8
![Page 30: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/30.jpg)
How to deep copy structs correctly?
30
Implement a clone function:
Vec* Vec_clone (const Vec* v)
{
...
![Page 31: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/31.jpg)
Arrays & structs as arguments
31
When an array is passed as an
argument to a function, the address of
the 1st element is passed.
Structs are passed by value, exactly as
the basic types.
![Page 32: structs & typedef · 2014-06-17 · typedef 5 • Synonyms for ... e.g. changing numbers in a data structure from char to int easily](https://reader034.vdocuments.net/reader034/viewer/2022042802/5f3f2722d48f690ea44bbd17/html5/thumbnails/32.jpg)
Arrays & structs as arguments
32
struct MyStr
{
int _a[10];
};
void f(int a[])
{
a[7] = 89;
}
void g(MyStr s)
{
s._a[7] = -1;
}
main()
{
MyStr x;
x._a[7] = 0;
f(x._a);
printf("%d\n", x._a[7]);
g(x);
printf("%d\n", x._a[7]);
}
Output: 89
89