pointers to pointers // exception handling error

Discussion in 'C++' started by muser, Sep 18, 2003.

  1. muser

    muser Guest

    I'm writing a function that isn't showing as an error during
    compilation, but it is at run time. The exception it is throwing
    informs me that it is due to my two pointers in that function. It is
    an access violation error, where the memory can't be read. I'll list
    the things I've tried in order to resolve the problem so that no one
    will suggest the same thing. I've tried initialising AN[4] as a just a
    character pointer, then just as a character variable, neither works as
    the rest of the function requires it to be both a pointer array
    variable. I've tried referencing BAN =& AN[4], which neither worked
    and rightly so as the function checks to see whether AN is isalpha
    or a space, if encountered the next element in the array requires
    checking, and the one preceeding it. BAN is used to get either the
    preceeding element or the one following it.

    Thank you in advance for your help in this matter.



    bool CheckAddressN( char* record )
    {

    char* AN[4];
    char* BAN;

    strncpy(AN[4], &record[27], 4);
    AN[4] = '\0';


    for(int i=0; i < 4; i++)

    if(AN = " ") // check that last character is a number and the
    next character
    {
    //is a char.
    BAN = (AN + 1 ) ;
    if(!isalpha(BAN)){}

    }
    return false;

    if(AN = " ") // check that last character is a number and the
    next character
    {
    //is a char.
    BAN = (AN - 1);
    if(!isdigit(BAN)){};

    }
    return false;


    if(AN = ";")
    {

    BAN = (AN - 1);

    if(!isdigit(BAN)){};
    }
    return false;

    if(AN = ";")
    {
    BAN = (AN + 1);

    if(!isalpha(BAN)){};

    }
    return false;

    if(AN = ";")
    {
    BAN = (AN + 1);

    for(int i=0; i < 17; i++)
    if(!isalpha(BAN))
    return false;
    }

    return true;

    }
    muser, Sep 18, 2003
    #1
    1. Advertising

  2. muser

    Artie Gold Guest

    muser wrote:
    > I'm writing a function that isn't showing as an error during
    > compilation, but it is at run time. The exception it is throwing
    > informs me that it is due to my two pointers in that function. It is
    > an access violation error, where the memory can't be read. I'll list
    > the things I've tried in order to resolve the problem so that no one
    > will suggest the same thing. I've tried initialising AN[4] as a just a
    > character pointer, then just as a character variable, neither works as
    > the rest of the function requires it to be both a pointer array
    > variable. I've tried referencing BAN =& AN[4], which neither worked
    > and rightly so as the function checks to see whether AN is isalpha
    > or a space, if encountered the next element in the array requires
    > checking, and the one preceeding it. BAN is used to get either the
    > preceeding element or the one following it.
    >
    > Thank you in advance for your help in this matter.
    >
    >
    >
    > bool CheckAddressN( char* record )
    > {
    >
    > char* AN[4];


    All right, you have an array of four pointers to char, each of which
    is of indeterminate value.

    > char* BAN;


    Now you have another pointer to char, also of indeterminate value.

    >
    > strncpy(AN[4], &record[27], 4);


    Now you're trying call `strncpy()' using as a destination a value
    that doesn't even exist! (in `char* AN[4]' valid indices range from
    0 to 3).

    You've invoked undefined behavior -- so *anything* can happen.

    > AN[4] = '\0';
    >
    >
    > for(int i=0; i < 4; i++)
    >
    > if(AN = " ") // check that last character is a number and the
    > next character



    Erm, no. You're *assigning* the address of the string literal
    '" "' to AN. All well and good (unlike the above), but
    certainly not what you intended to do. Besides, this test *cannot fail*.

    > {
    > //is a char.
    > BAN = (AN + 1 ) ;
    > if(!isalpha(BAN)){}
    >
    > }
    > return false;
    >
    > if(AN = " ") // check that last character is a number and the
    > next character


    See above. You're doing the same thing.

    > {
    > //is a char.
    > BAN = (AN - 1);
    > if(!isdigit(BAN)){};
    >
    > }
    > return false;
    >
    >
    > if(AN = ";")
    > {
    >
    > BAN = (AN - 1);
    >
    > if(!isdigit(BAN)){};
    > }
    > return false;
    >
    > if(AN = ";")
    > {
    > BAN = (AN + 1);
    >
    > if(!isalpha(BAN)){};
    >
    > }
    > return false;
    >
    > if(AN = ";")
    > {
    > BAN = (AN + 1);
    >
    > for(int i=0; i < 17; i++)
    > if(!isalpha(BAN))
    > return false;
    > }
    >
    > return true;
    >
    > }


    I'd be surprised if there aren't even more errors here.
    Rethink what you're doing, get a good C book (see
    http://www.accu.org for suggestions) and post back if you run into
    problems.

    Unfortunately, you've got a ways to go.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    Artie Gold, Sep 18, 2003
    #2
    1. Advertising

  3. muser wrote:
    > I'm writing a function that isn't showing as an error during
    > compilation, but it is at run time. The exception it is throwing
    > informs me that it is due to my two pointers in that function. It is
    > an access violation error, where the memory can't be read. I'll list
    > the things I've tried in order to resolve the problem so that no one
    > will suggest the same thing. I've tried initialising AN[4] as a just a
    > character pointer, then just as a character variable, neither works as
    > the rest of the function requires it to be both a pointer array
    > variable. I've tried referencing BAN =& AN[4], which neither worked
    > and rightly so as the function checks to see whether AN is isalpha
    > or a space, if encountered the next element in the array requires
    > checking, and the one preceeding it. BAN is used to get either the
    > preceeding element or the one following it.
    >
    > Thank you in advance for your help in this matter.
    >
    >
    >
    > bool CheckAddressN( char* record )
    > {
    >
    > char* AN[4];
    > char* BAN;
    >
    > strncpy(AN[4], &record[27], 4);


    Hugh ?

    You're copying to an unallocated, uninitialized pointer.

    > AN[4] = '\0';


    This would write to byte 5 ! The size is 4.

    I'll stop reading the rest.


    What are you trying to do ?
    Gianni Mariani, Sep 18, 2003
    #3
  4. muser

    Ron Natalie Guest

    "muser" <> wrote in message news:...
    > I'm writing a function that isn't showing as an error during
    > compilation,


    Undefined behavior is rarely caught at compile time.

    > char* AN[4];
    > char* BAN;
    >
    > strncpy(AN[4], &record[27], 4);
    > AN[4] = '\0';


    Yoiu have two major problems here. First, AN[4] is one past the end of the
    array. It's undefine behavior to access it.

    Second, AN is an array of four uninitialized pointers. You don't want pointers
    here it all it looks like. What you want is an array of 4 (well 5 actually) characters.

    char AN[5];
    strncpy(AN, &record[27], 4);
    AN[4] = '\0';


    > if(AN = " ") // check that last character is a number and the


    Again you have more problems. = is assignment NOT a test
    for equality. A single character is surrounded by apostrophes
    not quotes. The only reason this compiels at all is because of
    the misdeclaration of AN above.

    > BAN = (AN + 1 ) ;


    I can't even begin to understand what this is supposed to be.
    I suspect you wanted BAN to be just a char.

    BAN = AN + 1 ; // Add a logical one to the character value at AN.>
    or
    BAN = AN[i+1]; // character value at 1 past the index?

    > if(!isalpha(BAN)){}


    What on earth is the index for here?
    if(!isalpha(BAN))
    >


    I'm giving up. You need to learn the difference between:

    A character.
    A pointer.
    A string.

    Frankly, you should abandon the current scheme and switch to useing
    std::string. and it's member accessing and substr methods.
    Ron Natalie, Sep 18, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joost
    Replies:
    1
    Views:
    2,378
    bruce2424
    Jun 27, 2008
  2. Replies:
    0
    Views:
    399
  3. Peter
    Replies:
    34
    Views:
    1,917
    James Kanze
    Oct 17, 2009
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    657
  5. VSK
    Replies:
    0
    Views:
    237
Loading...

Share This Page