0

I'd like to know the different ways for initializing an Array in C, because i'm having a little problem when trying to do something...

Im starting programming in C in college, its the first language, and we are viewing pointers and some functions.

That said, I was trying the following:

#include <stdio.h>
int main () {
    char a[][10] = {"Hola","Chau"};
    char b[][10] = {*a[0],*a[1]};
    printf("Contenido de &a es: %s \n",&a);
    printf("Contenido de &b es: %s \n",&b);
    system("pause");
}

Which is copying what is inside a[0][0 ... 10] but wont work... at least in MS VS 2010 that is what we use at college.

So I'd like to know a link to a reference of the different ways possibles for initializing an array.

Searching here in stack, I've found the memcpy function and this work this way.

#include <stdio.h>
int main () {
    char a[5] = {"Hola"};
    char b[5];
    memcpy(b,a,3); /* testing */
    b[3] = '\0'; /* testing */
    printf("Contenido de &a es: %s \n",&a);
    printf("Contenido de &b es: %s \n",&b);
    system("pause");
}
JorgeeFG
  • 5,651
  • 12
  • 59
  • 92

1 Answers1

0

You should distinguish array assignment(e.g., memcpy, or use while/for loop to assign elements in array one-by-one, whatever) with array initialization. Array can only be initialized in its definition line.

Check out here for two ways to specify initializers for arrays: initialization of arrays.

Eric Z
  • 14,327
  • 7
  • 45
  • 69
  • Hmmm not sure if you understood me, I'd like to initialize an array with a value from other array..... – JorgeeFG Apr 16 '12 at 20:53