Home > Enterprise >  I am having hard time taking pointer structure as a function's parameter in C. I am a beginner
I am having hard time taking pointer structure as a function's parameter in C. I am a beginner

Time:11-16

#include<stdio.h>

struct create
{
    int a;
    int b;
    int c;
    float total;
};

struct create Create[3];

float givename(struct create* storage[3]);

int main()
{
    for(int j = 0; j<4;   j)
    {
        printf("Input a[%d]:", j 1);
        scanf("%d", &Create[j].a);
        printf("Input b[%d]:", j 1);
        scanf("%d", &Create[j].b);
        printf("Input c[%d]:", j 1);
        scanf("%d", &Create[j].c);
        float sum = givename(&Create);
        printf("%f", sum);
     }
}

float givename(struct create* storage[3])
{
    for(int i = 0; i<4;   i)
    {
         storage[i]->total = storage[i]->a   storage[i]->b   storage[i]->c;
         return storage[i]->total;
     }
}

This is something I wrote and of course it doesn't work, it might seem stupid to elitist C programmers but a help would be appreciated. I wanted to take an input into array of structure and then use it inside a function that has been called by reference. Please tell me can someone help me show what I am misunderstanding with the logic ?

CodePudding user response:

The function

float givename(struct create* storage[3])

takes an array of pointers to struct create, you are sending the address of an array of struct create. If you want an array of pointers to struct create, do it like this:

struct create * array[3];
array[0] = & Create[0];
array[1] = & Create[1];
array[2] = & Create[2];

Plus, this function stops during the first iteration, because of the return.

Array's size is 3, you're looping 4 times.

Your main needs a return value;

If you wanted to pass the whole array to givename (an array of struct create, not pointers), you don't need to since Create is global;

For each iteration in the first loop (3 after fixing), you iterate the whole array, I doubt this is what you wanted to do.

The following version prompts to fill 3 struct create, computes the total for each (and stores it), and prints it. Is this what you wanted to do ?

#include<stdio.h>
#include <stdlib.h>

struct create
{
    int a;
    int b;
    int c;
    float total;
};

struct create Create[3];

float givename(struct create * storage);

int main()
{
    for(int j = 0; j < 3;   j)
    {
        printf("Input a[%d]:", j 1);
        scanf("%d", & Create[j].a);
        printf("Input b[%d]:", j 1);
        scanf("%d", & Create[j].b);
        printf("Input c[%d]:", j 1);
        scanf("%d", & Create[j].c);
        float sum = givename(& Create[j]);
        printf("%f\n", sum);
    }

    return EXIT_SUCCESS;
}

float givename(struct create * storage)
{
    storage->total = storage->a   storage->b   storage->c;
    return storage->total;
}
echo 1 2 3 4 5 6 7 8 9 | yourProgram

>> Input a[1]:Input b[1]:Input c[1]:6.000000
>> Input a[2]:Input b[2]:Input c[2]:15.000000
>> Input a[3]:Input b[3]:Input c[3]:24.000000

What you wanted to achieve is quite unclear, and the naming doesn't help, I recommend you to give better names:

  • struct create doesn't describe what is stored inside

Consider compiling with -Wall -Wextra -Werror.

CodePudding user response:

Your givename() (seems like a bad name) currently only processes one struct as it returns inside the loop. And the way you are calling it after each input of 3 integers it would only process a struct (the first struct each time), and not the array of structs. To process the array of structs you could call it after you got all the input - outside the loop taking input. This shows a version processing during the loop for each struct and then a version processing after the loop with all input.

#include<stdio.h>

struct create
{
    int a;
    int b;
    int c;
    float total;
};

struct create Create[3];

float givename(struct create *storage);
float calculate_totals(struct create storage[3]);
float get_grand_total(struct create [3]);

int main()
{
    for(int j = 0; j<3;   j)
    {
        printf("Input a[%d]:", j 1);
        scanf("%d", &Create[j].a);
        printf("Input b[%d]:", j 1);
        scanf("%d", &Create[j].b);
        printf("Input c[%d]:", j 1);
        scanf("%d", &Create[j].c);
        float sum = givename(&Create[j]);
        printf("%f\n", sum);
    }
    get_grand_total(Create);
    
    /* calculate all totals after input */ 
    for(int j = 0; j<3;   j)
    {
        printf("Input a[%d]:", j 1);
        scanf("%d", &Create[j].a);
        printf("Input b[%d]:", j 1);
        scanf("%d", &Create[j].b);
        printf("Input c[%d]:", j 1);
        scanf("%d", &Create[j].c);
    }     
    float grand_total = calculate_totals(Create);
    printf("grand total = %f\n",grand_total);    
}

float givename(struct create *storage)
{
    storage->total = storage->a   storage->b   storage->c;
    return storage->total;
}

float get_grand_total(struct create storage[3])
{
    float grand_total = 0;
    for (int i = 0; i < 3; i  )
    {
        grand_total  = storage[i].total;   
    }
    printf("grand total sum=%f\n",grand_total);
    return grand_total;
}

float calculate_totals(struct create storage[3])
{
    float grand_total = 0;
    for(int j = 0; j<3;   j)
    {
        storage[j].total = storage[j].a   storage[j].b   storage[j].c;
        printf("total for struct %d=%f\n",j 1,storage[j].total);
        grand_total  = storage[j].total;
    }
    return grand_total;
}
  • Related