简体   繁体   中英

How to fill the array in the main function?

I found online the following program using struct and functions.

We call a function that fills the array. Inside the function there is the for-loop that inserts the elements that the user gives.

#include <stdio.h>
    
// student structure
struct student {
  char id[15];
  char firstname[64];
  char lastname[64];
  float points;
};

// function declaration
void getDetail(struct student *);

int main(void) {
  
  // student structure variable
  struct student std[3];
  
  // get student detail
  getDetail(std);
  
  return 0;
}

// function definition


void getDetail(struct student *ptr) {

  int i;

  for (i = 0; i < 3; i++) {
    printf("Enter detail of student #%d\n", (i + 1));
    printf("Enter ID: ");
    scanf("%s", ptr->id);
    printf("Enter first name: ");
    scanf("%s", ptr->firstname);
    printf("Enter last name: ");
    scanf("%s", ptr->lastname);
    printf("Enter Points: ");
    scanf("%f", &ptr->points);
    
    // update pointer to point at next element
    // of the array std
    ptr++;
  }

}  
} 

Is it possible to call the function "getDetail" repetitively in the main to fill the array instead of having the loop inside the function? I mean to have the for-loop in the main instead of inside the function?

I tried the following:

#include <stdio.h>
    
// student structure
struct student {
  char id[15];
  char firstname[64];
  char lastname[64];
  float points;
};

// function declaration
void getDetail(struct student *);

int main(void) {
  
  // student structure variable
  struct student std[3];
  
  // get student detail 
for (i = 0; i < 3; i++) { 
printf("Enter detail of student #%d\n", (i + 1));
  getDetail(std);
}
  
  return 0;
}

// function definition


void getDetail(struct student *ptr) {

    printf("Enter ID: ");
    scanf("%s", ptr->id);
    printf("Enter first name: ");
    scanf("%s", ptr->firstname);
    printf("Enter last name: ");
    scanf("%s", ptr->lastname);
    printf("Enter Points: ");
    scanf("%f", &ptr->points);
    
    // update pointer to point at next element
    // of the array std
    ptr++;

}

But it seems that this doesn't work. What do I have to change?


In the "getDetail" function why when the input is an array we do not use the symbol "&" and when the input is not array we use it?

So why we use only "&" before ptr->points?

Which is the difference?

ptr++; in the getDetail function will update the local variable ptr and it will vanish on returning from the function.

Have getDetail do only getting detail, and have main switch the element to receive the data.

#include <stdio.h>
    
// student structure
struct student {
  char id[15];
  char firstname[64];
  char lastname[64];
  float points;
};

// function declaration
void getDetail(struct student *);

int main(void) {
  
  // student structure variable
  struct student std[3];
  struct student* ptr = std;
  
  // get student detail 
  for (i = 0; i < 3; i++) { 
    printf("Enter detail of student #%d\n", (i + 1));
    getDetail(ptr);
    // update pointer to point at next element
    // of the array std
    ptr++;
  }
  
  return 0;
}

// function definition


void getDetail(struct student *ptr) {

    printf("Enter ID: ");
    scanf("%s", ptr->id);
    printf("Enter first name: ");
    scanf("%s", ptr->firstname);
    printf("Enter last name: ");
    scanf("%s", ptr->lastname);
    printf("Enter Points: ");
    scanf("%f", &ptr->points);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM