0% found this document useful (0 votes)
54 views

Generic Pointers: Void : A "Pointer To Anything"

The document discusses generic pointers in C. It explains that void pointers lose type information about what is being pointed to, reducing the compiler's ability to do type checking. It also notes that type casting tells the compiler to change an object's type for type checking purposes, but does not actually modify the object. The document warns that void pointers can be dangerous but are sometimes necessary.

Uploaded by

meenakshi
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Generic Pointers: Void : A "Pointer To Anything"

The document discusses generic pointers in C. It explains that void pointers lose type information about what is being pointed to, reducing the compiler's ability to do type checking. It also notes that type casting tells the compiler to change an object's type for type checking purposes, but does not actually modify the object. The document warns that void pointers can be dangerous but are sometimes necessary.

Uploaded by

meenakshi
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Generic Pointers

void *: a “pointer to anything”


type cast: tells the compiler to
void *p; “change” an object’s type (for type
int i; checking purposes – does not modify
char c; the object in any way)
p = &i;
p = &c; Dangerous! Sometimes necessary…
putchar(*(char *)p);

Lose all information about what type of thing


is pointed to
 Reduces effectiveness of compiler’s type-checking
 Can’t use pointer arithmetic

Cox Arrays and Pointers 1


Quiet Please!

It’s time to start …

Cox Arrays and Pointers 2


Pass-by-Reference

void
set_x_and_y(int *x, int *y)
{

*x = 1001;
*y = 1002; a 1001
1
}
b 1002
2
void
f(void)
{ x
int a = 1;
int b = 2; y

set_x_and_y(&a, &b);
}

Cox Arrays and Pointers 3


Arrays and Pointers
Behind the scenes “secret”: Passing arrays:
Array name  a pointer to the Really int *array
Must explicitly
initial (0th) array element pass the size
int
foo(int array[],
a[i]  *(a + i) unsigned int size)
{
An array is passed to a function … array[size - 1] …
as a pointer }
 The array size is lost!
int
main(void)
Usually bad style to interchange {
arrays and pointers int a[10], b[5];
 Avoid pointer arithmetic! … foo(a, 10)… foo(b, 5) …
}

Cox Arrays and Pointers 4


Arrays and Pointers

int
foo(int array[],
unsigned int size)
{
… What does this print? 8
printf(“%d\n”, sizeof(array));
... because array is really
}
a pointer
int
main(void)
{
int a[10], b[5];
… foo(a, 10)… foo(b, 5) … What does this print? 40
printf(“%d\n”, sizeof(a));
}

Cox Arrays and Pointers 5


Arrays and Pointers

int i; int *p;


int array[10]; int array[10];

for (i = 0; i < 10; i++) { for (p = array; p < &array[10]; p++) {


… …
array[i] = …; *p = …;
… …
} }

These two blocks of code are functionally equivalent

Cox Arrays and Pointers 6


Strings

In C, strings are just an array of characters


 Terminated with ‘\0’ character
 Arrays for bounded-length strings
 Pointer for constant strings (or unknown length)

char str1[15] = “Hello, world!\n”;


char *str2 = “Hello, world!\n”;

C, … He l lo , w o r l d !\n terminator

C terminator: ’\0’

Pascal, Java, … length H el l o, w o r l d ! \n

Cox Arrays and Pointers 7


String length

Must calculate length:


can pass an
int array or pointer
strlen(char str[])
{
array access int len = 0; Check for
to pointer! terminator

while (str[len] != ‘\0’)


len++;
What is the size
of the array???
return (len);
}
Provided by standard C library: #include <string.h>
Cox Arrays and Pointers 8
Pointer to Pointer (char **argv)

Passing arguments to main:


size of the argv array/vector
int
main(int argc, char **argv)
{ an array/vector of
char *
...
} Recall when passing an
array, a pointer to the
first element is passed
Suppose you run the program this way

UNIX% ./program hello 1 2 3

argc == 5 (five strings on the


command line)
Cox Arrays and Pointers 9
char **argv

“3”
0x1020 argv[4]
“2” These are strings!!
0x1018 argv[3] Not integers!
0x1010 argv[2] “1”
0x1008 argv[1] “hello”
0x1000 argv[0]
“./program”

Cox Arrays and Pointers 10


Structures and Unions in C

Alan L. Cox
[email protected]
Objectives

Be able to use compound data structures in


programs

Be able to pass compound data structures as


function arguments, either by value or by
reference

Be able to do simple bit-vector manipulations

Cox Structures and Unions 12


Structures
Compound data: struct ADate {
int month;
int day;
A date is
int year;
 an int month and
};
 an int day and
 an int year struct ADate date;

date.month = 1;
date.day = 18;
date.year = 2018;

Unlike Java, C doesn’t


automatically define functions for
initializing and printing …

Cox Structures and Unions 13


Structure Representation & Size
sizeof(struct …) =
sum of sizeof(field) struct CharCharInt {
+ alignment padding char c1;
Processor- and compiler-specific char c2;
int i;
} foo;

foo.c1 = ’a’;
foo.c2 = ’b’;
foo.i = 0xDEADBEEF;

c1 c2 padding i

61 62 EF BE AD DE
x86 uses “little-endian” representation

Cox Structures and Unions 14


Typedef

Mechanism for creating new type names


 New names are an alias for some other type
 May improve clarity and/or portability of the
program
Overload existing type
typedef long int64_t; names for clarity and
typedef struct ADate { portability
int month;
int day;
int year;
} Date; Simplify complex type names

int64_t i = 100000000000;
Date d = { 1, 18, 2018 };

Cox Structures and Unions 15


Constants

