[solved]help passing array of structs to function in c


 
Thread Tools Search this Thread
Top Forums Programming [solved]help passing array of structs to function in c
# 1  
Old 04-16-2012
[solved]help passing array of structs to function in c

this is my code to try and prinnt out a deck of cards. the print function worked when used inside main without being a function but now i cant get it to work as a function probably since i dont know how to pass a struct array in c. I gave it a shot but i keep getting an assortment of errors. The structure is called card_t and it has a number variable and suit variable which is an enum type.


Code:
 
#include <stdio.h>
#include "shuffle.h"
void print(struct card_t[]);
int main (int argc, char *argv[])
{
        card_t cards[52];
        int i, type;
        int j = 1;
        for (i = 0; i < 52; i++)
        {
                if (j > 13)
                {
                        j = 1;
                }
                if (i < 13)
                {
                        type = 0;
                }
                if (i >= 13 && i < 26)
                {
                        type = 1;
                }
                if (i >= 26 && i < 39)
                {
                        type = 2;
                }
                if (i > 38)
                {
                        type = 3;
                }
                cards[i].number = j;
                cards[i].suit = type;
                j++;
        }
        print(cards);
return 0;
}
void print (struct card_t cards[])
{
        int i;
        for (i = 0; i < 52; i++)
        {
                char *types[4];
                types[0] = "Clubs";
                types[1] = "Diamonds";
                types[2] = "Hearts";
                types[3] = "Spades";
                if (cards[i].number == 1)
                {
                        printf("Ace of %s\n", types[cards[i].suit]);
                }
                else if (cards[i].number == 11)
                {
                        printf("Jack of %s\n", types[cards[i].suit]);
                }
                else if (cards[i].number == 12)
                {
                        printf("Queen of %s\n", types[cards[i].suit]);
                }
                else if (cards[i].number == 13)
                {
                        printf("King of %s\n", types[cards[i].suit]);
                }
                else
                {
                        printf("%d of %s\n", cards[i].number, types[cards[i].suit]);
                }
        }
}


if anyone could please tell me what i can do to let the struct pass into the print function accurately i would greatly appreciate it.

NVM i actually figured it out myself. just had to remove struct from prototype and function def. thanks anyway

Last edited by bjhum33; 04-16-2012 at 06:47 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable value in a function to be used by another function

Hello All, I would like to ask help from you on how to pass variable value from a function that has been called inside the function. I have created below and put the variables in " ". Is there another way I can do this? Thank you in advance. readtasklist() { while read -r mod ver... (1 Reply)
Discussion started by: aderamos12
1 Replies

2. Shell Programming and Scripting

Pass array to a function and display the array

Hi All I have multiple arrays like below. set -A val1 1 2 4 5 set -A val2 a b c d . . . Now i would like to pass the individual arrays one by one to a function and display/ do some action. Note : I am using ksh Can you please advise any solution... Thanks in advance. (7 Replies)
Discussion started by: Girish19
7 Replies

3. Homework & Coursework Questions

Xlib help - Array of Structs

Hey guys! First of all english is not my main language so sorry for any english mistakes. Im from Portugal! 1. The problem statement, all variables and given/known data: Im having a problema creating and array of structs for a work i need to do. (xLib) 2. Relevant commands, code,... (1 Reply)
Discussion started by: Spiritvs
1 Replies

4. Programming

Xlib help - Array of Structs

Hey guys! First of all english is not my main language so sorry for any english mistakes. Second im a total beginner in programming, still i have a school work to do and i found a problem. Probably something easy to solve but it's driving me crazy. So i created a struct, that holds 4 ints: ... (1 Reply)
Discussion started by: Spiritvs
1 Replies

5. Programming

Passing two dimensional array to a function

Hi. I have a problem with passing two dimensional array to a function. First, let me show my code to explain what i am going to do: I have function:void initialize_board(char board);which is supposed to modify content of passed array. I have read here: Question 6.18 how such arrays should be... (3 Replies)
Discussion started by: Shang
3 Replies

6. UNIX for Dummies Questions & Answers

Passing struct through unix pipe -solved

EDIT: Nevermind, called a friend who is good at this stuff and he figured it out :D Hi all, So I'm trying to teach myself to write programs for unix in c. I am currently creating a program, and I need to pass a struct through a pipe, but I can't figure out how. The struct I want to pass... (0 Replies)
Discussion started by: twnsfn34
0 Replies

7. Shell Programming and Scripting

[bash] passing array to function

Hi, I'm trying to write a function that reassigns an array to another local array but the method used in reassigning the array reformats the contents of the array which is what I am trying to prevent. The method used to load a file into an array works as expected and the entire array is... (4 Replies)
Discussion started by: ASGR
4 Replies

8. Shell Programming and Scripting

Perl Function Array Arguement Passing

Hi All, I have some questions regarding array arguements passing for Perl Function. If @array contains 2 items , arguements passing would be like Code_A. But what if @array needs to add in more items, the rest of the code like $_ will have to be modified as well (highlighted in red), which is... (5 Replies)
Discussion started by: Raynon
5 Replies

9. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

10. Shell Programming and Scripting

[BASH - KSH] Passing array to a function

Passing a array to a function, a basic feature in modern language, seems to be only possible in KSH. Not in BASH. Depite all my efforts I couldn't come to a solution. See the following examples: It works perfectly in KSH: #!/usr/bin/ksh function print_array { # assign array by indirect... (3 Replies)
Discussion started by: ripat
3 Replies
Login or Register to Ask a Question