如何在C中的数组动态中放入不同类型的数据类型

时间:2016-10-09 01:01:17

标签: c

我尝试在动态数组中存储名称和年龄 当我们有一个不同类型的数据,int和Char,我们不知道在开始时的大小如何使用动态数组来存储2种类型

typedef struct personne{
char nom ;
int age ;

}personne;

struct personne saisie_personne_suivante(struct personne* x){


scanf("%s",&x->nom);


scanf("%d",&x->age);


return *x;
}

int main(void){


personne *ali;
ali = malloc(sizeof(char*));
saisie_personne_suivante(ali);

printf("\n %d ",ali->age);


printf("\n %s",&ali->nom);
return 0;
}

typedef struct personne{ char nom ; int age ; }personne; struct personne saisie_personne_suivante(struct personne* x){ scanf("%s",&x->nom); scanf("%d",&x->age); return *x; } int main(void){ personne *ali; ali = malloc(sizeof(char*)); saisie_personne_suivante(ali); printf("\n %d ",ali->age); printf("\n %s",&ali->nom); return 0; }

为什么我不成功?

4 个答案:

答案 0 :(得分:0)

我认为我们不能一次在数组中存储两种类型的数据。如果我们这样做,我们需要将一半内存分配给char,一半分配给整数,前提是你应该给出一些大小的数组。 =>在你的程序中这一行[ali = malloc(sizeof(char *))]你只传递char而不是变量的地址。如果你想存储两个值,只需传递int和char的地址。

答案 1 :(得分:0)

Vowels = [['a', 'a'], ['a', 'e']] Consonants = [['b', 'b', 'b'], ['b', 'b', 'c']] Vowels.product(Consonants).flat_map { |v,c| (v+c).permutation.to_a.uniq } #=> [["a", "a", "b", "b", "b"], ["a", "b", "a", "b", "b"], ["a", "b", "b", "a", "b"], ["a", "b", "b", "b", "a"], ["b", "a", "a", "b", "b"], ["b", "a", "b", "a", "b"], ["b", "a", "b", "b", "a"], ["b", "b", "a", "a", "b"], ["b", "b", "a", "b", "a"], ["b", "b", "b", "a", "a"], ["a", "a", "b", "b", "c"], ["a", "a", "b", "c", "b"], ["a", "a", "c", "b", "b"], ["a", "b", "a", "b", "c"], ["a", "b", "a", "c", "b"], ... ["c", "b", "a", "e", "b"], ["c", "b", "a", "b", "e"], ["c", "b", "e", "a", "b"], ["c", "b", "e", "b", "a"], ["c", "b", "b", "a", "e"], ["c", "b", "b", "e", "a"]] Vowels.product(Consonants).flat_map { |v,c| (v+c).permutation.to_a.uniq }.size #=> 120 是指向大小为ali的结构的指针,它可能因架构而异。

目前,让我们假设它有5个字节(可能在你的电脑上)。

您正在做的是,分配的空间大小等于sizeof(char) + sizeof(int)的大小(根据您的操作系统,宽度为32或64位)。

您可能想要做的是分配等于结构大小的空间(5个字节),即:

pointer to char

注意缺少*,因为你想要结构的实际内存而不是指向这样一个位置的指针。

顺便说一句,你也不想写:ali = malloc(sizeof(personne)); ,因为这只是你的结构所需的一个字节。

强烈建议您至少可以开始使用一本关于C 的书或者一本体面的教程。

答案 2 :(得分:0)

int main() {

personne *ali;
ali = (struct personne *)malloc(sizeof(personne));

saisie_personne_suivante(ali);

printf("\n %d ", ali->age);

printf("\n %c", ali->nom);
return 0;

}

没有足够的内存用于struct personne,因此你需要malloc sizeof(personne)内存。 nom不是指针,它是char变量,当你打印它时,使用printf("%c",ali-> nom);

答案 3 :(得分:0)

我可以同意那些推荐一本好书/教程开始的评论者,但仍然是:这是你修改过的代码,还有一些评论。

// printf(), fprintf(), and puts()
#include <stdio.h>
// exit(), malloc(), and scanf()
#include <stdlib.h>

#define PERSONNE_ERROR   0
#define PERSONNE_OK      1

typedef struct personne {
  // fixed width for 49 characters and the trailing NUL
  char nom[50];
  int age;
} personne;

int saisie_personne_suivante(struct personne *x)
{
  // For the returns of the scanf()s.
  // Because you always check the returns if available
  // (well, actually: the returns of printf() et al. rarely get checked)
  // preset it to a value meant to say "OK"
  int res = PERSONNE_OK;
  // UX: let the users know what they are supposed to do.
  puts("Your name, please");
  // we have a fixed maximum size of name and we can set it here within scanf()
  // scanf() returns the number of elements it parsed, *not* the number of characters

  // sacnf() needs a pointer to the memory it is expected to put the value into.
  // x->nom is already a pointer to a char array, no need to use "&"
  if ((res = scanf("%49s", x->nom)) != 1) {
    // we can return immediatly here.
    // If we would need to cleanup (free memory, for example) we would
    // set res to PERSONNE_ERROR and use a goto to jump at the place
    // where all the cleanup happens. But that should be done if the clean-up
    // is always the same (or could be sorted) and you need such cleanups
    // more than just two or three times.
    return PERSONNE_ERROR;
  }

  puts("Your age, too, if you don't mind.");
  // x->age is not a pointer to an int, hence we need to prefix "&"
  if ((res = scanf("%d", &x->age)) != 1) {
    return PERSONNE_ERROR;
  }
  return res;
}

int main(void)
{
  personne *ali;
  int res;

  // reserve momory for the struct
  ali = malloc(sizeof(personne));
  // call function that fills the struct and check the return
  if ((res = saisie_personne_suivante(ali)) != PERSONNE_OK) {
    fprintf(stderr, "Something went wrong with saisie_personne_suivante()\n");
    exit(EXIT_FAILURE);
  }
  // print the content of struct personne
  // you can feed printf() directly, no need to find the pointer to the memory
  // holding the int
  printf("Age:  %d\n", ali->age);
  // To print strings it needs to know the start of the string whcih needs to be
  // a pointer and ali->nom is a pointer to the start of the string
  printf("Name: %s\n", ali->nom);
  // free allocated memory (not really necessary at the end of the
  // program but it's deemed good style and because it costs us nothing
  // we cannot find a good reason to skip it)
  free(ali);
  // exit with a value that tells the OS that this programm ended without an error
  // It shoudl be 0 (zero) which it almost always is.
  // *Almost* always
  exit(EXIT_SUCCESS);
}

但是真的:去拿一些初学者的书/教程。我不能给你一个推荐,因为我不会用你的母语知道任何好的(有时英文版很好,但翻译缺乏很多)。