1 240-222 CPT: Ptr+Str/11 240-222 Computer Programming Techniques Semester 1, 1998 Objective of...
-
Upload
solomon-gardner -
Category
Documents
-
view
214 -
download
0
Transcript of 1 240-222 CPT: Ptr+Str/11 240-222 Computer Programming Techniques Semester 1, 1998 Objective of...
1240-222 CPT: Ptr+Str/11
240-222 Computer Programming Techniques240-222 Computer Programming TechniquesSemester 1, 1998Semester 1, 1998
Objective of these slides:– to discuss how pointers are used with
structs
11. Pointers and Structures
2240-222 CPT: Ptr+Str/11
OverviewOverview
1. A struct can contain Strings
2. Shuffling Cards
3. Pointers to structs
4. Updating a struct
3240-222 CPT: Ptr+Str/11
1. A struct can Contain Strings1. A struct can Contain Strings
struct card{ char *face; char *suit;}; :struct card a = {"Three", "Hearts"}; :
printf("%s", a.suit); /* prints Hearts */
4240-222 CPT: Ptr+Str/11
2. Shuffling Cards2. Shuffling Cards Sec. 10.7Sec. 10.7
#include <stdio.h>#include <stdlib.h>#include <time.h>
struct card { char *face; char *suit;};typedef struct card Card;
void filldeck(Card [], char *[], char *[]);void shuffle(Card []);void deal(Card []);
continued
5240-222 CPT: Ptr+Str/11
int main(){ Card deck[52]; char *face[] = {"Ace", "Deuce", "Three",
"Four", "Five", "Six", "Seven", "Eight", "Nine","Ten","Jack", "Queen", "King"};
char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
srand(clock()); /* set random num */
filldeck(deck, face, suit); shuffle(deck); deal(deck); return 0;}
continued
6240-222 CPT: Ptr+Str/11
void filldeck(Card wdeck[], char *wface[], char *wsuit[])
/* initialise the deck of cards */{ int i;
for (i = 0; i <= 51; i++) { wdeck[i].face = wface[i % 13]; wdeck[i].suit = wsuit[i / 13]; }}
continued
7240-222 CPT: Ptr+Str/11
void shuffle(Card wdeck[])/* randomly rearrange the deck of cards */{ int i, j; Card temp;
for (i = 0; i <= 51; i++) { j = rand() % 52; /* get rand num */
if (i != j) { temp = wdeck[i]; wdeck[i] = wdeck[j]; wdeck[j] = temp; } }}
continued
8240-222 CPT: Ptr+Str/11
void deal(Card wdeck[])/* display the deck */{ int i;
for (i = 0; i <= 51; i++) printf("%5s of %-8s%c",
wdeck[i].face, wdeck[i].suit,(i + 1) % 2 ? '\t' : '\n');
}
9240-222 CPT: Ptr+Str/11
Output Output (see fig. 10.4):(see fig. 10.4):
Eight of Diamonds Ace of HeartsEight of Clubs Five of Spades : :
10240-222 CPT: Ptr+Str/11
3. Pointers to Structs3. Pointers to Structs
struct card a, *c;a = {"Four", "Spades"};c = &a; :
printf("%s", c->suit);/* prints Spades */
c->suit is equivalent to (*c).suit
11240-222 CPT: Ptr+Str/11
#include <stdio.h> /* fig 10.2 */
struct card { char *face; char *suit;};
continued
Example
12240-222 CPT: Ptr+Str/11
int main(){ struct card a; struct card *aptr;
a.face = "Ace"; a.suit = "Spades"; aptr = &a; printf("%s of %s\n%s of %s\n%s of%s\n", a.face, a.suit, aptr->face, aptr->suit, (*aptr).face, (*aptr).suit); return 0;}
13240-222 CPT: Ptr+Str/11
Output:Output:
Ace of SpadesAce of SpadesAce of Spades
14240-222 CPT: Ptr+Str/11
4. Updating a struct4. Updating a struct
Code fragment:struct card { char *face; char *suit;};
int main(){ struct card a;
a.face = “Three; printf(“%s”, a.face);}
15240-222 CPT: Ptr+Str/11
DiscussionDiscussion
This code works, so what is the problem?
Answer: the scope of “Three”– at the end of main(), “Three” goes out of
scope and is deleted– this does not matter for this example because
the scope of main() is the entire program
16240-222 CPT: Ptr+Str/11
A More Dangerous ExampleA More Dangerous Example
int main(){ struct card a;
a = initialise(); printf(“%s”, a.face);
: printf(“%s”, a.face);}
struct card initialise(void){ struct card b; b.face = “Three”; return b;}
17240-222 CPT: Ptr+Str/11
DiscussionDiscussion
The scope of “Three” is initialise(), and so will be deleted after initialise() returns– return copies out the b struct, including its two
pointers
– a is assigned the pointers, but what do they point at?
– the deletion may not happen immediately, but depend on the memory usage of the rest of the program
the first printf() may work, sometimes!
18240-222 CPT: Ptr+Str/11
First SolutionFirst Solution
struct card1( char face[10]; char suit[10];};
int main(){ struct card1 a; a = initialise1();
: printf(“%s”, a.face);}
struct card1 initialise1(void){ struct card1 b; strcpy(b.face, “Three”); return b;}
19240-222 CPT: Ptr+Str/11
DiscussionDiscussion
The general solution is to make a copy of the string– “Three” is copied into the fixed size array b.face using strcpy()
– that array is copied out as part of the b struct using return
20240-222 CPT: Ptr+Str/11
Second SolutionSecond Solutionstruct card( char *face; /* no fixed size */ char *suit;};
int main(){ struct card a; a = initialise2();
: printf(“%s”, a.face);}
struct card initialise2(void){ struct card b; b.face = (char *)malloc(6); strcpy(b.face, “Three”); return b;}
21240-222 CPT: Ptr+Str/11
DiscussionDiscussion
The second solution still uses copying, but now calls malloc() to make dynamic memory
– return only copies out the pointers inside b
– but malloc’ed memory is not deleted even though the scope of b is initialise2()
– so a.face will point to the malloc’ed memory from initialise2()
22240-222 CPT: Ptr+Str/11
Third SolutionThird Solutionstruct card( char *face; /* no fixed size */ char *suit;};
int main(){ struct card a; initialise3(&a);
: printf(“%s”, a.face);}
void initialise3(struct card *ap){ ap->face = (char *)malloc(6); strcpy(ap->face, “Three”); }
23240-222 CPT: Ptr+Str/11
DiscussionDiscussion
The third solution uses malloc() to make dynamic memory, but for the a struct.
– pass in a pointer to a, and alter a via the pointer (this is how C implements call-by-reference).
– this is the most common coding style for manipulating structs inside functions