Dynamic stack in C does not compile because of incompatible char types

Asked

Viewed 146 times

0

I’m making a theme in which I have to name documents in a dynamic stack, stack them and print out the names of documents. The document name is in a variable "char files[20]" in struct. I created the functions and the list would be working if it wasn’t for an error: "[Error] incompatible types in assignment of 'char*' to 'char [20]". This error happens in line 22 of the code that is shown here below.

I tried in several ways to try to convert the variables so that they were compatible with each other, but without success.

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

struct Pilha
{
    char arquivos[20];
    struct Pilha *prox;
};

typedef struct Pilha PILHA;
typedef PILHA *pilha;

void empilhar(pilha *topo, char arquivos[])
{
    pilha novono;
    novono=(pilha)malloc(sizeof(struct Pilha));
    if (novono == NULL)
    printf("\nNão foi possível alocar, pois não tem memoria disponivel!");
    else
    {
        novono->arquivos=arquivos; //O erro acontece aqui!
        novono->prox=*topo;
        *topo=novono;
    }
}

void mostrandodocumentos(pilha files)
{
    if (files == NULL)
    printf("A pilha esta sem arquivos!");
    else
    {
        printf("\nArquivos: \n\n");
        do
        {
            printf("%s.txt\n",files->prox);
            files=files->prox;
        }while (files!=NULL);
    }
}

int main()
{
    pilha doc=NULL;
    char nomefile[20];

    printf("Declare o nome do documento:");
    scanf("%s",&nomefile);
    empilhar(&doc, nomefile);
    mostrandodocumentos(doc);
}

Thanks in advance for the help! This is the first time I create a question here.

  • char * is a char pointer. char[20] is the twenty-first position of the char array.

  • To copy a string in C, a string with the terminator ' 0', the strcpy function of <string is used. h> and not a simple assignment (=).

  • Here: scanf("%s",&filename); does not have this & filename is already an address (the address of the first position of the array). Use scanf("%s", filename);

1 answer

0

There are several errors in the program, and the choice of names is very bad (Stack, Stack, STACK ???)

Here is a version with some fixes.

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

typedef struct Pilha
{
  char arquivos[20];
  struct Pilha* prox;
} Pilha;

static void empilhar(Pilha** topo, char arquivos[])
{
  Pilha* novo_no = malloc(sizeof(Pilha));
  if (novo_no == NULL)
    printf("\nNão foi possível alocar, pois não tem memoria disponivel!");
  else
  {
    strcpy(novo_no->arquivos, arquivos);
    novo_no->prox = *topo;
    *topo = novo_no;
  }
}

static void mostrarDocumentos(Pilha* files)
{
  if (files == NULL)
    printf("A pilha esta sem arquivos!");
  else
  {
    printf("\nArquivos: \n\n");
    do
    {
      printf("%s.txt\n",files->arquivos);
      files = files->prox;
    } while (files != NULL);
  }
}

int main(void)
{
  Pilha* doc = NULL;
  char nomefile[20];

  printf("Declare o nome do documento:");
  scanf("%s", nomefile);
  empilhar(&doc, nomefile);
  mostrarDocumentos(doc);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.