-3

Lets say i have an array that holds structs called computer; Whenever I try to assign an element to the array it uses the "memcpy()" function, however i do not have access to this function or any other libraries. How do I assign an element to this array by reference without using memcpy()?

This would be my code if I had access to memcpy()

struct Computer{
    int val;
}; 
int main(){
    int i = 0;
    Computer list[10];

    Computer *p;
    p->val = 5; 
    list[i] = *p; 

}
  • 1
    Can you show some complete/compileable code you want to use? You say that your code would look like that if you had `memcpy`, but `memcpy` isn't in the code. In addition, `p` isn't initialized, and `array` and `i` aren't defined either. – Carl Norum Feb 20 '19 at 05:16
  • The compiler will automatically use the memcpy function when copying *p into the array. Hence when I compile I get an error saying "memcpy is not defined" because I am working in an environment where I cannot use it. P is just an instance of the computer struct and the i value doesn't matter. – Diego Deiew Feb 20 '19 at 05:23
  • 1
    Welcome to Stack Overflow. Please take the time to go through the [The Tour](http://stackoverflow.com/tour) and refer to the material from the [Help Center](http://stackoverflow.com/help/asking) on what and how you can ask here.It's specially important to post a [mcve]. – R Sahu Feb 20 '19 at 05:27
  • 1
    The compiler is permitted to make assumptions about its execution environment - the two are a pair. You can try using the compiler in freestanding mode, which tells it to assume as little about the environment as possible, [but even then, it will call memcpy](http://cs107e.github.io/guides/gcc/). – Raymond Chen Feb 20 '19 at 05:29
  • 1
    include your compiler with version and your build steps – kmdreko Feb 20 '19 at 05:30
  • It is for an operating system, so I will not be able to use any flags or libraries. There is a way to do this by making the array hold the struct by reference, I just don't know how to do it. – Diego Deiew Feb 20 '19 at 05:34
  • Arrays of references are illegal in C++: https://stackoverflow.com/questions/1164266/why-are-arrays-of-references-illegal – asimes Feb 20 '19 at 05:44
  • 1
    Why not just implement `memcpy` if that's the problem? – Carl Norum Feb 20 '19 at 06:29

1 Answers1

0

Below is an array of Computer by value or by address if you prefer. Having an array of Computer* const is essentially the same as references because they cannot be reassigned:

struct Computer {
    int val;
    Computer() = delete;
    Computer(int a) : val(a) {}
};

int main() {
    Computer byValue[] = {
        Computer(1),
        Computer(2),
        Computer(3)
    };

    Computer c1(1);
    Computer c2(2);
    Computer c3(3);
    Computer* const byAddress[] = {
        &c1,
        &c2,
        &c3
    };

    return 0;
}

Arrays of references are illegal in C++: Why are arrays of references illegal?

asimes
  • 5,749
  • 5
  • 39
  • 76