好吧所以我的目标是使用该函数来填充字符串数组,然后它将返回那些字符串数组:
#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");
}
答案 0 :(得分: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]);
}
}
答案 1 :(得分: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");
}
与您所做的相比,并尝试从中学习。
答案 2 :(得分:0)
一些事情:
你正在阅读char
而不是char*
到gende,所以你必须在参数列表中解决这个问题。
对于name
和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");
}