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.
– anonimo
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 (=).
– anonimo
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);
– anonimo