结构数组和malloc [C]

时间:2016-07-28 23:26:37

标签: c arrays pointers struct

我的代码有问题。我想编写一个在数组中包含各种个人信息的程序。我希望在内存(malloc)中的一个位置设置15个数组。 程序员还应根据要求输出(printf)一个人的个人信息(angestellter [0 - 14])。

我所了解的代码错误如下:

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

这是我的代码:

#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);
}

3 个答案:

答案 0 :(得分:1)

您使用的angestellterstruct angestellter的单个实例,您应该使用db,这是您动态分配的数组。您还应将其声明为struct angestellter *而不是struct angestellter **。这也需要传递给print_angestellter

您还需要使用strcpy复制字符串。您不能直接将字符串分配给字符数组。

#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);
}

答案 1 :(得分:0)

您似乎有两个问题。首先,在print_angestellter()函数中,使用全局angestellter结构,这非常好。但是,它是 struct ,而不是指向内存的数组或指针。因此,您无法使用运算符[],因此错误。您似乎必须重新设计print_angestellter()或使angestellter成为struct angestellter的数组。您在main()中也犯了类似错误,执行angestellter[0]而不是db[0]。为了解决这个问题,我建议为print_angestellter()添加一个指针参数,比如说print_angestellter(int, struct *angestellter a)并用angestellter[0]替换a[0]。 您还使用=复制字符串,但复制指针地址,字符串的内容。请改用strcpy()

答案 2 :(得分:0)

你写了“struct angestellter ** db = malloc(sizeof(angestellter)* 15);”表示数组的指针,没关系,但是你应该使用“db [0] - &gt; number”来代替“angestellter [ 0] - &gt; nummer“;函数”void print_angestellter()“,你应该添加形式参数”dp“,当你使用”malloc“时,不要忘记使用”free“来释放你应用的内存;