Daniel Del Core
Daniel Del Core

Reputation: 3221

C struct array assignment problems

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

Upvotes: 0

Views: 5140

Answers (8)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

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";

Upvotes: 4

Israel Unterman
Israel Unterman

Reputation: 13510

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.

Upvotes: 1

Jens
Jens

Reputation: 72639

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");

Upvotes: 3

Sachin Mhetre
Sachin Mhetre

Reputation: 4543

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

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

Upvotes: 1

Addicted
Addicted

Reputation: 1704

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.

Upvotes: 4

Williham Totland
Williham Totland

Reputation: 29009

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.

Upvotes: 1

Krishnabhadra
Krishnabhadra

Reputation: 34265

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");

Upvotes: 1

vaisakh
vaisakh

Reputation: 1031

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");

Upvotes: 3

Related Questions