Sponsored Content
Top Forums Programming [solved]help passing array of structs to function in c Post 302624731 by bjhum33 on Monday 16th of April 2012 05:16:38 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SN(4)							   BSD Kernel Interfaces Manual 						     SN(4)

NAME
sn -- Ethernet driver for SMC91Cxx based cards SYNOPSIS
To compile this driver into the kernel, place the following lines in your kernel configuration file: device sn Alternatively, to load the driver as a module at boot time, place the following line in loader.conf(5): if_sn_load="YES" DESCRIPTION
The sn device driver supports SMC91Cxx based ISA and PCMCIA cards. HARDWARE
The sn driver supports SMC91Cxx based ISA and PCMCIA cards including: o 3Com Megahertz X-Jack Ethernet PC Card XJ10BT, XJ10BC o 3Com Megahertz XJEM and CCEM series: CCEM3288C, CCEM3288T, CCEM3336, CEM3336C, CCEM3336T, XJEM1144C, XJEM1144T, XJEM3288C, XJEM3288T, XJEM3336 o Farallon EtherMac PC Card 595a o Motorola Mariner Ethernet/Modem PC Card o Ositech Seven of Diamonds Ethernet PC Card o Ositech Jack of Hearts Ethernet/Modem PC Card o Psion Gold Card Netglobal Ethernet PC Card o Psion Gold Card Netglobal 10/100 Fast Ethernet PC Card o Psion Gold Card Netglobal 56k+10Mb Ethernet PC Card o SMC EZEther PC Card (8020BT) o SMC EZEther PC Card (8020T) The sn driver supports the SMC 91C90, SMC 91C92, SMC 91C94, SMC 91C95, SMC 91C96, SMC91C100 and SMC 91C100FD chips from SMC. The Farallon EtherWave and EtherMac card came in two varieties. The ep(4) driver supports the 595 and 895 cards. These cards have the blue arrow on the front along with a 3Com logo. The Farallon 595a cards, which have a red arrow on the front, are also called EtherWave and EtherMac. They are supported by the sn driver. SEE ALSO
ed(4), ep(4), ie(4), intro(4), ng_ether(4), vx(4), ifconfig(8) HISTORY
The sn device driver appeared in FreeBSD 4.0. BSD
July 16, 2005 BSD
All times are GMT -4. The time now is 04:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy