pointers to pointers // exception handling error

M

muser

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;

}
 
A

Artie Gold

muser said:
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
 
G

Gianni Mariani

muser said:
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 ?
 
R

Ron Natalie

muser said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top