Re: I went a help



On Fri, 17 Mar 2006 05:06:17 -0600,
h_u_s2002@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (join) wrote:

that what i did auntil now but i cause i problem , the output of this
code id

null 0.000 0

How did you execute the code when it has syntax errors? Is this your
real code or did you retype it? You should always use cut and paste.



what the problem here?

also, what is the best way of finding the max,min ,average of the
gpa?


code:
#include <stdio.h>

#include <stdlib.h>
typedef struct student {
char *name;
int id;
float gpa;
} stu;

stu nig[5];
int main(){
int i,n,avg,min,max;
int id;
float gpa;
char name[5];
FILE *inputFile;
inputFile = fopen("q4.txt","r");

if (inputFile) printf("File succesfully opened for
reading!\n");
else {
printf("File not opened\n");
exit(1);
}


for(i=0;i<5; i++){
fscanf(inputFile, "%s %d
%f",nig.name,nig[i].id,nig[i].gpa);

nig.name is syntactically incorrect. You need nig[i].name, just like
with id and gpa. nig is an array of struct. nig[i] is the i-th
struct in the array. And nig[i].name is the pointer to char in that
struct.

Unfortunately, you have an additional problem. nig is a global array.
It is initialized by default such that each element of the array (in
this case that means each member of each element) is set to the
correct form of zero. Since name is a pointer, name is initialized to
NULL. Your call to fscanf directs that function to store the input
name there. You are not allowed to store data where a NULL pointer
points. You need to initialize all five nig[i].name pointers to point
somewhere in memory that belongs to your program.




printf("are:%s%d,%f",nig[i].name,nig[i].id,nig[i].gpa);

You should add a \n to the end of your format string.


//}
fclose(inputFile);

}
return 0;

}


Remove del for email
.



Relevant Pages

  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: Another spinoza challenge
    ... You should test against the int type's limits: ... typedef struct complex ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: Another spinoza challenge
    ... int main{ ... using struct and typedef. ... a pointer to an integer ... A macro is preferable because it is replaced by inline code, ...
    (comp.lang.c)
  • Re: [RFC][PATCH 1/6] memcg: fix pre_destory handler
    ... returns struct cgroup of id. ... SwapCgroup uses array of "pointer" to record the owner of swaps. ... struct cgroup_id is freed by RCU. ... changed interface from pointer to "int" ...
    (Linux-Kernel)
  • Re: Passing an array of structuresfrom a pointer?
    ... I've tried an attempt to do that, and I tried to "clean" the header and main ..c file. ... int LIST_NUMBER; ... typedef struct ddListBox{ ... DDLB_COLL1array! ...
    (microsoft.public.vc.language)