1

I am a student and I will have a presentation at school about arrays. I have this code that should assign a whole array into another array.

#define MAX 10
#include <stdio.h>

typedef struct {
    int data[MAX];
} INT_ARR;

int main()
{
    int i;
    INT_ARR arr1 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    INT_ARR arr2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    arr1 = arr2;

    return 0;
}
  • How can I use this technique in real projects?
  • What I can do with this?
  • What are the pros and cons?
9
  • 2
    In real programming you should use memcpy() function to copy content of one array to another. Or maybe allocate arrays dynamically using malloc()/calloc(), and then just assign pointer of one array to another. Depends on your particular task. Commented Mar 15, 2015 at 13:36
  • Could someone with enough reputation please close this as duplicate to (for example): stackoverflow.com/q/4530319/694576 or stackoverflow.com/q/17500960/694576 (I accidently hammered in the wrong answer ... :-}) Commented Mar 15, 2015 at 13:40
  • Both the questions you provided as originals are different questions, @alk. This one is asking about practical considerations when doing array assignment in this specific way. I don’t see this method in the referenced questions and it’s certainly not their main topic. Commented Mar 15, 2015 at 14:43
  • @Palec: C does not allow assigment of arrays (poleX = poleY; simply does not compile). So I wonder what the "practical consideration when doing array assigments in this specifc way" should be, if not the "work-arounds" mentioned on the answers I linked. Commented Mar 15, 2015 at 14:50
  • 1
    @SamProtsenko I often see that people are just not familiar with such capability of C and it's too bad. Actually, it's a kind of basic automatic resources management. The example here is pretty trivial but in case of a more complex structures with both simple and array fields, this can be pretty useful. Commented Mar 15, 2015 at 22:46

2 Answers 2

3

Pros

  • Assignment is simple. You write minimum of code, the compiler does all the work.
    It’s hard for you as a programmer to make a mistake – both in correctness and performance. Machines don’t make errors and compilers are much better at optimization than humans.
  • No function call is needed.
    Function calls are slow, generally speaking. Sometimes the optimizer can get rid of them, but getting rid of one while preserving maintainability of the code cannot be bad.
  • When used as a local variable, the array is stored on the stack.
    Allocation is fast, deallocation is fast.

Cons

  • Assignment is simple. You write minimum of code, the compiler does all the work.
    It might not be obvious that such an assignment is quite an expensive operation. Certainly more expensive than a simple int assignment. Confusing your future self or any other contributors to your project is bad.
  • Typing arr1.data[i] is more annoying than typing arr1[i].
  • When used as a local variable, the array is stored on the stack.
    Therefore it must be really tiny. Otherwise you get stack overflow.
  • Unlike C99 variable-length arrays, these arrays have their length defined at compile time.
  • You rarely need this.
    Tiny arrays of fixed width with the ability of assignment en masse are not very useful in practice. If you come across their real-world application, please @ping me in a comment with its description, I will be interested.

I’d like to write more about the last point.

Basically you have d-dimensional vectors for a fixed d, known at compile time. This could be useful if d could change in a future version of the program, but I guess that this is an extremely unlikely scenario and that plain structs would be better in such an application. By plain structs I mean structs used not only as a minimal wrapper of a single array; sure they can still contain an array as a member.

What you need more is the ability to copy parts of an array and to assign them separately. The memcpy() function serves that purpose. You can use it even with the arrays inside structs, like the ones in this question.

Another way to look at array assignment is through pointers. Sometimes you don’t need to have two distinct chunks of memory, you need just several names for one. In such a scenario, you should use pointers to the memory and thus avoid copying the array. Pointer assignment is fast and simple.

Miscellaneous remarks to the code

  • The initializers should have double braces:

    INT_ARR arr2 = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}};
    

    The outer ones are for the struct, the inner ones for the array.

  • In your code, there is an unused int i. I guess it is a relict from testing your code.
  • In C, you should use int main(void) instead of int main(). The latter is deprecated syntax and may be removed in future versions of C. In function declarations (not definitions), empty parentheses mean “I don’t say anything about parameters”. Using this instead of explicit void can lead to very unpleasant surprises.
  • You should choose more descriptive and preferably English identifiers. Even if you’re not a native English speaker. Thus you enable others to collaborate with you. I edited your question to improve the naming convention.
Sign up to request clarification or add additional context in comments.

6 Comments

A big pro you didn't mention is that assignment via = can't go wrong; whereas with memcpy it is possible to copy the wrong number of bytes or to/from the wrong place.
It's not uncommon in real code to have a structure with many members, one (or more) of which is an array; and to have that struct copied by value.
@Palec There are many cases in which a structure describes configuration - nothing restricts the configuration to non-arrays only and it can often consist of various kinds of data.
Thanks, @MattMcNabb. I forgot to mention that simplicity of assignment makes it obviously correct. Edited.
I figured that the question asks about structs with just one member, which is of an array type, @MattMcNabb. Made that explicit. I believe there are other questions about arrays in structs generally.
|
1

There is no "one true answer". It depends what you want to do. In fact, you probably won't use this in real programming except for some basic school projects.

As Sam said, you should use memcpy(). The only con I can think of is code performance. Find out more here.

1 Comment

I strongly disagree with the advice to use memcpy ; it should be a last resort due to how error-prone it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.