简体   繁体   English

C结构数组分配问题

[英]C struct array assignment problems

I'm new to C and I am working with a struct array. 我是C语言的新手,正在使用struct数组。 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

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 : 如果您想将"dan"复制到前四个元素中,并用零填充firstname的其余元素,请使用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: 值得指出的是,您可以将firstname用作指针,然后为字符串动态分配内存,或者直接分配它:

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

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

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数组那样做

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. 因为“ dan”是一个字符串文字,由指针持有,因此无法将其分配给数组。

In C, a string like this is implemented using a character array. 在C语言中,这样的字符串是使用字符数组实现的。 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. 字符数组正是您所定义的,但是在C语言中不能像这样直接分配字符串。为此,您将必须使用字符串函数。 The function to use is strcpy() . 使用的函数是strcpy() You have to assign like:- 您必须指定:-

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

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, 或者,如果您知道复制的字节数不超过30个字节,

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

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. 在您的情况下,使用SQLite可能是合适的。 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. 像这样使用数据库进行简单的学习练习似乎有点过头了,但是经验会在以后为您提供很多帮助。

您可以使用strcpy()将值分配给char数组。

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

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"; ) (您只能在初始化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. 更安全地使用要求最大字符串大小的strcpy变体。

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

 strcpy(stringArray, "sometext");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM