#include <stdio.h>
#include "struct.h"
#define NUM 3
struct Student
{
char name[20];
int age;
};
int main(void)
{
struct Student s_array[NUM];
for(int i=0;i<NUM;i++)
{
printf("name: ");
scanf("%s",s_array[i].name);
printf("age: ");
scanf("%i",s_array[i].age);
}
for(int i=0;i<NUM;i++)
{
printf("%s is %i years old",s_array[i].name,s_array[i].age);
}
return 0;
}
I don't know what's the problem I declared an array of structs of type student and used a for loop to initialise their fields, but when I type in the age it gives me segmentation fault. why is that??
struct.c:17:20: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
scanf("%d",s_array[i].age);
The int in the struct is an int, not a pointer to an int. Arrays can be assigned directly to pointers, but on other types, you need to apply the addressof (&) operator. Instead of saying "s_array[i].age", say "&(s_array[i].age)".
I worked out this code for you
#include < stdio.h>
#include < stdlib.h>
#define NUM 3
struct Student
{
char name[20];
int age;
};
int main(void)
{
struct Student s_array[NUM];
for(int i=0;i<NUM;i++)
{
printf("name: ");
scanf("%s",&s_array[i].name);
printf("age: ");
scanf("%d",&s_array[i].age);
}
for(int i=0;i<NUM;i++)
{
printf("%s is %d years old",s_array[i].name,s_array[i].age);
}
return 0;
}
#include<stdio.h>
//#include "struct.h>
#define NUM 3
struct Student
{
char name[20];
int age;
};
int main(void)
{
struct Student s_array[NUM];
for(int i=0;i<NUM;i++)
{
printf("name: ");
scanf("%s",&s_array[i].name);
printf("age: ");
scanf("%d",&s_array[i].age);//You sud use "&"operator to take input
}
for(int i=0;i<NUM;i++)
{
printf("%s is %d years old",s_array[i].name,s_array[i].age);
}
return 0;
}
Related
I'm trying to call two functions, the first function will receive data and the second will show the data. But my second function is equal to the first, so it doesn't do what I intend to do. Any suggestions guys?
struct Operador
{
char nome[32];
char telefone[15];
char idade[3];
};
struct Operador* fun( ) {
struct Operador* pItems = malloc( 3 * sizeof(struct Operador));
int n;
for(n=0;n<1;n++){
printf(" name: "); gets(pItems[n].nome);
printf(" telefone: "); gets(pItems[n].telefone);
printf(" age: "); gets(pItems[n].idade);
}
return pItems;
}
//*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*fim_operador
void lo()
{
struct Operador* pItems =fun();
int j;
printf("\n\n");
for(j=0;j<1;j++){
printf(pItems[j].nome);
printf(pItems[j].telefone);
printf(pItems[j].idade);
printf("\n\n");
}
free(pItems);
}
main()
{
fun();
lo();/ i want this function to simply display data
Some alterations in your code:
in function fun() instead of gets maybe it would be better to use scanf.
in function fun() and lo() you access your pointer to a struct with the '.' operator.
you should use the '->' operator instead.
i do not understand why you use a for loop to get and print the values while you have only one value but i suppose you have your reasons (maybe to add more structs at a future implementation.
below i suggest a code you could use:
#include <stdio.h>
#include <stdlib.h>
struct Operador
{
char nome[32];
char telefone[15];
char idade[3];
};
struct Operador* fun() {
struct Operador* pItems = malloc(3 * sizeof(struct Operador));
//int n;
//for (n = 0; n<1; n++){
printf(" give nome: ");
scanf("%s", pItems->nome);
printf(" give telefone: ");
scanf("%s", pItems->telefone);
printf(" give age: ");
scanf("%s", pItems->idade);
//}
return pItems;
}
//*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-
void lo(void)
{
struct Operador* pItems = fun();
// int j;
printf("\n\n");
// for (j = 0; j<1; j++){ //no need for "for"?
printf("Name is: %s \n", pItems->nome);
printf("telefone is: %s \n", pItems->telefone);
printf("age is: %s \n", pItems->idade);
printf("\n\n");
// }
free(pItems);
return;
}
main()
{
//fun(); //no need to call, is called in lo()
lo();
}
the output is:
give nome: jim
give telefone: 2673673
give age: 32
Name is: jim
telefone is: 2673673
age is: 32
Press any key to continue . . .
Every time I try to compile the following code I get a boatload of errors.
Does anyone see any obvious things that I overlooked with my C program. Any help would be appreciated.
The header file structures.h is also attached.
This is the structures.cc file
#include <stdio.h>
#include <string.h>
#include "structures.h"
void getOnePerson(person_t *p) {
char *name;
char *ssn;
int *year;
printf("Enter full name: ");
scanf("%s", &name);
p.name = &name;
printf("\nEnter ssn: ");
scanf("%s", &ssn);
p.ssn = &ssn;
printf("\nEnter year of birth: ");
scanf("%d", year);
p.yearOfBirth = &year;
}
void printOnePerson(person_t t1) {
char *name = t1.name;
char *ssn = t1.ssn;
int year = t1.yearOfBirth;
printf("%s", &name);
printf(":");
printf("%s", &ssn);
printf(":");
printf("%p", &year);
printf("\n");
}
void getPeople(person_t peoples[], int people) {
for (int a = 0; a < people; a++) {
getOnePerson(peoples[a]);
}
}
void printPeople(person_t peoples[], int people) {
for (int p = 0; p < people; p++) {
peoples[p];
}
}
This is the structures.h file
#include <stdio.h>
#include <string.h>
#define NAME_SIZE 80
#define SSN_SIZE 13
#define NUM_PEOPLE 10
typedef struct{
char name[NAME_SIZE];
char ssn[SSN_SIZE];
int yearOfBirth;
}person_t;
void getOnePerson(struct person_t *p);
void printOnePerson(struct person_t p);
void getPeople(struct person_t peoples[], int people);
void printPeople(struct person_t peoples[], int people);
Your function declarations in the header file are incorrect.
You make reference to one or more parameters of type struct person_t, but there is no such type. There is a type named person_t, so change the function prototypes to use that.
void getOnePerson(person_t *p);
void printOnePerson(person_t p);
void getPeople(person_t peoples[], int people);
void printPeople(person_t peoples[], int people);
Your get and print functions also have several issues. In both cases, you're using intermediate variables to read/write values which aren't necessary:
void getOnePerson(person_t *p) {
printf("Enter full name: ");
scanf("%s", p->name);
printf("\nEnter ssn: ");
scanf("%s", p->ssn);
printf("\nEnter year of birth: ");
scanf("%d", &p->yearOfBirth);
}
void printOnePerson(person_t t1) {
printf("%s", t1.name);
printf(":");
printf("%s", t1.ssn);
printf(":");
printf("%d", t1.yearOfBirth);
printf("\n");
}
very new to c. I wrote the following code.
typedef struct
{
char name[100];
int comp, math, phys;
int total;
} student[100];
int main(int argc, char** argv) {
int number;
do
{
printf("Enter how many students: ");
scanf("%d", &number);
if(number < 0)
{
printf("Wrong input! \n");
}
}
while(number < 0);
int i;
for(i=0; i < number; ++i)
{
printf("Student %d's name: ", i+1);
scanf("%s", student[i].name);
printf("Comp: " );
scanf("%d", &student[i].comp);
printf("Phys: " );
scanf("%d", &student[i].phys);
printf("Math: " );
scanf("%d", &student[i].math);
&student[i].total = &student[i].comp + &student[i].math + &student[i].phys;
}
printf("s%", &student[1].name);
return (EXIT_SUCCESS);
}
I keep getting error: expected expression before 'student' in all the scanf lines and the last printf line. What am I doing wrong? Very new to C so any help would be great.
Do
struct
{
char name[100];
int comp, math, phys;
int total;
} student[100];
If you want to combine the definition and identifier. You should know that student isn't a type, it's an array of structs without a name. There are alternatives to what you want to accomplish. For example:
typedef struct student
{
char name[100];
int comp, math, phys;
int total;
} student;
student students[100];
Drop the typedef keyword. You're wanting to create an array of 100 student objects, not a type name representing an array of 100 students.
Hopefully this helps you in the future:
// `Type var` is a shorter way to write `struct tag var`
// `Type` is just an alias (another name) for `struct tag`
typedef struct tag {
int x;
} Type;
// `Type100 arr` is a shorter way to write `struct tag100 arr[100]`
// `Type100` is just an alias for `struct tag100[100]`
// No, you can't do `struct tag100[100] arr`; `Type100 arr` gets around this restriction
typedef struct tag100 {
int x;
} Type100[100];
// `var` contains a single value of type `struct tagX`
struct tagX {
int x;
} var;
// `arr` is an array of 100 values of type `struct tagX100`
struct tagX100 {
int x;
} arr[100];
So, I'm new in this, and trying structures out... Error C2073 appeares..can someone help and give some advice?
I tried with FOR in main function to call functions "ispis" which is for printf only, and function "unos" which is for scanf so many times, how big is int "broj_knjiga". I tried to work with -> instead of . but I simply can't solve this problem(which is simple). Someone help?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void ispis(struct knjiga *pt, int broj)
{
int i;
for(i=0; i<broj; i++)
{
printf("\ID knjige: %d", &pt[i].ID_knjige);
printf("\Autor knjige: %s", &pt[i].autor);
printf("\Naslov knjige: %d", &pt[i].naslov);
}
}
void unos(struct knjiga *pt, int broj)
{
int i;
for(i=0; i<broj; i++)
{
printf("\nUnesite ID knjige: ");
scanf("%d", &pt[i].ID_knjige);
printf("\nUnesite autora knjige: ");
scanf("%d", &pt[i].autor);
printf("\nUnesite naslov knjige: ");
scanf("%d", &pt[i].naslov);
}
}
struct knjiga
{
int ID_knjige;
char autor[40];
char naslov[20];
};
int main()
{
struct knjiga *pt;
int broj_knjiga;
printf("Unesite koliko knjiga unosite: ");
scanf("%d", &broj_knjiga);
pt=(struct knjiga*)malloc(sizeof(struct knjiga)*broj_knjiga);
unos(pt, broj_knjiga);
ispis(pt, broj_knjiga);
return 0;
}
C is compiled from the top-down. Structs only exist below their declarations. Moving your struct definition above the rest of your code will fix your problem.
knjiga myStruct1; // invalid
struct knjiga
{
int ID_knjige;
char autor[40];
char naslov[20];
};
knjiga myStruct2; // valid
I have come across this wierd and mysterous (at least to me) error that I am finding a very hard time finding. It gives me an error at the line where I call my function input(student_list1[MAX], &total_entries); where the compiler says:
incompatible type for agument 1 in 'input'
What am I doing wrong here? I sense it something very simple and stupid but I have gone through the code several times now without any avail.
#define MAX 10
#define NAME_LEN 15
struct person {
char name[NAME_LEN+1];
int age;
};
void input(struct person student_list1[MAX], int *total_entries);
int main(void)
{
struct person student_list1[MAX];
int total_entries=0, i;
input(student_list1[MAX], &total_entries);
for(i=0; i<total_entries; i++)
{
printf("Student 1:\tNamn: %s.\tAge: %s.\n", student_list1[i].name, student_list1[i].age);
}
return 0;
} //main end
void input(struct person student_list1[MAX], int *total_entries)
{
int done=0;
while(done!=1)
{
int i=0;
printf("Name of student: ");
fgets(student_list1[i].name, strlen(student_list1[i].name), stdin);
student_list1[i].name[strlen(student_list1[i].name)-1]=0;
if(student_list1[i].name==0) {
done=1;
}
else {
printf("Age of student: ");
scanf("%d", student_list1[i].age);
*total_entries++;
i++;
}
}
}
struct person student_list1[MAX] in the function argument is actually a pointer to struct person student_list1.
student_list1[MAX] you passed is a (out of bound) member of the array struct person student_list1[MAX]. Valid array index shoudl be between 0 to MAX - 1.
Change it to:
input(student_list1, &total_entries);
Note that here the array name student_list1 is automatically converted to a pointer to student_list1[0].
There are many things wrong with the code; this is my attempt at making it somewhat more robust:
#include <stdio.h>
#include <string.h>
#define MAX 10
#define NAME_LEN 15
// use a typedef to simplify code
typedef struct person {
char name[NAME_LEN];
int age;
} person_t;
// size qualifier on student_list is redundent and person_t* does the same
void input(person_t *student_list, int *total_entries);
int main(void)
{
person_t student_list[MAX];
int total_entries, i;
// pass array and not the non-existent 'student_list[MAX]' element
input(student_list, &total_entries);
for(i=0; i<total_entries; i++)
{
// age is an int, not a string so use %d
printf("Student 1:\tName: %s.\tAge: %d.\n", student_list[i].name, student_list[i].age);
}
return 0;
} //main end
void input(person_t *student_list, int *total_entries)
{
int done = 0, i = 0;
*total_entries = 0;
while (i < MAX) {
printf("Name of student: ");
// use NAME_LEN instead of strlen(list[i].name) because latter is
// probably not initialized at this stage
if (fgets(student_list[i].name, NAME_LEN, stdin) == NULL) {
return;
}
// detect zero-length string
if (student_list[i].name[0] == '\n') {
return;
}
printf("Age of student: ");
scanf("%d", &student_list[i].age);
// read the newline
fgetc(stdin);
*total_entries = ++i;
}
}
input(student_list1[MAX], &total_entries); shoud be input(student_list1, &total_entries);.
In C,
void input(struct person student_list1[MAX], int *total_entries);
equals
void input(struct person *student_list1, int *total_entries);