converting char * in struct to a long value

J

jagmeena

Hello,

I am sure this problem has been addressed before, however, I could'nt get a
suitable solution to my problem. Hence I am posting here. Thanks a lot for
all your help.
The code I have is

typedef struct Rec {
unsigned char msg[10];
unsigned long len;
} rec;

static rec rc;
const unsigned char * tmp;

const char* getRec(const char* buf)
{
memset(&rc,0,sizeof(rc));
memcpy(rc.msg, buffer, strlen(buf);
rc.len = strlen(buf);
return (const char *)&rc;
}

long convertCharToLong(const unsigned char *lVal)
{
......
}

void main()
{
long v1;
char stg[100];
char buffer[255];
memset(buffer,0,255);
printf("Enter a string: ");
fgets(buffer);

tmp = getRec(buffer);

memset(stg,0,100);

memcpy(stg, tmp, 10);
printf("1. STG VALUE --> (%s)\n", stg);

v1 = convertCharToLong(tmp+10);
printf("2. LEN VALUE ---> (%ld)\n", v1);
}

The problem I have is writing the convertCharToLong function:
How do I convert the string I get from the offset, to a long value.

Thanks for all your help

jagmeena
(e-mail address removed)
 
K

Kevin Goodsell

jagmeena said:
Hello,

I am sure this problem has been addressed before, however, I could'nt get a
suitable solution to my problem. Hence I am posting here. Thanks a lot for
all your help.

Your code appears to be C. If you want a C answer, you won't get it in
comp.lang.c++. Please don't cross-post between comp.lang.c and
comp.lang.c++. Questions are rarely topical in both groups, and a good
solution in one is rarely a good solution in the other (or even a legal
solution).

-Kevin
 
D

David Rubin

jagmeena said:
Hello,

I am sure this problem has been addressed before, however, I could'nt get a
suitable solution to my problem. Hence I am posting here. Thanks a lot for
all your help.
The code I have is
[snip]
long convertCharToLong(const unsigned char *lVal)
{
......
}

strtol where lVal points to a string representation of a number (e.g.,
"12345").

/david
 
I

Irrwahn Grausewitz

(e-mail address removed) (jagmeena) wrote:

[ crosspost to c.l.c++ fixed ]

How do I convert the string I get from the offset, to a long value.
Just use strtol().

Regards

Irrwahn
 
J

Jack Klein

Hello,

I am sure this problem has been addressed before, however, I could'nt get a
suitable solution to my problem. Hence I am posting here. Thanks a lot for
all your help.
The code I have is

The code you have is littered with problems.
typedef struct Rec {
unsigned char msg[10];
unsigned long len;
} rec;

static rec rc;
const unsigned char * tmp;

const char* getRec(const char* buf)
{
memset(&rc,0,sizeof(rc));
memcpy(rc.msg, buffer, strlen(buf);

By looking ahead, I see that buffer is an array of 255 characters. If
you had correctly written code to get input from stdin into buffer,
which you didn't, there is a very good chance that strlen(buf) can be
anything up to 255. If it happens to be greater than 10, you will
overwrite the msg array and cause undefined behavior. If it is
greater than sizeof(struct Rec), which is implementation defined but
probably between 12 and 16, then you will really have heck to pay.

But if you don't overwrite your destination array, you still have
problems. You copy all the characters but not the terminating '\0',
so even if you fit everything in msg, it does not contain a string.

Well, actually as written it might contain a string the first time
this code is run, but only because rc is defined at file scope and so
initialized to all 0 bytes. If you ever more this structure inside a
function, where it belongs, you won't get a string. And if the user
types more than 9 characters, you still have the overflow.
rc.len = strlen(buf);
return (const char *)&rc;

Why would you do this? If you want an array of chars, use an array of
chars. If you use a structure, return a pointer to the structure.
From the rest of your code you don't know enough to be playing around
with casting types like this. Those who do know enough know to do it
only very rarely, and only for very good reasons.
}

long convertCharToLong(const unsigned char *lVal)
{
......
}

void main()

There is no "void main()", it's undefined behavior. In C, main()
returns an int.
{
long v1;
char stg[100];
char buffer[255];
memset(buffer,0,255);

Why the unnecessary call to memset(), which accomplishes nothing
useful?
printf("Enter a string: ");

On many platforms, this prompt will not appear until the user presses
the "enter" or "return" key at the end of his input. You need:

fflush(stdout);

....here to make sure it appears first.
fgets(buffer);

Obviously this is not code you compiled, because fgets() takes three
arguments, not one. Did you really use gets() but try to hide it
because you thought we would scold you?
tmp = getRec(buffer);

memset(stg,0,100);

What is with you and memset()? Is your computer too fast, so you
waste clock cycles to slow it down?
memcpy(stg, tmp, 10);
printf("1. STG VALUE --> (%s)\n", stg);

Of course, you might well not have a valid string here, so say hello
to undefined behavior.
v1 = convertCharToLong(tmp+10);

Exactly what do you think is at tmp+10? If your code did not blow up
with the undefined behavior of overflowing the array, the next thing
it did was to store a size_t value in rc.len. Now since tmp is
actually a pointer to the first byte of rc, tmp+10 points to the first
byte in rc after rc.msg. That is either a padding byte or the
location of rc.len. It certainly does not point to any characters, if
points to either a padding byte or an unsigned long.
printf("2. LEN VALUE ---> (%ld)\n", v1);
}

The problem I have is writing the convertCharToLong function:
How do I convert the string I get from the offset, to a long value.

If there is a string in rc at all, tmp+10 does not point to it.
Thanks for all your help

jagmeena
(e-mail address removed)

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
B

Barry Schwarz

Hello,

I am sure this problem has been addressed before, however, I could'nt get a
suitable solution to my problem. Hence I am posting here. Thanks a lot for
all your help.
The code I have is

Actually, this is probably not your code. (If it were, you would not
be able to compile it due to the syntax errors.) In the future, you
will get better responses if you use cut and paste instead of
retyping.
typedef struct Rec {
unsigned char msg[10];
unsigned long len;
} rec;

static rec rc;
const unsigned char * tmp;

const char* getRec(const char* buf)
{
memset(&rc,0,sizeof(rc));
memcpy(rc.msg, buffer, strlen(buf);

There is no buffer. Did you mean buf?

Is there some reason you didn't want to use strcpy?

Are you aware that this will copy all the characters including the
'\n' that fgets normally places in the buffer?

rc.msg is only 10 characters. Your code does nothing to insure the
input will fit. If it doesn't, you invoke undefined behavior.
rc.len = strlen(buf);
return (const char *)&rc;
}

long convertCharToLong(const unsigned char *lVal)
{

Look up the strtol function in your reference.
......
}

void main()

main returns int period
{
long v1;
char stg[100];
char buffer[255];
memset(buffer,0,255);

Why bother since you will read into this array immediately.
printf("Enter a string: ");

Without a fflush(stdout), there is no guarantee that this will be
visible when the next statement executes.
fgets(buffer);

Did you forget a couple of parameters?
tmp = getRec(buffer);

tmp is declared as const unsigned char *. getRec returns a const
char*. These are not compatible without a cast.
memset(stg,0,100);

memcpy(stg, tmp, 10);
printf("1. STG VALUE --> (%s)\n", stg);

v1 = convertCharToLong(tmp+10);

tmp points to rc.msg. You have no idea what tmp+10 points to since
there could be padding in the struct after msg.
printf("2. LEN VALUE ---> (%ld)\n", v1);
}

The problem I have is writing the convertCharToLong function:
How do I convert the string I get from the offset, to a long value.

You are not getting a string from the offset. If you want to convert
the input to long, you should be using tmp (after ensuring it points
to an area long enough to hold the entire string).



<<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,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top