0

I'm new to C and I am working with a struct array. I'm having trouble assigning values to it. here is my struct..

struct student{
        char firstname[30];
        char surname[30];
        int streetNo;
        char streetName[30];
        char suburb[30];
        char state[4];
        int postCode;
        char DOB[10];
        int studentNo;
        char gender;
        char courseNo[4];
        char active;
        int WAM;
    };
struct student person[1000];

here is me assigning a value to the struct

person[100].firstname = "dan";

and this is my error

 assignment type mismatch: array[30] of char "=" pointer to char

8 Answers 8

4

You can only initialize array like that at the time of declaration only, else you need to use

strcpy(person[100].firstname,"dan");

you can't even do like that with a simple char array

char a[30];
a="dan";

the complier will tell :

incompatible types when assigning to type ‘char[30]’ from type ‘char *’

because "dan" is a string literal that is being held by a pointer which can't be assigned like this to an array.

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

3 Comments

You can also go like this... `struct student { char *firstName; }person[100]; person[30].firstName = "dan";'
Not good. This will run into major problems as soon as you try to change student.firstname.
You can only initialize array like that at the time of declaration only - Nice catch!
4

Although you can initialize an array of characters from a string literal like this

char str[] = "dan";

you cannot assign a string literal to an array of characters the way you are trying to do.

You need to copy your string into the character array using one of the string copy functions:

strcpy(person[100].firstname, "dan");

If yo would like to copy "dan" into the first four elements and pad the remaining elements of firstname with zeros, use strncpy:

strncpy(person[100].firstname, "dan", 30);

It is worth pointing out that you could make firstname a pointer, and either allocate memory for your strings dynamically, or assign it directly:

 struct student{
    char *firstname;
    char *surname;
    /* and so on... */
 };

 student[100].firstname = "dan";
 student[100].surname = "brown";

Comments

3

In C, a string like this is implemented using a character array. A character array is exactly what you have defined, but it is not possible to assign a string directly like this in C. You will have to use string functions for that. The function to use is strcpy(). You have to assign like:-

strcpy(person[100].firstname, "dan");

1 Comment

thank you that worked great! i totally forgot about strcpy ahahahaha
3

Arrays are not pointers, and this is one example of it. Arrays cannot be assigned to, only array elements can be assigned to. You could do

 person[100].firstname[0] = 'd';
 person[100].firstname[1] = 'a';
 person[100].firstname[2] = 'n';
 person[100].firstname[3] = '\0'; /* Pretty tedious... */

or, if you know that you don't copy more than 30 bytes,

 strcpy (person[100].firstname, "dan");

Comments

1

An array name itself gives array base address.. And an array base address cannot be a left side value.

  person[100].firstname  = 

gives you error since you are assigning some other value to array base address which is not allowed.

You can initialize

char stringArray[]  = "some string";

but you can't assign value to already declared array

  char stringArray[100];
  stringArray     =   "some string";  <== error

You alternative is to use strcpy

 strcpy(stringArray, "sometext");

Comments

1

The specific problem is that you are attempting to assign a string to an array of bytes.

What you need to do in that particular case is to copy the contents of the string you want into the array, like so:

strncpy(person[100].firstname, "dan", 30);

A more general problem is what you are doing is terrible. For student records like this, the only sensible thing to do is to use a proper database; in your case using SQLite is probably appropriate. Using a database for a simple learning exercise like so might seem like overkill, but it is experience that'll help you out a lot later.

Comments

1

You can strcpy() for assigning value to char array.

strcpy(person[100].firstname,"Dan");

Comments

1

You can't assign like this to an array. Even without a struct. That is

char name[10];
name = "ert";

is an error.

(You can do it only in initialization char name[10] = "ert";)

The correct way to do it is

strcpy(person[100].firstname, "dan");

Safer to use a variation of strcpy that requires a max size of string.

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.