views:

103

answers:

4

Below is my code snippet

struct encode
{

   char code[MAX];

}a[10];

int main()
{ 
char x[]={'3','0','2','5','9','3','1'};

  for(i=0;i<1;i++)
  {

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

//This will printout like 3025931 now I want this to be stored in structure.         
  }

strcpy(a[0].code,x); 
// or 
a[0].code=x;//neither works

display();

}

void display()
{
printf("%c",a[0].code);
}

I want the output to be like:3025931.

Which I am not getting due to incompatible assign type. Please tell me where am i going wrong.

+2  A: 

I see two problems here. The first is that the source of the strcpy is a where it probably should be x.

The second is that x is not null-terminated. Strings in C are null-terminated character arrays.

I would change the two lines:

char x[] = {'3','0','2','5','9','3','1'};
strcpy(a[0].code, a);

to:

char x[] = {'3','0','2','5','9','3','1', '\0'};
strcpy(a[0].code, x);

Here's a complete program that gives you what you want (it actually prints out the number twice, once in your inner loop character by character and once with the printf so that you can see they're the same):

#include <stdio.h>
#include <string.h>
#define MAX 100
struct encode {
    char code[MAX];
} a[10];

int main() {
    int i, j;
    char x[] = {'3','0','2','5','9','3','1','\0'};

    for(i = 0; i < 1; i++) {
        for(j = 0; j < 7; j++) {
            printf("%c", x[j]);
        }
        printf("\n");

        strcpy(a[0].code, x);
    }
    printf("%s\n",a[0].code);
    return 0;
}

Update based on comment:

I am sorry. I am new to C. My apologies for not pasting the code snippet correctly in the beginning: "printf("%c",a[0].code);" doesn't display "3025931".

No, it won't. That's because a[0].code is a character array (string in this case) and you should be using "%s", not "%c". Changing the format specifier in the printf should fix that particular issue.

paxdiablo
...and of course that new intialisation of `x` could be equivalently written as `char x[] = "3025931";`
caf
I am sorry I am new to 'c' my apologise for not pasting the code snippet correctly in the begining..void display(){printf("%c",a[0].code);} doesnt display "3025931"
meg
paxdiablo thanks for your answers.. what if i am dealing with int array like struct encode { int code[MAX];} a[10];int x[]={'3','0','2','5','9','3','1'}; should I use strcpy after converting from int[] to char[]?
meg
Then you cannot treat the array as a string, you must process the int elements one-by-one. I'm not sure exactly *what* you're trying to achieve. It may be better to ask a separate question on how to do it (with no preconceived notions) rather than how to modify this existing code.
paxdiablo
A: 

Here,

strcpy(a[0].code, a);

did you mean

strcpy(a[0].code, x);

...?

Also, x needs to be null terminated, or you need to replace strcpy with strncpy or memcpy and pass in a length.

moonshadow
A: 

This line doesn't make much sense:

   strcpy(a[0].code, a);

Perhaps you want this:

    memcpy(a[0].code, x, sizeof x);
    a[0].code[sizeof x] = '\0';

(The second line is necessary to nul-terminate code, making it a proper C string).

caf
A: 

A lot of things are wrong in your program. The most offending line is this:

strcpy(a[0].code, a);

but there are other oddities as well, e.g.

  • display is never called
  • a is only assigned (kind of), but never read (except in display, which is never called)
  • the i loop makes no sense

Basically, this program looks like copy-pasted by someone who has no clue.

ammoQ
"Basically, this program looks like copy-pasted by someone who has no clue." Is that necessary?
ChaosPandion
ChaosPandion: a fact is a fact
ammoQ
BTW, the OP has been heavily edited, it was worse in the beginning
ammoQ