String compilation problem in C

Asked

Viewed 100 times

1

I’m trying to compile this code into 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:

trab3.c:6attribute' before ː=' token char name[50]="A";

  • Initialize the struct on main... Try to see if this is it!

  • Better explain the purpose of the code

  • i have to create this Tipoaluno and record several students of this type in a file, only that the fields name and course for example will not be used for search and search, so the teacher gave as dirt put any character in the variable.

  • Try to see the code I posted... If your teacher sent it by any character... I recommend you do it in the main and not within the struct.

2 answers

4


To do it the way you want it is this way:

#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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

You can’t initialize the structure itself, and it doesn’t make sense.

It is true that it is not often done so in real application. You are wasting memory. Not that it’s wrong, actually there are cases that are better this way because it avoids unnecessary memory allocation and release, which is something that costs expensive and can unintentionally generate errors. There are those who prefer to do so in some cases. For exercise it is good to start doing 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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Paragraph 2 in malloc() is because the string which will be used is 1 in 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, has a lot of care to use correctly, including when you decide to change the value of string. By the way, it should be constant, but I’m not going to try to solve all the questions, there’s no reason to select one to solve. This works, and is strictly correct, but is not the ideal form for real code in production.

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

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

-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 creating a struct with assignments in variables, think that structs are like classes you can use more than once... This is an example you can make.

  • This example will write in unallocated memory because aux2 points to garbage.

  • Well remembered!!! @Marcelouchimura corrected!

  • There is much more problem than this, see my answer. And it is still wrong, in fact it may have introduced new errors.

Browser other questions tagged

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