简体   繁体   English

结构数组和malloc [C]

[英]array of structs and malloc [C]

I have a problem with this code. 我对此代码有疑问。 I want to code a program that has various personal info in an array. 我想编写一个在数组中具有各种个人信息的程序。 And I want 15 Arrays to be set up in one place in the memory (malloc). 我想在内存(malloc)的一个位置设置15个数组。 Also the programm should output (printf) the personal info of one person on request (angestellter[0 - 14]). 程序也应根据请求输出一个人的个人信息(printf)(angestellter [0-14])。

The Code Errors I recive are the following: 我收到的代码错误如下:

 gcc ANGDB.c ANGDB.c: In function 'print_angestellter': ANGDB.c:14:18: error: subscripted value is neither array nor pointer nor vector nu = angestellter[x].nummer; ^ ANGDB.c:15:18: error: subscripted value is neither array nor pointer nor vector vn = angestellter[x].vorname; ^ ANGDB.c:16:18: error: subscripted value is neither array nor pointer nor vector nn = angestellter[x].nachname; ^ ANGDB.c: In function 'main': ANGDB.c:25:13: error: subscripted value is neither array nor pointer nor vector angestellter[0] -> nummer = 1; ^ ANGDB.c:26:13: error: subscripted value is neither array nor pointer nor vector angestellter[0] -> vorname = "George"; ^ ANGDB.c:27:13: error: subscripted value is neither array nor pointer nor vector angestellter[0] -> nachname = "Washington"; 

This is my code: 这是我的代码:

#include <stdio.h>
#include <stdlib.h>

struct angestellter{
    int nummer;
    char vorname[50];
    char nachname[50];
}angestellter;

void print_angestellter(int x){
    int nu;
    char vn[50];
    char nn[50];
    nu = angestellter[x].nummer;
    vn = angestellter[x].vorname;
    nn = angestellter[x].nachname;
    printf("%d, %s, %s\n", nu, vn, nn);
}

int main(){
    struct angestellter **db = malloc(sizeof(angestellter)*15);
    angestellter[0] -> nummer = 1;
    angestellter[0] -> vorname = "George";
    angestellter[0] -> nachname = "Washington";
    print_angestellter(0);
}

Where you're using angestellter , which is a single instance of struct angestellter , you should be using db , which is your dynamically allocated array. 在使用angestellter (它是struct angestellter的单个实例)的struct angestellter ,应该使用db (它是动态分配的数组)。 You should also declare it as struct angestellter * instead of struct angestellter ** . 您还应该将其声明为struct angestellter *而不是struct angestellter ** This will also need to be passed to print_angestellter . 这也将需要传递给print_angestellter

You also need to use strcpy to copy strings. 您还需要使用strcpy复制字符串。 You can't directly assign a string to a character array. 您不能直接将字符串分配给字符数组。

#include <stdio.h>
#include <stdlib.h>

struct angestellter{
    int nummer;
    char vorname[50];
    char nachname[50];
};

void print_angestellter(struct angestellter *db, int x){
    int nu;
    char vn[50];
    char nn[50];
    nu = db[x].nummer;
    strcpy(vn, db[x].vorname);     // use strcpy to copy strings
    strcpy(nn, db[x].nachname);
    printf("%d, %s, %s\n", nu, vn, nn);
}

int main(){
    struct angestellter *db = malloc(sizeof(struct angestellter)*15);
    db[0].nummer = 1;
    strcpy(db[0].vorname, "George");
    strcpy(db[0].nachname, "Washington");
    print_angestellter(db, 0);
}

You seem to have two issues. 您似乎有两个问题。 First of all, in your print_angestellter() function, you use the global angestellter struct, which is perfectly fine. 首先,在您的print_angestellter()函数中,您使用了全局angestellter结构,这非常好。 However, it is a struct , not an array or pointer to memory. 但是,它是一个struct ,而不是一个数组或指向内存的指针。 Therefore you cannot use the operator [] , hence the errors. 因此,您不能使用运算符[] ,因此会出现错误。 It seems that you will have to redesign print_angestellter() or make angestellter an array of struct angestellter s. 看来您将不得不重新设计print_angestellter()或使angestellter成为struct angestellter的数组。 You also made a similar mistake in main() , doing angestellter[0] instead of db[0] . 您在main()也犯了类似的错误,做了angestellter[0]而不是db[0] To fix this, I suggest adding a pointer argument to print_angestellter() , say making it print_angestellter(int, struct *angestellter a) and replacing angestellter[0] with a[0] . 为了解决这个问题,我建议在print_angestellter()添加一个指针参数,比如说使其成为print_angestellter(int, struct *angestellter a)然后将angestellter[0]替换为a[0] You also use = to copy strings, but that copies the pointer addresses, not the contents of the string. 您还可以使用=复制字符串,但是复制指针地址, 而不是字符串的内容。 Use strcpy() instead. 请改用strcpy()

您写了“ struct angestellter ** db = malloc(sizeof(angestellter)* 15);”表示数组的指针,可以,但是您应该使用“ db [0]-> number”代替“ angestellter [0]-> nummer“;函数” void print_angestellter()“,应在使用” malloc“时添加形式参数” dp“,不要忘记使用” free“释放您申请的内存;

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

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