7

In higher level languages I would be able something similar to this example in C and it would be fine. However, when I compile this C example it complains bitterly. How can I assign new arrays to the array I declared?

int values[3];

if(1)
   values = {1,2,3};

printf("%i", values[0]);

Thanks.

Daniel Wood
  • 4,487
  • 3
  • 38
  • 36
  • 1
    See here for some ideas: http://stackoverflow.com/questions/1223736/c-change-all-values-of-an-array-of-structures-in-one-line/1223806#1223806 – Karl Voigtland Oct 17 '09 at 13:09

9 Answers9

12

You can only do multiple assignment of the array, when you declare the array:

int values[3] = {1,2,3};

After declaration, you'll have to assign each value individually, i.e.

if (1) 
{
  values[0] = 1;
  values[1] = 2;
  values[2] = 3;
}

Or you could use a loop, depending on what values you want to use.

if (1)
{
  for (i = 0 ; i < 3 ; i++)
  { 
    values[i] = i+1;
  }
}
Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
8

In C99, using compound literals, you could do:

memcpy(values, (int[3]){1, 2, 3}, sizeof(int[3]));

or

int* values = (int[3]){1, 2, 3};
Karl Voigtland
  • 7,637
  • 34
  • 29
  • 3
    +1; keep in mind that in the latter case, the array will have automatic storage duration, ie returning it from a function will get you eaten by a grue – Christoph Oct 17 '09 at 13:58
  • Christoph, would that mean that `memmove`ing from such an array would involve first initializing it in automatic storage and then copying it over? – Michael Krelin - hacker Oct 17 '09 at 14:00
  • @hacker: in priciple, yes, in practice, the compiler will optimize it away (for gcc, `-O1` is enough) – Christoph Oct 17 '09 at 14:14
  • On the other hand, are you sure it is really automatic storage? Isn't this similar to string literals? Or would using `const` keyword make it behave the way string literals would? – Michael Krelin - hacker Oct 17 '09 at 17:29
  • @hacker: yes, `const` compound literals may be shared the same way strings are (this is an implementation detail and has no bearing on language semantics); non-const literals are guaranteed to be distinct on each entry to the enclosing block – Christoph Oct 17 '09 at 19:01
  • Yes, but now that I think of it, I don't think I've ever seen non-const literals in C before… – Michael Krelin - hacker Oct 17 '09 at 20:26
  • Also, keep in mind as the answerer said that this is C99, not the more popular ISO C90. – BobbyShaftoe Oct 18 '09 at 00:38
4
 //compile time initialization
 int values[3] = {1,2,3};

//run time assignment
 value[0] = 1;
 value[1] = 2;
 value[2] = 3;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
pierrotlefou
  • 39,805
  • 37
  • 135
  • 175
3

you can declare static array with data to initialize from:

static int initvalues[3] = {1,2,3};
…
if(1)
    memmove(values,initvalues,sizeof(values));
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1
#include<stdio.h>
#include<stdlib.h>
#include<stdarg.h>

int *setarray(int *ar,char *str)
{
    int offset,n,i=0;
    while (sscanf(str, " %d%n", &n, &offset)==1)
    {
        ar[i]=n;
        str+=offset;
        i+=1;
    }
    return ar;
}

int *setarray2(int *ar,int num,...)
{
   va_list valist;
   int i;
   va_start(valist, num);

   for (i = 0; i < num; i++) 
        ar[i] = va_arg(valist, int);
   va_end(valist);
   return ar;
}

int main()
{
    int *size=malloc(3*sizeof(int*)),i;
    setarray(size,"1 2 3");

    for(i=0;i<3;i++)
        printf("%d\n",size[i]);

    setarray2(size,3 ,4,5,6);
    for(i=0;i<3;i++)
        printf("%d\n",size[i]);

    return 0;
}
frozenat99
  • 11
  • 3
  • you can also loop sscanf from a function then return the array – frozenat99 Jul 31 '16 at 01:55
  • Welcome to Stack Overflow. Please read the [About] page soon. When you answer a question over 6 years after it is asked (and it has up-voted and accepted answers), you must have something distinctive to add — a novel approach, or perhaps the product has changed since the original answers were given. Fortunately for you, none of the other answers suggests parsing a string representation of the array values — so that much is OK. I'm not sure it is a good mechanism, but it is different. Your call to `malloc()` is not allocating enough space on any machine where `sizeof(char) != sizeof(int)`. – Jonathan Leffler Jul 31 '16 at 02:09
  • edited malloc, if using strings does not work, you can also use the functions from stdarg.h – frozenat99 Jul 31 '16 at 11:28
0

I would post this as a comment, but I don't have enough reputation. Another (perhaps dirty) way of initializing an array is to wrap it in a struct.

#include <stdio.h>

struct wrapper { int array[3]; };

int main(){
    struct wrapper a;
    struct wrapper b = {{1, 2, 3}};

    a = b;

    printf("%i %i %i", a.array[0], a.array[1], a.array[2]);

    return 0;
}
GRAYgoose124
  • 621
  • 5
  • 24
0

It is also possible to hide the memcpy by using the compiler's block copy of structs. It makes the code ugly because of all the .i and i: but maybe it solves your specific problem.

typedef struct {
    int i[3];
} inta;

int main()
{
    inta d = {i:{1, 2, 3}};

    if (1)
        d = (inta){i:{4, 5, 6}};

    printf("%d %d %d\n", d.i[0], d.i[1], d.i[2]);

    return 0;
}
eyalm
  • 3,366
  • 19
  • 21
0

There is also this... :)

char S[16]="";
strncpy(S,"Zoodlewurdle...",sizeof(S)-1);

Test what happens if you declare S[8] or S[32], to see why this is so effective.

I wrote my own string functions based on the logic of OpenBSD's strlcpy, aimed at ensuring a terminator byte MUST exist in the event of overflow, and standard strncpy won't do this so you have to watch carefully how you use it.

The method above is effective because the ="" at declaration ensures 0 bytes throughout, and sizeof(S)-1 ensures that if you overdo the quoted string passed to strncpy, you get truncation and no violation of the last 0 byte, so this is safe against overflow now, AND on accessing the string later. I aimed this at ANSI C so it ought to be safe anywhere.

-1

This works and optimizes better under gcc with -O3 (the compiler completely removes the code), whereas the memcpy forces the memory to be copied in all cases.

template <typename Array>
struct inner
{
    Array x;
};


template <typename Array>
void assign(Array& lhs, const Array& rhs)
{
    inner<Array>& l( (inner<Array>&)(lhs));
    const inner<Array>& r( (inner<Array>&)(rhs));
    l = r;
}

int main()
{
    int x[100];
    int y[100];

    assign(x, y);
}