char array

cat int.c

int main() {
unsigned char wwn[8];
    wwn[0]=50;
    wwn[1]=00;
    wwn[2]=53;
    wwn[3]=30;
    wwn[4]=08;
    wwn[5]=09;
    wwn[6]=82;
    wwn[7]=17;
printf("WWN: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n ",
            wwn[0],
            wwn[1],
            wwn[2],
            wwn[3],
            wwn[4],
            wwn[5],
            wwn[6],
            wwn[7]);

return;
}
{367}: cc int.c
int.c:8:9: invalid digit "8" in octal constant
int.c:9:9: invalid digit "9" in octal constant
{368}: ./a.out
WWN: 30:78:35:30:30:30:35:33
 {369}:

questions:
1) Why i did not get the same value which i assigned?
2) Why warning message is coming while compiling?

When a number starts with a leading "0", it's interpreted as an octal number, which can only use digits 0-7 (inclusive). Fix this, recompile and see what the output is.

Also, do you know that using %x will print out your number in hexadecimal?

1 Like

Thanks for ur input JohnGraham

cat int.c

int main() {
unsigned char wwn[8];
unsigned char pwwn[8]="0x5000533008098217";
    wwn[0]=0x50;
    wwn[1]=0x00;
    wwn[2]=0x53;
    wwn[3]=0x30;
    wwn[4]=0x8;
    wwn[5]=0x9;
    wwn[6]=0x82;
    wwn[7]=0x17;
printf("WWN: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n ",
            wwn[0],
            wwn[1],
            wwn[2],
            wwn[3],
            wwn[4],
            wwn[5],
            wwn[6],
            wwn[7]);
printf("\nPWWN: %x\n",pwwn);
return;
}

{222}: cc int.c
int.c: In function `main':
int.c:4: warning: initializer-string for array of chars is too long

{223}: ./a.out
WWN: 50:00:53:30:08:09:82:17
 
PWWN: bfe78d98
{224}:

questions:
1) How can i make it to an single shot assignment?

Using general array assignment

unsigned char wwn[8] = {0x50, 0x00, 0x53, 0x30, 0x08, 0x09, 0x82, 0x17};

OR

Using string literal syntax

unsigned char wwn[8] = "\x50\x00\x53\x30\x08\x09\x82\x17";
1 Like

Hi Expl and JohnGraham

1)Can i know the reason why the compile doesn't understand the following assignment?

(i)unsigned char pwwn[8]="0x5000533008098217";
or
(ii)unsigned char wwn[8]=0x5000533008098217;

2)Is there is any why to print the pwwn or wwn single shot?
like printf("\nPWWN: %x\n",pwwn);

(i) you are assigning simple ASCII string here that is literally "0x5000533008098217". But since you are defining 'pwwn' to be 8 byte long the string will be striped by compiler to first 8 characters so the assigned string becomes "0x500053" without 0 byte at the end.

(ii) this is just wrong, you cant assign any kind of number to a stack array directly. To set content to an array at this step is possible only with ways from my previous post.

Unless you write/find a custom function to do it, no you can't.

1 Like

Thanks a lot explman ("expl")

Hi Expl and JohnGraham

i

nt main() {
unsigned char wwn[8]="\x50\x00\x53\x30\x8\x9\x82\x17";
    unsigned char wwn1[8]="\x50\x00\x53\x30\x08\x09\x82\x21";
    print_wwn(wwn);
    print_wwn(wwn1);
  strcpy(wwn1,"\x50\x00\x53\x30\x08\x09\x82\x32");
    print_wwn(wwn1);
return;
}
int print_wwn(unsigned char wwn[8]) {
printf("WWN: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n ",
            wwn[0],
            wwn[1],
            wwn[2],
            wwn[3],
            wwn[4],
            wwn[5],
            wwn[6],
            wwn[7]);
return 0;
}

output:

{250}: cc key.c
{251}: ./a.out
WWN: 50:00:53:30:08:09:82:17
 WWN: 50:00:53:30:08:09:82:21
 WWN: 50:00:53:30:08:09:82:21
{252}:

Question:
Why the string copy does not working??

strcpy(wwn1,"\x50\x00\x53\x30\x08\x09\x82\x32");

strcpy() will copy until 0 terminated char is reached and you have 0 char in second position so it will copy only first byte and leave the rest the same.

For binary copy you should use memmove():

memmove(wwn1, "\x50\x00\x53\x30\x08\x09\x82\x32", sizeof(wwn1));
1 Like