• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

ProgEx08

Started by Frederick J. Harris, November 06, 2009, 03:25:56 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frederick J. Harris


/*
    Up to this point we've looked at some of the basics of console output with
    C/C++ and some of the basics of functions and passing pointer and reference
    parameters.  We've also discussed character strings and arrays some.  You
    might be thinking that in some ways its not too different than BASIC.  Well,
    it actually is quite different from basic and we'll now start discussing
    some of the really painful differences that are going to give BASIC
    programmers the most difficulty.  And, if you havn't guessed already, the
    really painful differences concern strings.

    The way I presented all the programs up to this point really masked the
    problems and I did that intentionally.  For example, if I wanted to show
    how to output a character string consisting of my name I set up the program
    like so...

    char szBuffer[] = "Frederick J. Harris"

    printf("My Name Is %s\n", szBuffer);

    ...and the output would be...

    My Name Is Frederick J. Harris
    Press Any Key To Continue....

    In the top line above we really have an array variable declaration combined
    with its initialization.  szBuffer without the square brackets is a pointer
    to the first byte of storage where the 'F' is stored; szBuffer[1] would be
    'r'; szbuffer[2] would be 'e', etc.  So what really happens when the C
    compiler compiles a line such as...

    char szBuffer[] = "Frederick J. Harris"

    ...is it allocates at least 20 bytes to hold the string and a null
    terminator, and four bytes for the pointer variable szBuffer itself.
    However, what you can't do is the following...

    char szBuffer[20];
    szBuffer = "Frederick J. Harris";

    If you try to compile something as inocent as the above, the GNU compilers
    will give an error message something like this...

    "ISO C++ forbids assignment of arrays"

    There is a way to do it however; you need to use the string primitives in
    string.h.  The char array declaration...

    char szBuffer[20];

    will indeed allocate storage for a string of 19 characters plus a null
    terminator; but it won't put the string there.  You have to do it yourself
    with the strcpy()) function.  This function takes two pointer parameters;
    the first parameter is a pointer to the destination of where a null
    terminated string is to be copied; and the second parameter is a pointer to
    the source string to be copied.  So the complete code segment would look
    like this...

    char szBuffer[20];
    strcpy(szBuffer, "Frederick J. Harris");
    printf("szBuffer = %s\n", szBuffer);

    If you are thinking that this is horrendous to have to do something like
    that you are right.  Strings in C or C++ can be a BIG problem.

    Carefully note that this declaration/allocation...

    char szBuffer[20];

    ...will allocate a buffer that this program will own consisting of only 20
    bytes.  It will indeed be capable of storing a string consisting of the 19
    asci characters of my name, i.e., "Frederick J. Harris", but not consisting
    of this string...

    Mr. Frederick John Harris

    because this latter string comprises 25 asci characters.  If you need a
    buffer to store a string consisting of 25 asci characters, you'll need to
    size the character array to 26 characters to account for the null
    terminator.

    Below is a complete little program showing the necessary includes and steps:
*/

#include <stdlib.h>   //necessary for equate EXIT_SUCCESS
#include <stdio.h>    //necessary for printf() and getchar()
#include <string.h>   //necessary for strcpy()

int main()
{
char szBuffer[20];                       //allocates 20 byte char array

strcpy(szBuffer, "Frederick J. Harris"); //copies my name to those 20 bytes
printf("szBuffer = %s\n",szBuffer);      //outputs szBuffer contents to console
getchar();                               //waits for keypress, i.e., Waitkey$

return EXIT_SUCCESS;
}

/*
Output:
szBuffer = Frederick J. Harris
*/

/*
  At this point you might legitimately ask what can be done in the case of a
  string which a user might enter during a running program where you don't
  know beforehand how long the string might be.  To that issue we turn next.
*/