
|
Strings in C, and pointers/arrays with functionsFirst of all, homework solutions for the safeway program:
#include <stdio.h>
#include "../inc/simpio.h"
typedef struct {
char name[255];
int quantity;
} product;
int main() {
int i, inputq;
char choice;
string inputstr;
bool nomatch;
product p[9];
strcpy(p[0].name, "Milk");
strcpy(p[1].name, "Coca cola");
strcpy(p[2].name, "Pepsi");
strcpy(p[3].name, "Sprite");
strcpy(p[4].name, "7up");
strcpy(p[5].name, "Mountain dew");
strcpy(p[6].name, "Root beer");
strcpy(p[7].name, "Beer");
strcpy(p[8].name, "Orange juice");
for (i = 0; i < 9; i++)
p[i].quantity = 0;
while (1) {
printf("Do you want to (l)ist, (r)ead, (w)rite, or (q)uit? ");
choice = getchar();
getchar();
if (choice == 'q')
break;
switch (choice) {
case 'l':
for (i = 0; i < 9; i++)
printf("%d) %s\n", i+1, p[i].name);
break;
case 'r':
printf("Type in product name: ");
inputstr = GetLine();
nomatch = TRUE;
for (i = 0; i < 9; i++) {
if (strcmp(inputstr, p[i].name) == 0) {
nomatch = FALSE;
break;
}
}
if (nomatch) {
printf("No match for product.\n");
} else {
printf("Product name: %s\n", p[i].name);
printf("Quantity: %d\n", p[i].quantity);
}
break;
case 'w':
printf("Type in product name: ");
inputstr = GetLine();
nomatch = TRUE;
for (i = 0; i < 9; i++) {
if (strcmp(inputstr, p[i].name) == 0) {
nomatch = FALSE;
break;
}
}
if (nomatch) {
printf("No match for product.\n");
} else {
printf("Type in new quantity: ");
inputq = GetInteger();
p[i].quantity = inputq;
printf("Quantity updated.\n");
}
break;
default:
printf("Unknown command.\n");
break;
}
}
return 0;
}
Today we will only have a short lecture, and you will have time
to work on your projects. You will not have classwork or homework on
the lecture topics as they are not new big concepts, but rather additions
to some old topics. Also, from now on, you will not be assigned classwork
or homework programs, as you will be working on your projects (however,
there may be quizes). We are having lectures on these topics because
you should know about them and they may be helpful on your projects;
also they may be on quizes and the final.
Strings in C
Up to now we've been using the data type string to store
strings in C. It is sad to know that this data type you've been using
was written by the author of the textbook Eric Roberts as part of the
libraries given by this book; and thus is not perfect and frequently
cause errors like segmentation fault with your programs.
What you should really use, as a standard in C, to store strings,
is a char array. As you remember, an array can be declared
either initially or by using malloc() later on in the program.
Therefore, both of the ways below will work fine:
char name[255];or char* name; name = malloc(255*sizeof(char));255 is just an arbitrary maximum length I set for the strings, which you can set to anything. With the second way that uses malloc(), you can even use a variable for it. With that done, there are a whole bunch of functions provided by
the standard C library string.h (it is already included if you use simpio.h).
All of these functions are compatible with character arrays. That means
when you declare a string called "name" like above, you can simply pass in
"name" as the argument without any brackets [], any stars *, or any addresses
& at all.
How pointers/arrays work with functions
We have already talked about how pointers get passed into functions
as arguments before. It is also good to know that pointers can also be
used as return values, if you need to do that for some reasons. Example:
char* MakeString() {
char* temp;
temp = (char*)malloc(6*sizeof(char));
strcpy(temp, "Hello");
return temp;
}
int main() {
char* a;
a = MakeString();
printf("%s\n", a); // prints Hello
return 0;
}
I hope you still remember that pointers store addresses. In the
computer, when you declare and use an array, an array actually stores
an address also; and the brackets [] dereference that address. What
that means is, a pointer and an array are essentially the same thing.
When you access an array you declared like this:
int myarray[5]; myarray[3] = 1;If you had a pointer and allocated memory for it, it is the same as doing: int* myptr; myptr = (int*)malloc(5*sizeof(int)); *(myptr+3) = 1;In other words, an array is a pointer, all it does is to provide you a convenient way to access its elements using the brackets []. In other words, in terms of function arguments, pointers and
arrays are interchangeable. When you declare a function that takes in
a pointer, you can also use an array with it. When you declare a
function that takes in an array, you can also use a pointer with it.
That's why for the strcpy and other string functions above, you can
use both char[] or char* with them.
Unfortunately, this doesn't work with return values. When a function
returns a pointer, you have to set a pointer equal to it, not an array.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||