jenny
jenny

Reputation: 520

Why can we not return a structure to a function?

Why can we not return a structure as a return value? I would also like to print it as well? I want to print the variables of the structure returned.

#include <stdio.h>

struct distance
{
    int feet;
    float inch;
};

distance add(struct distance d1,struct distance d2);

distance d3;
int main()
{
    struct distance dist1, dist2,dist3;
    printf("first distance\n");
    scanf("%d",&dist1.feet);
    scanf("%f",&dist1.inch);
    printf("second distance\n");
    scanf("%d",&dist2.feet);
    scanf("%f",&dist2.inch);
    dist3 = add(dist1,dist2);
    printf("sum of distances= %d\'-%.1f\"",dist3.feet,dist3.inch);
    return dist3;
}

 distance add(struct distance d1,struct distance d2){
    //Adding distances d1 and d2 and storing it in d3
    d3.feet = d2.feet+d1.feet;
    d3.inch = d2.inch+d1.inch;
    if(d3.inch>12){
        d3.inch-=12;
        ++d3.feet;
    }
}

The following error message is displayed prog.c:7:1: error: unknown type name ‘distance’ distance add(struct distance d1,struct distance d2);

Upvotes: 0

Views: 71

Answers (5)

jenny
jenny

Reputation: 520

#include<stdio.h>

struct distance
{
    int feet;
    float inch;
};

struct distance d1,d2,d3;

struct distance add(struct distance d1, struct distance d2);


int main()
{
    struct distance dist1, dist2,dist3;
    printf("first distance\n");
    scanf("%d",&dist1.feet);
    scanf("%f",&dist1.inch);
    printf("second distance\n");
    scanf("%d",&dist2.feet);
    scanf("%f",&dist2.inch);
    dist3 = add(dist1,dist2);
    printf("sum of distances= %d\'-%.1f\"",dist3.feet,dist3.inch);

}

 struct distance add(struct distance d1,struct distance d2){
    //Adding distances d1 and d2 and storing it in d3
    d3.feet = d2.feet+d1.feet;
    d3.inch = d2.inch+d1.inch;
    while(d3.inch>12){
        d3.inch-=12;
        ++d3.feet;
        return d3;
    }
}

Yes got it! here is the final output for the following inputs inputs:
11
2.3 11 10 and here is the output: sum of distances= 23'-0.3"

Upvotes: 0

Andrew Henle
Andrew Henle

Reputation: 1

To address this error message:

prog.c:7:1: error: unknown type name ‘distance’ distance add(struct distance d1,struct distance d2)

you need to realize that there's a difference between the types struct distance and distance. They are not the same.

Given just:

struct distance
{
    int feet;
    float inch;
};

This is incorrect, as there is no distance type:

distance add(struct distance d1,struct distance d2);

That code assumes distance is a type. It's not. The correct type is struct distance:

struct distance add(struct distance d1,struct distance d2);

You can also solve that with a typedef:

typedef struct distance
{
    int feet;
    float inch;
} distance;

Personally, I don't like hiding types that way - I'd rather see something like struct distance and know it's a struct.

Upvotes: 0

ThingyWotsit
ThingyWotsit

Reputation: 368

typedef struct
{
    int feet;
    float inch;
} distance;

Then get rid of the 'struct' declarations everywhere else...

Note: many thanks to @Lundin for correction:)

Upvotes: -1

cleblanc
cleblanc

Reputation: 3688

You need to return it

distance add(struct distance d1,struct distance d2){
    struct distance d3;

    //Adding distances d1 and d2 and storing it in d3
    d3.feet = d2.feet+d1.feet;
    d3.inch = d2.inch+d1.inch;
    if(d3.inch>12){
        d3.inch-=12;
        ++d3.feet;
    }
    return d3;
}

Upvotes: 2

unwind
unwind

Reputation: 399813

Yes, of course you can return a structure, but you have to actually return it using the return statement:

struct distance add(struct distance d1, struct distance d2)
{
  d1.feet += d2.feet;
  d1.inches += d2.inches;
  while(d1.inches > 12)
  {
    ++d1.feet;
    d1.inches -= 12;
  }
  return d1;  /* Return the result. */
}

Also no point in creating a d3 (and certainly not globally!), just use one of the arguments as the new value, and return that.

Using a loop to adjust for overflow is better, so I did that too.

Finally, you don't typedef the struct distance so you can't just say distance as the return type, it has to be struct distance like the arguments.

Upvotes: 3

Related Questions