error when passing value to function

R

ritchie

Hi,

Just wanted to ask if anybody has any idea what i'm doing wrong.

I have a function which edits a linked list. It works fine.
I pass in a pointer to the list plus the value of the item to be
edited.

The value to be edited is a string which has been converted to an int
using atoi.
It is converted fine, then I pass this value to the function and when
I try to access the LL values I get an access violation error.

I debugged the code and I cannot figure out what's going wrong.

I also tried converting the string to an int in the same way, within
the edit function and this works fine.

Has anyone experienced this before??

Thanks in advance,
Ritchie.

/***************************code*************************/
-----------------------------------------
Function call in main --
editList( &nStart, convertDate( ) );
(value returned is correct)
-----------------------------------------

int convert( ) /*takes input as string & converts to int */
{
char sYear[1], sMonth[1], sDay[1], sFullDate[9]="";
int iFullDate=0;

printf("Enter day (dd): "); scanf("%s", sDay);
printf("Enter month (mm): "); scanf("%s", sMonth);
printf("Enter year (yyyy): "); scanf("%s", sYear);

strcat(sFullDate, sYear);
strcat(sFullDate, sMonth);
strcat(sFullDate, sDay);

/*convert full date to int*/
return iFullDate = atoi(sFullDate);
}

/*-------------------edit-------------------------------*/
void edit( NodePtr *sPtr, int iData)
{
NodePtr prevPtr, currPtr;

if( iData == (*sPtr)->iOrderDate.iFullDate ) /*THIS IS WHERE I GET
ERROR*/
{
printf("Account found > ");
...
...
...
/*********************************************************/
 
L

Leor Zolman

Hi,

Just wanted to ask if anybody has any idea what i'm doing wrong.

I have a function which edits a linked list. It works fine.
I pass in a pointer to the list plus the value of the item to be
edited.

The value to be edited is a string which has been converted to an int
using atoi.
It is converted fine, then I pass this value to the function and when
I try to access the LL values I get an access violation error.

I debugged the code and I cannot figure out what's going wrong.

What do you mean by "debugged"? If you have an IDE that shows the value of
your data, and you looked at the contents of sYear, sMonth and sDay as you
were stepping through the first part of convert(), the problem should have
been pretty obvious...
I also tried converting the string to an int in the same way, within
the edit function and this works fine.

My /guess/ is that you did them one at a time (scanf, convert, scanf,
convert, scanf convert), right? You might have gotten away with that, if
you were unlucky ;-)
Has anyone experienced this before??

Buffer overruns? Yup, at least once or twice ;-)

You've sized your buffers as ONE-element char arrays. That means they each
have room for their terminating NUL, and nothing else. To make this work
more-or-less the way you intend, make those buffers bigger. If you really
want a robust app, throw away everything you've done for the input routine
and start over using functions you can sanity-check along the way. For
example, you might consider having the user enter all the data into one
char buffer (using, say, fgets), then using sscanf to convert all the
values at once and validate that they were provided. After that, you can
check the values to be valid for what they represent, of course, as well.
-leor
Thanks in advance,
Ritchie.

/***************************code*************************/
-----------------------------------------
Function call in main --
editList( &nStart, convertDate( ) );
(value returned is correct)
-----------------------------------------

int convert( ) /*takes input as string & converts to int */
{
char sYear[1], sMonth[1], sDay[1], sFullDate[9]="";
int iFullDate=0;

printf("Enter day (dd): "); scanf("%s", sDay);
printf("Enter month (mm): "); scanf("%s", sMonth);
printf("Enter year (yyyy): "); scanf("%s", sYear);

strcat(sFullDate, sYear);
strcat(sFullDate, sMonth);
strcat(sFullDate, sDay);

/*convert full date to int*/
return iFullDate = atoi(sFullDate);
}

/*-------------------edit-------------------------------*/
void edit( NodePtr *sPtr, int iData)
{
NodePtr prevPtr, currPtr;

if( iData == (*sPtr)->iOrderDate.iFullDate ) /*THIS IS WHERE I GET
ERROR*/
{
printf("Account found > ");
...
...
...
/*********************************************************/

Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
B

Barry Schwarz

Hi,

Just wanted to ask if anybody has any idea what i'm doing wrong.

I have a function which edits a linked list. It works fine.
I pass in a pointer to the list plus the value of the item to be
edited.

The value to be edited is a string which has been converted to an int
using atoi.
It is converted fine, then I pass this value to the function and when
I try to access the LL values I get an access violation error.

I debugged the code and I cannot figure out what's going wrong.

I also tried converting the string to an int in the same way, within
the edit function and this works fine.

Has anyone experienced this before??

Thanks in advance,
Ritchie.

/***************************code*************************/

Where is the function editList defined?
-----------------------------------------

int convert( ) /*takes input as string & converts to int */
{
char sYear[1], sMonth[1], sDay[1], sFullDate[9]="";
int iFullDate=0;

printf("Enter day (dd): "); scanf("%s", sDay);
printf("Enter month (mm): "); scanf("%s", sMonth);
printf("Enter year (yyyy): "); scanf("%s", sYear);

Each of these invokes undefined behavior due to overflow of the
destination arrays.
strcat(sFullDate, sYear);
strcat(sFullDate, sMonth);
strcat(sFullDate, sDay);

/*convert full date to int*/
return iFullDate = atoi(sFullDate);
}

/*-------------------edit-------------------------------*/
void edit( NodePtr *sPtr, int iData)
{
NodePtr prevPtr, currPtr;

if( iData == (*sPtr)->iOrderDate.iFullDate ) /*THIS IS WHERE I GET
ERROR*/

What does *sPtr evaluate to? Is it possibly NULL? Since you don't
show the call to this function, it is a little difficult to say what
is wrong.
{
printf("Account found > ");
...
...
...
/*********************************************************/



<<Remove the del for email>>
 

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

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,221
Latest member
TrinidadKa

Latest Threads

Top