Avatar of hyaku
hyaku

asked on 

Assigning a char to an unsign char

How do i assign a char value to an unsign char?
Assuming char val = 1;
unsigned char ch;
ch should store the value of 1 when its being assign with val.

I seems to get weird result instead..
help pls..
C

Avatar of undefined
Last Comment
cup
Avatar of Infinity08
Infinity08
Flag of Belgium image

   char val = 1;
    unsigned char ch = val;

works perfectly. What weird result are you talking about ?
Avatar of hyaku
hyaku

ASKER

i got other numbers instead.
if the value of val i got it from a struct, could that be the cause of it?
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> i got other numbers instead.
Can you show me your exact code, and the way you "get the numbers" ?
Avatar of hyaku
hyaku

ASKER

typedef struct{
unsigned char  working[1];
} STATUS;

typedef struct{
char  is_working[1];
} CHECK_STATUS;
Assuming that is_working is storing the value of 1, 1 represent working.

CHECK_STATUS     cs;
STATUS   s
strcpy(s.working , cs->shift_i,1);
sprintf("Working status = %1.1s", s.working);

is this correct?
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> is this correct?
No, there are a few problems. Try this instead :

    typedef struct{
    unsigned char  working;
    } STATUS;

    typedef struct{
    char  is_working;
    } CHECK_STATUS;


    CHECK_STATUS     cs;
    STATUS   s
    s.working = cs->shift_i[0];
    sprintf("Working status = %d", (int) s.working);

Howis cs->shift_i defined ?
Avatar of cup
cup

If you're using strcpy, the size of the array has to be at least 2.

Shouldn't it be just

s.working[0] = cs.shift_i[0];
sprintf ("Working status = %d", (int) s.working[0]);

s.working gives the address of s.working.  It is OK for %s as that results in the contents of what the pointer is pointing to but for %d it will just print the address.
Avatar of hyaku
hyaku

ASKER

sry, shift _i is actually is_working.
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of hyaku
hyaku

ASKER

It works.
thanks for the help.
Avatar of cup
cup

If you're using C++, just use struct instead of typedefing i.e.

struct STATUS {
   unsigned char working
};

struct CHECK_STATUS {
char is_working
};

The rest of the code remains the same.
C
C

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, so it has found lasting use in applications that had formerly been coded in assembly language, including operating systems as well as various application software for computers ranging from supercomputers to embedded systems. It is distinct from C++ (which has its roots in C) and C#, and many later languages have borrowed directly or indirectly from C.

23K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo