facebook like button

10 January, 2012

Returning a structure in C

The need:
     This program is basically solution of a problem I found on internet. Problem statement is:
There is a purse filled with some coins of gold, silver ans copper. A person draws coins one by one and notes down the type of coin each time. He writes g for gold, s for silver and c for copper coin. Thus a character string is formed. You have to write a function countCoins() which takes that string as parameter and returns count each type of coin. As we know that a function can return only single value, we have 2 options left to return all three counts with one function:
    1. store 3 answers in some contiguous location and return a pointer to that location.
    2. If possible, club all three variables to make a single variable and return that single variable. Yes it is possible, we can make a structure variable containing all three counts and return that structure variable.

The code:
----------------------------------------------------------
#include<stdio.h>
typedef struct
{
    int silver;
    int copper;
    int gold;
}CoinPurse;
CoinPurse countCoin(char *);

int main()
{
    int i;
    CoinPurse a,*b;
    char *coins="gscgcc";
    a=countCoin(coins);
    printf("Coppper coins are %d\n",a.copper);
    printf("Gold coins are %d\n",a.gold);
    printf("Silver coins are %d\n",a.silver);
    return 0;
}

CoinPurse countCoin(char *coins)
{
    CoinPurse x;
    x.copper=0;
    x.gold=0;
    x.silver=0;
    while(*coins)
    {
        switch(*coins)
        {
        case 'c':    x.copper++;
                    break;
        case 'g':    x.gold++;
                    break;
        case 's':    x.silver++;
                    break;
        default:    break;
        }
        coins++;
    }
    return x;
}
----------------------------------------------------------
Approach:
    The approach is very simple. The structure CoinPurse contains counter for each type of coin. the function countCoin() takes a character string , examines each character and based on that increments the correct counter. after the string is finished, the structure containing the counts is returned by the function countCoin(). In this program i have given the character string *coins as "gscgcc" soit is clear that there are 2 gold, 1 silver and 3 copper coins which can be verified by the program output.

3 comments:

  1. returning structures from function is not considered good programming practice: for large structure bitwise copy is required and requires unpredictable time.

    More elegant way:
    1. return pointer to structure.
    2. pass a pointer to structure to function.

    ReplyDelete
    Replies
    1. This program is just for demonstration that structures can also be returned. Otherwise in 'the need' itself I have said "store answers in some contiguous location(may be structure) and return a pointer to that location."

      Delete
  2. The Wynn Casino Is A Bad Place To Play & Stay | Dr.MCD
    With a $2.4 billion market cap, the Wynn 진주 출장안마 Las Vegas 세종특별자치 출장안마 is 문경 출장안마 one of 속초 출장샵 the most well-known luxury resorts in the world. the 논산 출장안마 casino at Wynn Las Vegas,

    ReplyDelete

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply