0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct data{
char name[20];
char lastname[25];
int age;
}person;
void insert(person *p,int *num);
int main()
{
int num;
person p;
insert(&p,&num);
printf("Name: %s",p[0].nome); /* Here i would print the first struct by
my array, but: is not array or not pointer Why?? */
}
void insert(person *p, int *num)
{
int dim;
person *arr;
printf("Insert how many people do you want? "); /* How many index the
array should have */
scanf("%d",&dim);
arr = (person *) malloc(dim*sizeof(person)); /* I'm not sure for
this explicit cast. */
for(int i = 0; i < dim; i++)
{
printf("Insert name: ");
scanf("%s",arr[i].name);
printf("Insert lastname: ");
scanf("%s",arr[i].lastname);
printf("Insert age:': ");
scanf("%d",&arr[i].age);
}
*num = dim;
*p = *arr;
}
我已經試過:'人*插入(INT * NUM)如何通過引用返回一個結構數組?
和它的作品,但怎麼能傳遞一個數組引用`
這PROGRAMM應該問他有多少人你要不要?插入(在函數中插入)和用for,他應該問名字,姓氏,年齡。
插入後,他應該打印,但爲了快速,我會嘗試數組(結構)的第一個元素(索引)。
C沒有引用或傳遞通過引用。所有函數參數都是按值傳遞的,只能返回值。但是,您可以傳遞指針(按值)或返回指針,這些指針具有相似的效果。 –