Allow consistent use of the same constant


throughout the program
 Improves clarity of the program
 Reduces likelihood of simple errors
 Easier to update constants in the program
Constant names are
Preprocessor directive capitalized by convention

#define SIZE 10
Define once,
int array[10]; int array[SIZE]; use throughout
the program

for (i=0; i<10; i++) { for (i=0; i<SIZE; i++) {


… …
} }
Cox Structures and Unions 16
Arrays of Structures
Array declaration Constant

Date birthdays[NFRIENDS];

bool
check_birthday(Date today)
{
int i; Array index, then
structure field
for (i = 0; i < NFRIENDS; i++) {
if ((today.month == birthdays[i].month) &&
(today.day == birthdays[i].day))
return (true);

return (false);
}

Cox Structures and Unions 17


Pointers to Structures

Date void
create_date1(int month, create_date2(Date *d,
int day, int month,
int year) Pass-by-reference int day,
{ int year)
Date d; {
d->month = month;
d.month = month; d->day = day;
d.day = day; d->year = year;
d.year = year; }

return (d);
Date today;
}

today = create_date1(1, 18, 2018);


Copies date create_date2(&today, 1, 18, 2018);

Cox Structures and Unions 18


Pointers to Structures (cont.)
void
create_date2(Date *d,
0x30A8 year: 2018
int month,
int day, 0x30A4 day: 18
int year)
0x30A0 month: 1
{
d->month = month; 0x3098 d: 0x1000
d->day = day;
d->year = year;
}

void 0x1008 today.year: 2018


fun_with_dates(void)
{ 0x1004 today.day: 18
Date today; 0x1000 today.month: 1
create_date2(&today, 1, 18, 2018);
}

Cox Structures and Unions 19


Pointers to Structures (cont.)

Date *
create_date3(int month,
int day,
int year)
{
What is d pointing to?!?!
Date *d;
(more on this later)

d->month = month;
d->day = day;
d->year = year;

return (d);
}

Cox Structures and Unions 20


Abstraction in C

From the #include file widget.h: Definition is hidden!

struct widget;

struct widget *widget_create(void);


int widget_op(struct widget *widget, int operand);
void widget_destroy(struct widget *widget);

From the file widget.c:

#include “widget.h”

struct widget {
int x;

};

Cox Structures and Unions 21


Collections of Bools (Bit Vectors)

Byte, word, ... can represent many Booleans


One per bit, e.g., 00100101 = false, false, true, ..., true

Bit-wise operations:
Bit-wise AND: 00100101 & 10111100 == 00100100
Bit-wise OR: 00100101 | 10111100 == 10111101
Bit-wise NOT: ~ 00100101 == 11011010
Bit-wise XOR: 00100101 ^ 10111100 == 10011001

Cox Structures and Unions 22


Operations on Bit Vectors

const unsigned int low_three_bits_mask = 0x7; 0…00 0111


unsigned int bit_vec = 0x15; 0…01 0101

A mask indicates which bit positions we are interested in

Always use C’s unsigned types for bit vectors

Selecting bits:
important_bits = bit_vec & low_three_bits_mask;
Result = ?
0…00 0101 == 0…01 0101 & 0…00 0111

Cox Structures and Unions 23


Operations on Bit Vectors

const unsigned int low_three_bits_mask = 0x7; 0…00 0111


unsigned int bit_vec = 0x15; 0…01 0101

Setting bits:
bit_vec |= low_three_bits_mask;
Result = ?

0…01 0111 == 0…01 0101 | 0…00 0111

Cox Structures and Unions 24


Operations on Bit Vectors

const unsigned int low_three_bits_mask = 0x7; 0…00 0111


unsigned int bit_vec = 0x15; 0…01 0101

Clearing bits:
bit_vec &= ~low_three_bits_mask;
Result = ?

0…01 0000 == 0…01 0101 & ~0…00 0111

Cox Structures and Unions 25


Bit-field Structures
Special syntax packs
structure values more struct Flags {
tightly int f1:3;
unsigned int f2:1;
unsigned int f3:2;
Similar to bit vectors, but
} my_flags;
arguably easier to read
 Nonetheless, bit vectors my_flags.f1 = -2;
are more commonly my_flags.f2 = 1;
used.
my_flags.f3 = 2;

Padded to be an integral
number of words f1 f2 f3

 Placement is compiler- 1 1 0 1 1 0 … …
specific.

Cox Structures and Unions 26


Unions
Choices:
union AnElt {
int i;
An element is
char c;
 an int i or
} elt1, elt2;
 a char c
elt1.i = 4;
sizeof(union …) = elt2.c = ’a’;
elt2.i = 0xDEADBEEF;
maximum of sizeof(field)

c padding

EF BE AD DE
i

Cox Structures and Unions 27


Unions
A union value doesn’t “know” which case it contains

?
union AnElt {
int i;
char c;
How should your program keep track
} elt1, elt2;
whether elt1, elt2 hold an int or
elt1.i = 4;
a char?
elt2.c = ’a’;
elt2.i = 0xDEADBEEF;
?
Basic answer: Another variable holds
if (elt1 currently has a char) … that info

Cox Structures and Unions 28


Tagged Unions

Tag every value with its case

I.e., pair the type info together with the union


Implicit in Java, Scheme, ML, …

enum Union_Tag { IS_INT, IS_CHAR };


struct TaggedUnion { Enum must be external to struct,
enum Union_Tag tag; so constants are globally visible.
union {
int i;
char c;
} data;
Struct field must be named.
};

Cox Structures and Unions 29


Next Time

Memory Allocation

Cox Structures and Unions 30

You might also like