首页 > 解决方案 > 错误:在 '.' 之前应为 ';'、',' 或 ')' 令牌?

问题描述

我正在用 C 语言编写一个带有结构和指针等的程序。但是,当我运行它时,它会给出这个错误:“error: expected ';', ',' or ')' before '.' 令牌”

在以下行

char *strcpy(char *account[i].nome, const char *nomi[p]);

基本上我想做的是,将从 char nomi[p] 中获取的随机名称赋值给 account.nome[i] 变量,其中 p 是 0 到 4 之间的随机索引,其中 [i] 在 for 循环中被清除。

如您所见,我评论了这一行

//account[i].nome = nomi[p];

否则它会给我这个错误->错误:赋值给具有数组类型的表达式

有什么提示吗?

#include <stdio.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h> // includo la libreria per gestire le funzioni sui caratteri

#define MAX_ACCOUNT 5
#define MAX_NOME 20
#define MAX_COGNOME 20
#define MAX_EMAIL 40

typedef struct {
  int giorno;
  int mese;
  int anno;
} data;

typedef struct {
  char nome[MAX_NOME];
  char cognome[MAX_COGNOME];
  data datadinascita;
  char email[MAX_EMAIL];
  char password;
} acc;

int main(void) {

  int seed = time(NULL); //     Randomizzo
  srand(seed);           //     time
  unsigned i = 0;
  unsigned p = 0;
  unsigned q = 0;
  unsigned r = 0;
  char* nomi[4] = {"Gianmarco","Francesco","Michele","Marco","Roberto"};
  char* cognomi[4] = {"Lorusso","Simone","Caggiano","Moramarco","Colonna"};
  char* email[4] = {"rymmysice-2084@gmail.com","junetome-4060@hotmail.com","ikijaza-9272@live.it","hokalife-2155@libero.it","ottejotto-2395@gmail.com"};

  acc account[MAX_ACCOUNT] = {0};
  data datadinascita[MAX_ACCOUNT]; //variabile datadinascita


  //Ciclo di lettura
  for(i = 0; i < MAX_ACCOUNT; i++) {

    p = rand() % (4-0+1) + 0; //Max 4, Min 0
    q = rand() % (4-0+1) + 0; //Max 4, Min 0
    r = rand() % (4-0+1) + 0; //Max 4, Min 0

    char *strcpy(char *account[i].nome, const char *nomi[p]);
    //account[i].nome = nomi[p];
    //account[i].cognome = *cognomi[q];
    datadinascita[i].giorno = rand() % (31-1+1) + 1; //Max 31, Min 1
    datadinascita[i].mese = rand() % (12-1+1) + 1; //Max 12, Min 1
    datadinascita[i].anno = rand() % (2003-1960+1) + 1960; //Max 2003, Min 1960
    //account[i].email = *email[r];

    printf("ACCOUNT #%d: ", i+1);
    printf("Prova");
    printf("\n\tNome: %19s", account[i].nome);
    printf("\n\tCognome: %19s", account[i].cognome);
    printf("\n\tData di nascita: %d-%d-%d", datadinascita[i].giorno, datadinascita[i].mese, datadinascita[i].anno);
    printf("\n\tEmail: %39s", account[i].email);
    printf("\n");

  }

  return 0;
}

标签: cpointersstruct

解决方案


应该是这样的

strcpy(account[i].nome, nomi[p]);

推荐阅读