views:

84

answers:

2

paxdiablo gave the previous answer for it working with char array.. can I know how to work with int array for the same below code?

LIke:

struct encode {
    int code[MAX]; //instead char code[MAX]
} a[10];

int main() {
    int i, j;
    int x[] = {3,0,2,5,9,3,1};
    //instead char x[] = {'3','0','2','5','9','3','1','\0'};
    for(i = 0; i < 1; i++) {
        for(j = 0; j < 7; j++) {
            printf("%d", x[j]);
        }
        printf("\n");

        a[0].code=x;
        //strcpy(a[0].code, x); for char

    }
    printf("%d\n",a[0].code);
    //printf("%s\n",a[0].code); for char
    return 0;
}

Like show can this be done for int array? Thanks in advance.

+1  A: 

Refer answer from your previous Question http://stackoverflow.com/questions/1911401/question-on-struct-with-char-array

  • Use memcpy to copy the integer array elements.
  • use %d in case you want to print integers.
aJ
I only asked the previous question in the link.. memcpy with %d prints out the memory and not the inter array. Is there anything I am missing.?
meg
Yes. Thats why I asked to refer the answer and use memcpy instead of strcpy for copying integers.
aJ
A: 

Some questions for you, all the snippets below are from your code:

  1. In the following, what's the type of x[j]. What type does %c expect?

    printf("%c", x[j]);

  2. What's the type of a[0].code below? It's an array, with a predefined memory allocated to it. What to you expect the above statement to do?

    a[0].code=x;
  3. Again, what is the type of a[0].code? What does %d format expect?

    printf("%d\n",a[0].code);

The answers to all the questions above are easy, or should be available in any text book.

Programming doesn't work by trial-and-error. You should find a good reference and a good textbook, and think about what you're doing.

As a first step, please answer the questions I have asked above, and hopefully you will be able to fix most of your mistakes yourself.

Alok
Thanks Alok. It my mistake in "%d " and not "%c" gives "incompatible types in assignment". I used memcpy(encd[k].code, c, sizeof c); but the result value for printf("%d",a[0].code); give 8731424 which is memory and not "3025931" which I am expecting. i worked with char[] which gives the required result.. but when int array doesnt give the same result. This is what i need int x[]={3,2,4,5,6} store it in struct and display back like "32456". Thanks I will change the original snippet which is cofusing.
meg
`a[0].code` is not `int` because you declared it as an *array of `int`s*! Please try to answer the questions based on your code, not based upon what you want them to be.
Alok