1

I have this list and I'm writing a function to ask the user to add the info:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTAB 40
#define MAXSTR 25

typedef enum {Prob, Impr, Terr, Jail} Type;

typedef struct {
    int pos;
    char name[MAXSTR+1];
    char owner[MAXSTR+1];
    int price;
    Type info;
} Casella;

struct lista {
    Casella Tab;
    struct lista *node;
};

typedef struct lista Lista;

void printElement (Lista *);
void printTab (Lista *);
Lista * initializeTab (Lista *, int);
Lista * addInfo (Lista*);

int main()
{
    Lista *list = NULL;
    int i;

    for (i=0; i<MAXTAB; i++){
        list = initializeTab(list, i);
    }

    printTab(list);

    return 0;
}

void printElement (Lista *l){

    printf("Position: %d\n", l->Tab.pos);

}

void printTab (Lista *l){

    while(l!=NULL){
        printElement(l);
        l=l->node;
    }

}

Lista * initializeTab (Lista *l, int x){

    Lista *newTab = NULL;

    newTab = (Lista*)malloc(sizeof(Lista));

    newTab->Tab.pos = x;
    newTab->node = l;

    return newTab;

}

Lista * addInfo (Lista *l){
    Lista *list = NULL;
    list->Tab.name = (char*)malloc(MAXSTR * sizeof(char));
    return list;
}`

In the function "addInfo", I try to allocate memory for the Tab name, but it tells me I can't assign type array char to that. My question is, how do I allocate memory for various list elements? Like I want to allocate memory for the list.name, then list.owner, list.pos etc... and then assign values to them.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Simone
  • 129
  • 1
  • 5

2 Answers2

3

In your structure definition

struct {
    int pos;
    char name[MAXSTR+1];   //array
    char owner[MAXSTR+1];  //array
    int price;
    Type info;
} Casella;

name and owner are arrays, and they already have [MAXSTR+1] elements each. You don't need to allocate memory using malloc(). Just do strcpy().

Also, pos being an integer, simply assign the value using =. That should suffice.

Where you need to allocated memory is to list, which is a pointer. Something like

 Lista *list = malloc(sizeof*list);  //and a NULL check later

is required in first place.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Arrays in are non-writeable lvalues, to achieve what you want you need to use a pointer instead, change the structure definition to this

typedef struct {
    int pos;
    char *name;
    char *owner;
    int price;
    Type info;
} Casella;

Also, you malloc() and the pointer points to valid uninitialized memory so if you try to read from it undefined behavior will happen, try to change the addInfo() function to something like this

Lista *
addInfo (Lista *lista, const char *const name)
{
    lista->Tab.name = strdup(name);
    return lista;
}

Another very important mistake is that you set list to NULL inside addInfo() and then immediately dereference it, that is surely undefined behavior and the most likely outcome is that your program will crash.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97