Exercise 3-21 (Print string array)

Chapter_3     Exercise_3-20     ArrayArguments CommandLineArgs     Exercise_3-22







Exercise 3-21     TCP1, p. 229


Exercise 3-21. Create an array of string objects and assign a string to each element. Print out the array using a for() loop.




CONTENTS:     strings.c     Strings.cpp




strings.c         download


#include <stdio.h> // for printf(), putchar()

void print(char* a[], int size); // virtually identical
void print(char** a, int size); // function declarations

int main()
{
char* a[5] = {"Hello!", "What's", "up?"};
print(a, 5); // prints extra (null) and space for 2 elements
print(a, 3);

// char* b[5] = {"How", "are", "you", "today?", ''}; // error: '' not string
char* b[5] = {"How", "are", "you", "today?", ""}; // "" is a string
print(b, 5); // prints extra space

char* c[5] = {"I", "am", "fine,", "thank", "you!", ""}; // warning:
print(c, 5); // excess elements in array initializer (compiles just fine)
// print(c, 6); // compiles just fine, prints nothing, run-time errors:
// print(c, 7); // Segmentation fault (core dumped)

return 0;
}

void print(char* a[], int size) // char** a
{
int i;

for(i = 0; i < size; i++)
{printf("%s ", a[i]);}

putchar('\n');
}
/*
gcc strings.c -o strings
./strings
Hello! What's up? (null) (null)
Hello! What's up?
How are you today? // 1 extra space
I am fine, thank you!
*/











Strings.cpp         download


#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;

void print(string a[], int size); // compiler considers these two
void print(string* a, int size); // function declarations identical

int main()
{
string a[5] = {"Hello!", "What's", "up?"};
print(a, 5); // prints 2 extra spaces
print(a, 3);

// string b[5] = {"How", "are", "you", "today?", ''}; // error: '' not string
string b[5] = {"How", "are", "you", "today?", ""}; // "" is a string
print(b, 5); // prints extra space
// compile error: too many initializers for string[5]:
// string c[5] = {"I", "am", "fine,", "thank", "you!", ""};
string c[5] = {"I", "am", "fine,", "thank", "you!"};
print(c, 5); // prints a newline
// print(c, 6); // no error or warning, prints no newline
// print(c, 7); // no error or warning, runs for ever, stopped by Ctrl^C

return 0;
}
// compiler registers the signature print(string*, int):
void print(string a[], int size) // string* a
{
for(int i = 0; i < size; i++)
{cout << a[i] << " ";}

cout << endl;
}
/*
g++ Strings.cpp -o Strings
./Strings
Hello! What's up? // 2 extra spaces
Hello! What's up?
How are you today? // 1 extra space
I am fine, thank you!
// I am fine, thank you! // no newline here // print(c, 6);
// I am fine, thank you! // print(c, 7); // ./Strings | less // Ctrl^C + q
*/





Note:  If you uncomment (allow in) the line with
print(c, 7);
you can run the program with
./Strings | less
but then you have to quickly press Ctrl^C to not run out of memory, Then you can stop the program by pressing `q' (quit).









Chapter_3     Exercise_3-20     ArrayArguments BACK_TO_TOP CommandLineArgs     Exercise_3-22



Comments

Popular posts from this blog

Contents