Assigning value to variable of type char

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

Post Reply
aligorith
Posts: 0
Joined: Sun Dec 05, 2004 3:40 am
Location: Chch, NZ

Assigning value to variable of type char

Post by aligorith »

Hi,

What should be assigned to a variable that is of type char?

Aligorith

z3r0_d
Posts: 289
Joined: Wed Oct 16, 2002 2:38 am
Contact:

Post by z3r0_d »

I can only guess what you actually mean

but characters, the single byte ones

char q = 'q';

char newline = '\n';

aligorith
Posts: 0
Joined: Sun Dec 05, 2004 3:40 am
Location: Chch, NZ

Post by aligorith »

Looking around, I found that you could use strcpy.

For example:
strcpy(data->myvar, "")

Have I got this right? That's the way the subtarget value is set in blender/source/blender/src/buttons_object.c

Aligorith

Lynx3d
Posts: 0
Joined: Tue Jan 24, 2006 5:09 pm

Post by Lynx3d »

don't mix up "char var1" with "char *var2"...
var1 holds exactly one byte, possible assignments go from -128 to +127. If you assign 'a' it just gets converted to the corresponding ASCII value (97 decimal in this case). var2 is just a pointer, pointing to a potentially large buffer, or even nowhere usefull at all...

char *strcpy(char *s1, const char *s2) takes two pointers to char, assuming that s1 points to a buffer capable of holding at least strlen(s2)+1 bytes. If not, bad things will happen ;)

-edit-
uhm wait i think char is not not necessarily signed, so its contents may be interpreted as 0-255 depending on platform/compiler(-settings)...

Post Reply