C Programming VII
Dr. Sorin Draghici
Wayne State University

Summary

Structures
What is a structure ?
How do we define a structure ?
How do we use a structure ?
More about pointers
Pointers and arrays

Structure


A data structure for storing entities of different types.

Initializations

We can initialize structures the same way we initialize atomic types or arrays:

int i = 0 ;

static int a[] = { 0, 1, 2, 3, 4 } ;

static struct t_book my_book = {

Using structures

The selection operator "." allows us to refer to one field of a given structure:

void print_book ( struct t_book *b )
{
printf("The title is: %s\n", (*b).title ) ;
printf("The author is: %s\n", b->author ) ;
printf("The price is: %lf\n", b->value );
}

The "->" operator is dereferencing + selection (follow a pointer to a structure and take just one field of the structure)


More about pointers

Examples:

char c1, c2, *p ;
p=&c ; /* p will point to the location of c */
c='a' ; /* c will take the value 'a' */
putchar(*p) ; /* the content of the location pointed by p will be printed */
p++ ; /* allowed but wrong in this context */
(*p)++ ; /* the content of the location pointed by p will be incremented*/
putchar(*p) ; /* see above */
c1 = *p ; /* the content of the location pointed by p will be copied in c1 */
c = p ; /* not allowed - type mismatch */
p = *c ; /* not allowed */
p = &c1 ; /* p will point to the location of c1 */

Pointers and arrays

char carray[20], *p ;
float farray[4], *pf ;

Observation 1:

by definition, the name of an array is the address of the first element
p = &(carray[0]) ; <=> p = carray ;

Observation 2:

p + i points to the i-th element of the array: p = p + i*sizeof(data type)
p + i will point

Example

Exam question:

Draw a picture showing the situation in the memory after the execution of the following instructions. The picture should include all variables. You can assume that sizeof(int) = 2, sizeof(char) = 1 (in bytes).

char carray[ 16 ] = { "this is a string"} ;
char *p1 ;
int iarray[ 20 ], *p2 ;
p1 = carray ; p2 = iarray ;

p1 += 2 ;
*p1++ = 'a' ;
*p1 = 't' ;
*p2 = 5 ;

Operations with pointers


What you can do with pointers:


What you cannot do with pointers:

  • sum of 2 pointers
  • multiplication
  • division

More about '&' and pointers

The & operator can be applied to:

  • variables
  • elements of vectors
  • structures

The & operator cannot be applied to:

  • expressions (the address is not know at comp. time)
  • constants (don't have an address)
  • register variables (don't have an address)

Examples

#define PI 3.14

double *dp, atof (char *), d, *x, *y ;

&(x+y) NO!
&PI NO!
*dp = *dp + 10 ; YES!
*dp = *dp + 1; YES!
*dp += 1 ; YES!
++ *dp ; YES!
(*dp)++ ; YES! We need (): p(*)=p(++) and left ->right
x = y ; YES!
*x = *y ; YES!
*x = &y NO!
x = &d ; YES!
d = *x ; YES!
d = &y ; NO!
*x = d + *y ; YES!

Pointers and Arrays

Whatever one can do by indexing an array, one can do by using pointers:

int a[10], *pa ;
pa = &a[0] or pa = a ;
*(pa+1) = 0 ; a[1] = 0 ;
*(pa+i) = 1 ; a[i] = 0 ;

int *pa ;
int a[10] ;
pa = a ;
pa[i] = 3 ; /* It's ok ! A pointer is used with an index !!!*/
*(a+2) = 5 ; /* It's ok ! An array is used as a pointer */
a++ ; /* NO! The name of an array is the address of the first element. I cannot change this value! */

Arrays and pointers

Another example:

struct cell {

  • int key ;
  • struct cell *next ;
  • } c_array [10] ;
struct cell *p ; p = & carray[0] ; /* or p = carray */ (p+2)->key = 3 ;
More examples

Write a function which copies one character string into another. Both strings should be already allocated.

Obs: like any character string in C, both strings are terminated with '\0'.

Solution 1:

int strcpy( char s[], char t[] )
{ int i ; i = 0 ; do

  • {
  • s[ i ] = t [ i ] ;
  • i++ ;
  • }
while( s[ i -1 ] != '\0' ) ; }

Solution 2: int strcpy( char s[], char t[] )
{ int i ; i = 0 ; while( ( s[ i ] = t [ i ] ) != '\0' )

  • i++ ;
}

Solution 3: int strcpy( char *s, char *t )
{ while( ( *s = *t ) != '\0' )

  • {
  • s++ ;
  • t++ ;
  • }
}

Solution 4: int strcpy( char *s, char *t )
{ while( ( *s++ = *t++ ) != '\0' ) ; }

Solution 4: int strcpy( char *s, char *t )
{ while( ( *s++ = *t++ ) ) ; }

  • What is a structure ?
  • How do we define a structure ?
How do we use a structure
Reading

Chap. 11, pag. 360 - 385
We already know scanf, printf
Chap. 14, pag. 469 - 498, 504 - 508
That's without union and typedef