hello i m suppose to do this program which should pass array of ptrs to a structure intilise and put student info in each array i did most of it but now i got problem coz i cant just pass address of structure since its an array of ptrs to a structure you cant do &str since that will work on one structure only anyways here is the code

#include <stdio.h>
#define CSIZE 4
typedef struct Names {
    char SzFirst[100];
    char SzLast[100];
}Family;
typedef struct Student {
    Family Handle;
    float Grade[3];
    float Average;
}student;
void EatLines() {
    while(getchar()!='\n')
        continue;
}
void FillArr(student **std,int Size) {
    float sum=0;
    for(int i=0;i<Size;i++) {//Fill info
        printf("Please enter Your first and Last name");
        scanf("%s %s",std[i]->Handle.SzFirst,std[i]->Handle.SzLast);
        EatLines();
        printf("Please enter 3 scores of some of your exams");
        for(int x=0;x<sizeof std[i]->Grade/sizeof std[i]->Grade[0];x++) {
            scanf("%f",&std[i]->Grade[x]);//lol weird code :P
            sum+=std[i]->Grade[x];
        }
        EatLines();
        std[i]->Average= sum / sizeof std[i]->Grade/sizeof std[i]->Grade[0];//average=sum/howmany :P
        sum=0;//reset sum to 0 so we preform some average again on next struct :P
    }
}
void ShowArr(student **std,int Size) {
    for(int i=0;i<Size;i++)
        printf("Student num %d name %s %s and his average score is %d",
                i+1,std[i]->Handle.SzFirst,std[i]->Handle.SzLast,std[i]->Average);
}
int main(void)
{
    student std[CSIZE];
    FillArr(&std,sizeof std/sizeof std[0]);
    ShowArr(&std,sizeof std/sizeof std[0]);
    return 0;
}

Recommended Answers

All 8 Replies

its giving me this error

structure.c|40|error: cannot convert `student (*)[4]' to `student**' for argument `1' to `void FillArr(student**, int)'|
structure.c|41|error: cannot convert `student (*)[4]' to `student**' for argument `1' to `void ShowArr(student**, int)'|
||=== Build finished: 2 errors, 0 warnings ===|
but i m passing first address shouldnt the increment operator point to 2nd array of the structure and since i made the structure in the function argument is a ptr to a ptr ?wouldnt that work ?

This oughtta help for the compile.

void FillArr(student *std,int Size) {
/* ... */
void ShowArr(student *std,int Size) {
/* ... */
    FillArr(std,sizeof std/sizeof std[0]);
    ShowArr(std,sizeof std/sizeof std[0]);

And lots of -> to . , plus %f for floating point instead of %d .

but why does it work isnt std isnt address since its not like arrays structures names arent addresses?

and also since i m using a copy since its not ptrs why the value got changed ?

http://c-faq.com/aryptr/aryptrequiv.html

That is, whenever an array appears in an expression, the compiler implicitly generates a pointer to the array's first element, just as if the programmer had written &a[0].

oh so structure or no structure doesnt matter if its put into an array but why has value changed if i didnt use orignal variable ? since i used only . i thought . only does a copy and when you put it inside other function it will the value will dispear

You're passing a pointer. You dereference the pointer, modifying what is pointed to. Even if the pointer is a copy of the pointer you want, the contents being pointed to are the same and that is what you are modifying.

oke thanks man a lot for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.