pointers to pointers // exception handling error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • muser

    pointers to pointers // exception handling error

    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[i] 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[i] = " ") // check that last character is a number and the
    next character
    {
    //is a char.
    BAN = (AN[i] + 1 ) ;
    if(!isalpha(BAN[i])){}

    }
    return false;

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

    }
    return false;


    if(AN[i] = ";")
    {

    BAN = (AN[i] - 1);

    if(!isdigit(BAN[i])){};
    }
    return false;

    if(AN[i] = ";")
    {
    BAN = (AN[i] + 1);

    if(!isalpha(BAN[i])){};

    }
    return false;

    if(AN[i] = ";")
    {
    BAN = (AN[i] + 1);

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

    return true;

    }
  • Artie Gold

    #2
    Re: pointers to pointers // exception handling error

    muser wrote:[color=blue]
    > 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[i] 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];[/color]

    All right, you have an array of four pointers to char, each of which
    is of indeterminate value.
    [color=blue]
    > char* BAN;[/color]

    Now you have another pointer to char, also of indeterminate value.
    [color=blue]
    >
    > strncpy(AN[4], &record[27], 4);[/color]

    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.
    [color=blue]
    > AN[4] = '\0';
    >
    >
    > for(int i=0; i < 4; i++)
    >
    > if(AN[i] = " ") // check that last character is a number and the
    > next character[/color]


    Erm, no. You're *assigning* the address of the string literal
    '" "' to AN[i]. All well and good (unlike the above), but
    certainly not what you intended to do. Besides, this test *cannot fail*.
    [color=blue]
    > {
    > //is a char.
    > BAN = (AN[i] + 1 ) ;
    > if(!isalpha(BAN[i])){}
    >
    > }
    > return false;
    >
    > if(AN[i] = " ") // check that last character is a number and the
    > next character[/color]

    See above. You're doing the same thing.
    [color=blue]
    > {
    > //is a char.
    > BAN = (AN[i] - 1);
    > if(!isdigit(BAN[i])){};
    >
    > }
    > return false;
    >
    >
    > if(AN[i] = ";")
    > {
    >
    > BAN = (AN[i] - 1);
    >
    > if(!isdigit(BAN[i])){};
    > }
    > return false;
    >
    > if(AN[i] = ";")
    > {
    > BAN = (AN[i] + 1);
    >
    > if(!isalpha(BAN[i])){};
    >
    > }
    > return false;
    >
    > if(AN[i] = ";")
    > {
    > BAN = (AN[i] + 1);
    >
    > for(int i=0; i < 17; i++)
    > if(!isalpha(BAN[i]))
    > return false;
    > }
    >
    > return true;
    >
    > }[/color]

    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

    Comment

    • Gianni Mariani

      #3
      Re: pointers to pointers // exception handling error

      muser wrote:[color=blue]
      > 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[i] 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);[/color]

      Hugh ?

      You're copying to an unallocated, uninitialized pointer.
      [color=blue]
      > AN[4] = '\0';[/color]

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

      I'll stop reading the rest.


      What are you trying to do ?

      Comment

      • Ron Natalie

        #4
        Re: pointers to pointers // exception handling error


        "muser" <charlie12345@h otmail.com> wrote in message news:f9a2a258.0 309180915.2326c 1b5@posting.goo gle.com...[color=blue]
        > I'm writing a function that isn't showing as an error during
        > compilation,[/color]

        Undefined behavior is rarely caught at compile time.
        [color=blue]
        > char* AN[4];
        > char* BAN;
        >
        > strncpy(AN[4], &record[27], 4);
        > AN[4] = '\0';[/color]

        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';

        [color=blue]
        > if(AN[i] = " ") // check that last character is a number and the[/color]

        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.
        [color=blue]
        > BAN = (AN[i] + 1 ) ;[/color]

        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[i] + 1 ; // Add a logical one to the character value at AN[i].>
        or
        BAN = AN[i+1]; // character value at 1 past the index?
        [color=blue]
        > if(!isalpha(BAN[i])){}[/color]

        What on earth is the index for here?
        if(!isalpha(BAN ))[color=blue]
        >[/color]

        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.


        Comment

        Working...