Problem in compiling string in C

0

I'm trying to compile this code in C:

#include<stdio.h>
#include<string.h>
#define tam 100

typedef struct TipoAluno{
    char nome[50]="A";
    char curso[50]="B";
    char cota[50]="C";
    int matricula;
    int coef;
}TipoAluno;

void GeraArquivo(FILE *arq, TipoAluno vet[tam]){
    int i;
    for (i = 0; i < tam; ++i){
        /* code */
    }
}

int main(int argc, char const *argv[]){
    FILE *f;
    f=fopen("CadatrosAlunos.txt","a");
    return 0;
}

But the compiler returns the error:

  

attribute 'before' = 'token char name [50]="A";

    
asked by anonymous 02.06.2018 / 18:06

2 answers

3

To do it the way you want it to be like this:

#include<stdio.h>
#define tam 100

typedef struct {
    char nome[51];
    char curso[51];
    char cota[51];
    int matricula;
    int coef;
} TipoAluno;

int main() {
    TipoAluno aluno = {
        .nome = "A",
        .curso = "B",
        .cota = "C",
        .matricula = 1
    };
    printf("%s", aluno.nome);
}

See running on ideone .

You can not initialize the structure in itself, and it does not even make sense.

It is true that this is not usually done in real application. You're wasting memory. Not that it is wrong, in fact it has cases that is best because it avoids allocation and release of unnecessary memory, which is something that is expensive and can inadvertently generate errors. There are those who prefer to do so in some cases. For exercise it is a good start to do so, but the most common is to allocate the string separately according to the required size.

Then you could do:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define tam 100

typedef struct {
    char *nome;
    char *curso;
    char *cota;
    int matricula;
    int coef;
} TipoAluno;

int main() {
    TipoAluno aluno = {
        .nome = malloc(2),
        .curso = malloc(2),
        .cota = malloc(2),
        .matricula = 1
    };
    strcpy(aluno.nome, "A");
    strcpy(aluno.curso, "B");
    strcpy(aluno.cota, "C");
    printf("%s", aluno.nome);
}

See working on ideone .

The 2 in malloc() is because the string that will be used has 1 size (needs one more for the terminator). It would not make sense to allocate more than needed.

Note that this is a very naive implementation, it takes a lot of care to use it correctly, including when it resolves to change the string value. Alias, it should be constant, but I will not try to solve all the questions, you do not have to select any to solve. This works, and it's strictly correct, but it's not the ideal way to produce real code.

Note also that I did not give free() to rent some, this makes leaking memory. For a simple case like this, no problem, but in a real program would need to do at the right time and place, which is not easy to manage and depends on a number of factors.

In fact, the ideal even there may be to use union , but it is too advanced and depends on requirements, not something for an exercise.

    
02.06.2018 / 18:45
-1
#include<stdio.h>
#include<string.h>
#define tam 100

typedef struct TipoAluno{
    char *nome;
    char *curso;
    char *cota;
    int matricula;
    int coef;
}TipoAluno;

void GeraArquivo(FILE *arq, TipoAluno vet[tam]){
    int i;
    for (i = 0; i < tam; ++i){
        /* code */
    }
}

int main(){
    FILE *f;
    char *aux;
    TipoAluno *aux2;
    aux2=(*TipoAluno)malloc(sizeof(TipoAluno));
    f=fopen("CadatrosAlunos.txt","a");
    if(f==NULL)
    {
    printf("Ocorreu um erro!");
    }else
    {
    printf("\n escreva uma string:");
    scanf("%s",aux);
    printf("\n Digite o nome do aluno:");
      scanf("%s",aux2->nome);
    fprintf(f,"%s",aux);
    fprintf(f,"%s",aux2);
    printf("Essa palavra foi escrita no arquivo com sucesso!");
    }
    //poderia usar o tipo rw que é melhor 
    return 0;
}

The first big mistake you made was to create a struct with assignments in the variables, think that structs are like classes you can use more than once ... This is an example you can do.

    
02.06.2018 / 19:01