0

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). Also the programm should output (printf) the personal info of one person on request (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);
}
2
  • 4
    angestellter is a single variable of type struct angestellter; you cannot use subscripting on it. Commented Jul 28, 2016 at 23:34
  • 1
    It looks like you are allocating memory and assigning it to a variable named db. Shouldn't you use db instead of angestellter? Commented Jul 28, 2016 at 23:47

3 Answers 3

1

Where you're using angestellter, which is a single instance of struct angestellter, you should be using db, which is your dynamically allocated array. You should also declare it as struct angestellter * instead of struct angestellter **. This will also need to be passed to print_angestellter.

You also need to use strcpy to copy strings. 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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

print_angestellter could pass the struct members directly: printf("%d, %s, %s\n", db[x].nummer, db[x].vorname, db[x].nachname);
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. However, it is a struct, not an array or pointer to memory. Therefore you cannot use the operator [], hence the errors. It seems that you will have to redesign print_angestellter() or make angestellteran array of struct angestellters. You also made a similar mistake in main(), doing angestellter[0] instead of 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]. You also use = to copy strings, but that copies the pointer addresses, not the contents of the string. Use strcpy() instead.

Comments

0

you wrote "struct angestellter **db = malloc(sizeof(angestellter)*15);"means pointer of array,its ok,but you should use "db[0]->number" to replace "angestellter[0] -> nummer";The function "void print_angestellter()",you should add formal parameter"dp",when you use "malloc",don't forget use "free" to free the memory you apply;

1 Comment

Welcome to StackOverflow. You may want to visit How do I format my posts using Markdown or HTML? to help you format your answer so that it is more readable with the code distinguishable from the general text, etc..

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.