0

I'm constructing an unsigned char * in c and I want to know how I finish it. So usually at the end of my memories I put '\0' but unsigned char recognize it as a 0.

so when I do something like that :

void complement(unsigned char *c, int n){
  while(*c!='\0'){
    printf("%d\n", n-(*c));
    c++;
  }
}

it stop when I read "0" (and when I read 0 I want to print n, the complement)

So what can I use to have a proper ending condition to my while ?

Braiam
  • 1
  • 11
  • 47
  • 78
Jackie
  • 143
  • 1
  • 3
  • 8
  • 1
    Just add 'printf("%d\n",n);' after the while block. – Secto Kia Oct 08 '16 at 13:04
  • I want to read all 0 not only one, when c = "00002552552555" for exemple when I read my 0 it transform to 255. – Jackie Oct 08 '16 at 14:59
  • So is this byte data, not ASCII character data? If it's byte data with a valid range of 0 to 255, you will have to provide the length. There will be no value in the data that will flag the end. – Rob K Feb 03 '17 at 18:19

1 Answers1

0

Perhaps you're allowed to switch from while to do-while like this?

#include <stdio.h>

void complement(unsigned char *c, int n)
{
  do{
    printf("%d\n", n-(*c));
    printf("The character is %c\n", *c); // Just for debugging...
  }while(*(c++)!='\0');
}

int main(){
unsigned char *toPrint = (unsigned char *)"Print me!\0";
complement(toPrint, 0);
return 0;
}
  • I got .pgm image so I have some valus for my pixels, I put them in my unsigned char * so when i read "0" while will stop but I don't want to stop when I read 0. – Jackie Oct 08 '16 at 14:58
  • Can you show the invocation of complement and the initialization of c? – Nikolay Tsanov Oct 08 '16 at 15:44