2

I thought I was finally getting somewhere in C, but have hit a brick wall on this one. I've just spent the last 30 mins looking up possible solutions, some involving memcpy and strcpy etc, but none seem to fix my issue. It may be a case of me not using them correctly.

I've created a basic program illustrating my problem. I'm hoping be shown how to fix this part of the code will help me better understand where I'm going wrong.

#include <stdio.h>
#include <stdlib.h>

//The program is trying to attempt to give 'test[0] a value of 255'

int main(int argc, char ** argv)
{
    //example variables
    int height = 4;
    int width = 4;
    unsigned char image[width][height];
    char *test;

    //gave the 0th element a basic value
    image[0][0] = 255; 
    //prints out 'Image = 255' as expected
    printf("Image = %d\n", image[0][0]);
    //allocate test some memory
    test = malloc(height * width * sizeof(char));

    //Now the problems..

    //attempt to give test[0] the value of image[0][0]
    test[0] = image[0][0];

    //prints out '-1'
    printf("Test = %d\n", test[0]);
}
gsamaras
  • 71,951
  • 46
  • 188
  • 305
AspiringNewbie
  • 113
  • 1
  • 7
  • 4
    Whether 30 mins are enough or not to ask in SO is debatable. On the other hand, providing an MCVE to demonstrate your problem is awesome, +1. – gsamaras Jan 11 '19 at 12:02
  • The best idea is to get rid of everything `char` and use `uint8_t` from stdint.h instead. Only use `char` for strings. – Lundin Jan 11 '19 at 12:26

2 Answers2

3

Change this:

char *test;

to this:

unsigned char *test;

since you want the destination array to be of the same type as the source array.

Live Demo

PS: A char usually (as @Bodo commented) defaults to a signed char, where 255 results in -1.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 2
    It is implementation-defined whether `char` is signed or unsigned by default. Many systems use a signed default type. see https://stackoverflow.com/questions/17097537/why-is-char-signed-by-default-in-c/17097580 – Bodo Jan 11 '19 at 12:03
  • 1
    Wow thank you, can't believe I didn't even consider this. I completely overlooked the meaning of unsigned until now. Appreciate the help, when researching Into the problems I was definitely going down a wrong path.. – AspiringNewbie Jan 11 '19 at 12:03
  • @AspiringNewbie you are welcome! Signedness is important, so make sure you won't ignore it next time! :) – gsamaras Jan 11 '19 at 12:06
  • @Bodo I didn't know that, thank you! Answer improved. However, I think this [Is char signed or unsigned by default?](https://stackoverflow.com/questions/2054939/is-char-signed-or-unsigned-by-default) is better, hope you will agree. – gsamaras Jan 11 '19 at 12:08
2

You have defined test as a pointer to a char. However, you are assigning an unsigned char to a char (which [apparently] is signed by default [in your environment]), so 255 is expressed as -1. Try this:

int main(int argc, char ** argv)
{
    //example variables
    int height = 4;
    int width = 4;
    unsigned char image[width][height];
    unsigned char *test;

    //gave the 0th element a basic value
    image[0][0] = 255; 
    //prints out 'Image = 255' as expected
    printf("Image = %d\n", image[0][0]);
    //allocate test some memory
    test = malloc(height * width * sizeof(char));

    //attempt to give test[0] the value of image[0][0]
    test[0] = image[0][0];

    //now prints out '255'
    printf("Test = %u\n", test[0]);
}
Jose
  • 3,306
  • 1
  • 17
  • 22
  • Thank you both so much, you saved me even more frustrating hours googling the wrong things in hope to fix it. Genuinely didn't even contemplate the unsigned issue. I Hope one day when I know more I can help the community back in return. Thanks again :) – AspiringNewbie Jan 11 '19 at 12:05
  • 1
    Actually they defined the variable as `char`, which is different to `signed char` and `unsigned char` both. This is an oddity in the C language, see this: https://stackoverflow.com/questions/2054939/is-char-signed-or-unsigned-by-default – Lundin Jan 11 '19 at 12:25
  • Thank you for pointing that out and the link @Lundin! I'm going to edit for a more accurate answer. – Jose Jan 11 '19 at 12:39