0

好的,所以我的目标是使用该函数来填充字符串数组,然后它将返回这些字符串数组:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int getinfo (char* nam[], int ag[], char* dot[], char* gende[], int x)
{
    printf ("What is the student's name?\t");
    scanf ("%d", &nam[x]);
    printf ("\nWhat is the student's age?\t");
    scanf ("%d", &ag[x]);
    printf ("\nWhat is the student's Date of Birth?\t");
    scanf ("%s", &dot[x]);
    printf ("\nWhat is the student's gender?\t");
    scanf ("%c", &gende[x]);
    printf ("\nWhat is the student's adress?\t");
    return nam[x];
}

int main ()
{
    int amount, y;
    printf("How many students are you admitting?\t");
    scanf ("%d", &amount);
    char *name[50], *dob[50], gender[50];
    int age[50];

    for(y = 0; y < amount; y++)
    {
        getinfo(&name[y], &age[y], &dob[y], &gender[y],y);
    }
    system("pause");
}
4

3 回答 3

1

这只会初始化名称数组,以便您更好地理解发生了什么。缺少的最基本的事情是您必须为字符串数组中的每个字符串分配空间。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void getinfo (char* nam[],int count){
  int y;
  for(y = 0; y < count; y++){
    nam[y] = malloc(50);
    printf ("What is the student's name?\t");
    scanf ("%s", nam[y]);
  }
}

int main (){
  int amount, y;
  printf("How many students are you admitting?\t");
  scanf ("%d", &amount);
  char *name[50];
  getinfo(name,amount);
  for(y = 0; y < amount; y++){
    printf("%s\n",name[y]);
  }
}
于 2013-03-09T02:12:30.427 回答
0

一些东西:

  • 您正在阅读chars 而不是char*s 性别,因此您必须在参数列表中修复它。

  • 对于nameand dot,您只是声明了一个包含指向字符串的指针的数组。您还需要分配内存来存储字符串。在下面的代码中,为每个字符串分配了 50 个字符。

我还建议不要使用scanf来读取字符串,因为下面的代码容易受到缓冲区溢出的影响。没有什么可以阻止用户输入超过 50 个字符。见http://c-faq.com/stdio/scanfprobs.html

无论如何,这里有一些改动很小的代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int getinfo (char* nam[], int ag[], char* dot[], char gende[], int x)
{
    printf ("What is the student's name?\t");
    scanf ("%d", &nam[x]);
    printf ("\nWhat is the student's age?\t");
    scanf ("%d", &ag[x]);
    printf ("\nWhat is the student's Date of Birth?\t");
    scanf ("%s", &dot[x]);
    printf ("\nWhat is the student's gender?\t");
    scanf ("%c", &gende[x]);
    printf ("\nWhat is the student's adress?\t");
    return nam[x];
}

int main ()
{
    int amount, y;
    printf("How many students are you admitting?\t");
    scanf ("%d", &amount);
    char *name[50], *dob[50], gender[50];
    int age[50];

    for(y = 0; y < amount; y++)
    {
        name[y] = malloc(50);
        dob[y] = malloc(50);
    }

    for(y = 0; y < amount; y++)
    {
        getinfo(&name[y], &age[y], &dob[y], &gender[y],y);
    }
    system("pause");
}
于 2013-03-09T02:44:14.747 回答
0

尝试这个:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef char string[50];

void getinfo(char *nam, int *ag, char *dot, char *gende){
    printf ("What is the student's name?\t");
    scanf("%s", nam);

    printf ("\nWhat is the student's age?\t");
    scanf("%d", ag);

    printf ("\nWhat is the student's Date of Birth?\t");
    scanf("%s", dot);

    printf ("\nWhat is the student's gender?\t");
    scanf("%s", gende);

}

int main(){

    int
        amount,
        *age,
        y;

    string
        *name,
        *dob,
        *gender;

    printf("How many students are you admitting?\t");
    scanf("%d", &amount);

    name    = (string *)malloc(sizeof(string));
    dob     = (string *)malloc(sizeof(string));
    gender  = (string *)malloc(sizeof(string));
    age     = (int *)malloc(sizeof(int));

    for(y = 0; y < amount; ++y)
        getinfo(name[y], &age[y], dob[y], gender[y]);

    system("pause");

}

比较你所做的并尝试从中学习。

于 2013-03-09T02:05:05.093 回答