Thread: Returning and receiving an unsigned char array

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    20

    Returning and receiving an unsigned char array

    Why is this giving me a problem? How can I fix it? I'm basically passing a size containing a value of 2 to the makeArray function. Then I make an unsigned char* array Inside the makeArray function, fill its first two subscript, then return that array to main.cc.


    Error:
    main.cc:13:8: error: array type 'unsigned char *[size]' is not assignable


    array = makeArray(size);


    ~~~~~ ^


    1 error generated.


    make: *** [main.o] Error 1




    main.cc
    Code:
        int size = 2;
    
    
        unsigned char* array[size];
    
    
        array = makeArray(size);
    
    
    
    
        for (int i = 0; i < size; i++) {
            printf("%s\n", array[i]);
    
    
        } // end of for loop


    functions.cc
    Code:
    unsigned char* makeArray(int size) {
        unsigned char* array = new unsigned char[size];
    
    
        // fill in the array
        array[0] = "1";
        array[1] = "0";
    
    
        // return array
        return array;
    
    
    } // end of makeArray
    Last edited by asilvester635; 02-16-2017 at 02:34 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    The problem is exactly what it's saying. You can't assign "1" (a string) to a char. It needs to be '1' (an actual char).

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    20
    Hey, I made the changes, but it's still giving me this error

    Error:
    main.cc:13:8: error: array type 'unsigned char *[size]' is not assignable
    array = makeArray(size);
    ~~~~~ ^
    1 error generated.
    make: *** [main.o] Error 1

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    array needs to be an unsigned char *.

  5. #5
    Registered User
    Join Date
    Jan 2017
    Posts
    20
    I'm sorry, but my array is in that format already. Ultimately I want to store the pointer to an unsigned char* array that I made in makeArray() into an unsigned char* array that I created in main.cc. How do I achieve that?
    Last edited by asilvester635; 02-16-2017 at 03:23 PM.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    No, array as you defined it is
    Code:
    unsigned char * array[size];
    It needs to be
    Code:
    unsigned char * array;
    I.e., you defined it as an array of pointers, but it needs to just be a simple pointer in order to receive the address of a dynamically-allocated array.

  7. #7
    Registered User
    Join Date
    Jan 2017
    Posts
    20
    Alright, when I did that and compiled It told me to change the print line in my for loop of main from printf("%s\n", array[i]); to printf("%c\n", array[i]);.

    I recompiled and it gave me this error located inside the function.cc. Where in my code am I initializing the return object of type 'unsigned char*' with an lvalue of 'unsigned char'?

    Error:
    function.cc:49:9: error: cannot initialize return object of type
    'unsigned char *' with an lvalue of type 'unsigned char'
    return *array;
    ^~~~~~
    1 error generated.
    make: *** [readWritePPM.o] Error 1

    Then
    I changed the portion where I fill in the array inside my makeArray function to the code below. I declared that the array is of size 2, but I never really initialized the last storage location array[2]. It works now but why does it let me initialize three slots inside the array when I only made it size 2? Aren't we overriding some other storage location? Possibly erasing a piece of memory (might be important data) and replacing it with another value using this code array[2] = '1';?

    Code:
    unsigned char* makeArray(int size) {
        unsigned char* array = new unsigned char[size];
    
    
        // fill in the array
        array[0] = '1';
        array[1] = '0';
        array[2] = '1';
    
    
        // return array
        return array;
    
    
    } // end of makeArray
    Last edited by asilvester635; 02-16-2017 at 04:51 PM.

  8. #8
    Registered User
    Join Date
    Jan 2017
    Posts
    20
    It works perfectly now. Thanks.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <vector>
    #include <string>
    
    constexpr int size = 2; // Use constexpr for constant data, if possible; otherwise const.
    std::vector<std::string> array{ makeArray(size) }; // Use std::vector for arrays and std::string for strings. Use brace initialisers if possible.
    for (auto & str : array) // Use range-for loop if possible
        std::cout << str << "\n"; // Use std::cout for output, not printf.
    Code:
    std::vector<std::string> makeArray(int size)
    {
        return { "1", "0" };
    } // end of makeArray
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help w/ Receiving a Char Array w/ a Function
    By StevenSegal in forum C Programming
    Replies: 2
    Last Post: 10-31-2015, 01:02 PM
  2. Compare unsigned char array with const char*
    By Lazar in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2015, 08:36 AM
  3. Unsigned char array to unsigned char?
    By Kishintai in forum C Programming
    Replies: 16
    Last Post: 04-26-2013, 01:37 PM
  4. uincode char array to array<unsigned char,1>^
    By ripspinner in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2009, 05:41 PM
  5. Converting unsigned long array to unsigned char array
    By delvec28 in forum C Programming
    Replies: 2
    Last Post: 09-07-2009, 08:53 PM

Tags for this Thread