I'm a beginning at C and I'm quite confused and clueless on how to store input into an array when that input was made by calling a function for.
I have an inputCustomerDetails
function and I don't quite know how to store what it reads into an array.
typedef struct customer
{
char name[256];
int age;
}Customer
Customer inputCustomerDetails()
{
Customer new_cust;
printf("Enter your name: ");
scanf("%s", new_cust.name);
printf("Enter your age: ");
scanf("%s", new_cust.age);
return new_cust;
}
void main(){
Customer customer[5];
Customer *cust;
cust = customer;
*cust = inputCustomerDetails();
for(int i = 0; i < 5; i )
{
scanf("%d", &cust[i]);
}
}
I think the scanf
part is obviously wrong but I was basically doing trials and error by reading about arrays.
CodePudding user response:
Your inputCustomerDetails
function fills in a local Customer
structure and returns a copy of that. You can assign that returned copy directly to an element of your customer
array.
Also, the scanf("%s", new_cust.age);
line is wrong: use the %d
format for integer input and pass the address of the target integer as the corresponding argument to scanf
. (In the case of reading the .name
member, that array automatically 'decays' into the address of its first element, which is what the %s
specifier expects.)
Here's a possible solution:
#include <stdio.h>
typedef struct customer {
char name[256];
int age;
} Customer; // Need a semicolon here!
Customer inputCustomerDetails(void)
{
Customer new_cust;
printf("Enter your name: ");
scanf("%s", new_cust.name);
printf("Enter your age: ");
scanf("%d", &new_cust.age); // Note the %d format and the & sign
return new_cust;
}
int main()
{
Customer customer[5];
for (int i = 0; i < 5; i )
{
customer[i] = inputCustomerDetails();
}
// Check ...
for (int i = 0; i < 5; i )
{
printf("%s is %d\n", customer[i].name, customer[i].age);
}
}
CodePudding user response:
Your code is full of bugs and misconceptions.
This is what you want:
#include <stdio.h>
#include <stdlib.h>
typedef struct customer
{
char name[256];
int age;
} Customer; // a ; was missing here
Customer inputCustomerDetails()
{
Customer new_cust;
printf("Enter your name: ");
scanf("%s", new_cust.name);
printf("Enter your age: ");
scanf("%d", &new_cust.age); // a & was missing here, and %s
// wa used wrongly rather then %d
return new_cust;
}
void main() {
Customer customer[5];
for (int i = 0; i < 5; i )
{
customer[i] = inputCustomerDetails();
}
// now the customer array contains 5 customers
}
The main
function was overly complicated and wrong.
CodePudding user response:
There are a few point in your code that should be addressed. int main(void)
or int main(int argc, char **argv)
are correct. Unless you have a very specific reason, do not use any other signature for main
. For functions which accept no arguments, use an explicit void
instead of omitting the argument list. It's not 1987.
You must always check the value returned by scanf, and you should use a width modifier for each conversion. Even simple code using a raw %d
may exhibit undefined behavior on some inputs (in particular, if the input string cannot fit in an integer). Usually, casual code does not bother limiting the input on %d
conversions, but you should always limit your reads for %s
to no more than one less than the size of the buffer and always check the return value. It is safest to just avoid scanf
completely. Learning its foibles will not be enlightening, and you will wind up burning a lot of time on stupid bugs.
Don't bother with the typedef. They are useful for complicated function definitions and for hiding opaque types. Neither of those situations applies here.
Passing the structure by value can be very expensive. It is more idiomatic not to.
#include <stdio.h>
#include <stdlib.h>
struct customer
{
char name[256];
int age;
};
void
inputCustomerDetails(struct customer *new_cust)
{
printf("Enter your name: ");
if( scanf("%5s", new_cust->name) != 1 ){
fprintf(stderr, "invalid input\n");
exit(EXIT_FAILURE);
}
printf("Enter your age: ");
if( scanf("M", &new_cust->age) != 1 ){
fprintf(stderr, "invalid input\n");
exit(EXIT_FAILURE);
}
}
int
main(void)
{
struct customer customer[5];
struct customer *last = customer sizeof customer / sizeof *customer;
struct customer *cust;
for( cust = customer; cust < last; cust = 1 ){
inputCustomerDetails(cust);
}
return EXIT_SUCCESS;
}