2

I am working on a project that requires that I make an array of a certain structure type. The structure looks like this:

typedef struct
{
char array[30];
int num;
}structure

I have declared an array of these structures like this:

structure struct1[5];

I passed this structure array to a function which fills the first four elements with a string and a number. The fifth is left blank. Later in the program, I pass the array again and try to set the string in the structure to a user determined string using gets(). I am getting this error:

438:19: error: incompatible types when assigning to type 'char[30]' from type 'char *'

If I need to provide more clarification, please tell me.

Thanks!

EDIT: Here is what I am doing:

typedef struct
{
char array[30];
int num;
}structure;

void fillStructs(structure[]);
void userEditStructs(structure[]);

main()
{
structure myStructs[5];
fillStructs(myStructs);
userEditStructs(myStructs);
return 0;
}

void fillStructs(structure s[])
{
    //code to fill myStructs elements 0-3.
}

void userEditStructs(structure s[])
{
char newArr[30];
int newNum;
printf("Please enter your string:\n\nEntry:\t");
gets(newArr);
printf("Please enter your number:\n\nEntry:\t");
scanf("%i", newNum);
s[5].array = newArr;
s[5].num = newNum;
}
3
  • 3
    Please add the code where you're passing the array of structures to the function. Commented Nov 20, 2013 at 17:23
  • use strcpy and not operator = Commented Nov 20, 2013 at 17:25
  • Never ever use gets(). It is dangerous, it is deprecated, it is removed from C++ and should be removed from C too. Commented Nov 20, 2013 at 17:33

2 Answers 2

3

you are doing something like this

char a[20];
a = "bla";

you cant do this.

do strcpy(a,"bla"); instead. ( #include <string.h> )

Sign up to request clarification or add additional context in comments.

Comments

0

Without looking at the code you are probably trying to do something like:

struct[4].array = gets(<unknown>);

which won't work, as you can't assign the returned char* from gets to an array as the compiler says. You are also using gets, which is strongly discouraged as it performs no bounds checking. Instead, do the following:

fgets(struct[4].array, sizeof(struct[4].array), stdin);

which will do proper bounds checking.

Comments

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.