Trying to learn pointers and structs, getting stuck (C)

Hello. I've been reading along in the K&R (2nd ed, ofc!) and my aim is to make a simple blackjack clone as my first game. It's supposed to run in the terminal and have simple keystrokes for actions. I'm stuck on building the initial deck and a debug function to test and ensure the deck is built properly. What I have so far is in the following paste:
http://sprunge.us/JUMS?c
Understand that I'm sort of flailing about in my understanding of pointers and how they interact with structs. As far as I can tell, there are two ways to do this; with pointers and with array subscription. I'd prefer the latter if possible since it would be easier to understand, but I'd like to learn both. The problem I'm getting is that my deck isn't being built properly, which makes the test_deck() function return a few bogus cards and then a bunch of zeros (if it doesn't segfault!)
Any help would be great.

struct card nd;
struct card *pnd;
int suits;
pnd = &nd;
pnd++;
nd = *pnd;
Not good. "nd" is *one* instance of the struct, and "pnd" points to that. Incrementing "pnd" points it to a memory location after the end of the 'array' (which is only one element long). This is why it's segfaulting.
Here are two ways to do it, one using pointers and one using array indices (pointers in disguise). Both return a pointer to the deck (as it's an array), so the rest of your code will need to be slightly modified:
struct card* create_deck (void)
struct card* nd = (struct card*) malloc (sizeof (struct card) * 52);
struct card *pnd;
int suits;
pnd = nd;
for (suits = 1; suits <= 4; suits++) {
int n;
for (n = 1; n <= 13; n++) {
switch(suits) {
case 1:
pnd->suit = 'h';
break;
case 2:
pnd->suit = 'd';
break;
case 3:
pnd->suit = 'c';
break;
case 4:
pnd->suit = 's';
break;
pnd->value = n;
pnd++;
return nd;
struct card* create_deck (void)
struct card *nd = (struct card*) malloc (sizeof (struct card) * 52);
int suits, n, i;
i = 0;
for (suits = 1; suits <= 4; suits++) {
for (n = 1; n <= 13; n++) {
switch(suits) {
case 1:
nd[i].suit = 'h';
break;
case 2:
nd[i].suit = 'd';
break;
case 3:
nd[i].suit = 'c';
break;
case 4:
nd[i].suit = 's';
break;
nd[i].value = n;
i++;
return nd;
edit: Of course, you will also need to free the returned value at some point.
Last edited by Barrucadu (2011-01-25 18:11:17)

Similar Messages

  • Tried to restore ipod and it gets stuck at the very end

    please help! i plugged in my ipod to recovery mode to get all the apps off and it gets stuck at the very end of the restore. ive tried it more than five times now please help!!

    Welcome to the discussions,
    did you check your security software settings? http://support.apple.com/kb/TS3125 iTunes needs to connect to the Apple server during the restore process.

  • I am trying to download my purchased music and it get stuck in waiting

    I am trying to download my purchased music and it get stuck in waiting. Any suggestion?

    Maybe... Download = browser is used - check out the browser's preferences.
    Be well...

  • I'm trying to install Windows XP on a system, and Setup gets stuck at the "Setup is inspecting your computer's hardware configuration" stage

    I’m trying to install Windows XP on a system, and Setup gets stuck at the “Setup is inspecting your computer’s hardware configuration” stage
    how can I fix it, i turned off and on, and the message still there help please

    If you have a PPC Mac, you can't install Windows unless you are using something like Parallels or a virtual application.  If you are using Boot Camp, eject the disc, reboot and try again.
    Miriam

  • When I'm trying to deauthorize my iPod from my iTunes, it's trying to deauthorize it' but iTunes get stuck and I can only shut it down

    I have just bought an new i Phone, but I had an old iPod that I authorized on iTunes.
    Now, when I'm trying to deauthrize it, iTunes just get stuck, and won't continue working unless i shut it down directly from the task manager....
    What can I do?
    Thanks

    Could any of the music have been sourced on an external USB drive or a network drive? In iTunes, go to one of the songs that won't sync. Bring up the info page, and look at the location of that song on the disk. Does that file and path exist? That could be your problem, especially if they were on a USB drive you forgot about.

  • HT3606 i keep trying to update the software and it gets stuck on configuration, so i can i sped up the process?

    i keep trying to update the software and it gets stuck on configuration, so i can i sped up the process?

    Some general advice on updating:
    It is worth noting that it is an extreme rarity for updates to cause upsets to your system, as they have all been extensively beta-tested, but they may well reveal pre-existing ones, particularly those of which you may have been unaware. If you are actually aware of any glitches, make sure they are fixed before proceeding further.
    So before you do anything else:
    If you can, make a full backup first to an external hard disk. Ideally you should always have a bootable clone of your system that enables you to revert to the previous pre-update state.
    Turn off sleep mode for both screen and hard disk.
    Disconnect all peripherals except your keyboard and mouse.
    1. Repair Permissions (in Disk Utility)
    2. Verify the state of your hard disk using Disk Utility. If any faults are reported, restart from your install disk (holding down the C key), go to Disk Utility, and repair your startup disk. Restart again to get back to your startup disk.
    At least you can now be reasonably certain that your system does not contain any obvious faults that might cause an update/upgrade to fail.
    3. Download the correct version of the COMBO update from the Apple download site.
    The Combo updater of Leopard 10.5.8 can be found here:
    http://support.apple.com/downloads/Mac_OS_X_10_5_8_Combo_Update
    If you prefer to download updates via Software Update in the Apple menu (which would ensure that the correct version for your Mac was being downloaded), it is not recommended to allow SU to install major (or even minor) updates automatically. Set Software Update to just download the updater without immediately installing it. There is always the possibility that the combined download and install (which can be a lengthy process) might be interrupted by a power outage or your cat walking across the keyboard, and an interrupted install will almost certainly cause havoc. Once it is downloaded, you can install at a time that suits you. You should make a backup copy of the updater on a CD in case you ever need a reinstall.
    Full details about the 10.5.8 update here: http://support.apple.com/kb/HT3606
    More information on using Software Updater here:
    http://support.apple.com/kb/TA24901?viewlocale=en_US
    Using the Combo updater ensures that all system files changed since the original 10.5.0 are included, and any that may have been missed out or subsequently damaged will be repaired. The Delta updater, although a temptingly smaller download, only takes you from the previous version to the new one, i.e. for example from 10.5.7 to 10.5.8. Software Update will generally download the Delta updater only. The preferable Combo updater needs to be downloaded from Apple's download site.
    Now proceed as follows:
    4. Close all applications and turn off energy saving and screensaver.
    5. Unplug all peripherals except your keyboard and mouse.
    6. Install the update/upgrade. Do not under any circumstances interrupt this procedure. Do not do anything else on your computer while it is installing. Be patient.
    7. When it ask for a restart to complete the installation, click restart. This can take longer than normal, there are probably thousands of files to overwrite and place in the correct location. Do nothing while this is going on.
    8. Once your Mac is awake, repair permissions again, and you should be good to go!
    If your Mac seems slightly sluggish or ‘different’, perform a second restart. It can’t hurt and is sometimes efficacious! In fact a second restart can be recommended.
    9. Open a few of your most used applications and check that all is OK. In this connection please remember that not all manufacturers of third party applications and plug-ins, add-ons, haxies etc, will have had time to do any necessary rewrites to their software to make them compliant with the latest version of your operating system. Give them a week or two while you regularly check their websites for updates.
    N.B. Do not attempt to install two different updates at the same time as each may have different routines and requirements. Follow the above recommendations for each update in turn.
    Lastly, Apple's own article on the subject of Software Update may also be useful reading:
    http://docs.info.apple.com/article.html?artnum=106695
    [b]If you are updating Safari (or just have):[/b]
    Input Managers from third parties can do as much harm as good. They use a security loophole to reach right into your applications' code and change that code as the application starts up.  If you have installed an OS update and Safari is crashing, the very [i]first[/i] thing to do is clear out your InputManagers folders (both in your own Library and in the top-level /Library), log out and log back in, and try again.
    So, disable all third party add-ons before updating Safari, as they may not have been updated yet for the new version. Add them back one by one. If something goes awry, remove it again and check on the software manufacturer's website for news of an update to match your version of Safari.
    Most errors reported here after an update are due to an unrepaired or undetected inherent fault in the system, and/or a third party add-on.
    Additional tips on software installation here:
    http://docs.info.apple.com/article.html?artnum=106692
    To reiterate, Input Managers reach right into an application and alter its code. This puts the behavior of the affected application outside the control and responsibility of its developers: a recipe for  problems. That's not to say that issues absolutely will ensue as a result of Input Managers, but you, as a user, must decide. If the functionality of a specific Input Manager or set thereof is really important to you, you may well choose to assume the associated risk.
    Again, the advice is to remove all Input Managers from the following directories:
    /Library/InputManagers
    ~/Library/InputManagers
    especially prior to system updates (they can always be added back one-by-one later).
    Solutions for troubleshooting installation, startup, and login issues in Mac OS X v10.5
    http://support.apple.com/kb/TS1541?viewlocale=en_US

  • Trying to update to 10.7 I tunes to windows 7 64 bit and download gets stuck and can't switch it off without crashing computer-pain and frustrating,can anyone at apple help?

    Trying to update to 10.7 I tunes to windows 7 64 bit and download gets stuck and have to crash the computer to stope download window to try other options-it's been the same all week
    Even when I try to download 64bit updated as forums say it gets stuck.
    You would have thought it should be a simple update as usual.
    Any advice
    Thanks,

    I'd first try downloading an installer from the Apple website using a different web browser:
    http://www.apple.com/quicktime/download/
    If you use Firefox instead of IE for the download (or vice versa), do you get a working installer?

  • Visual Studio 2013 Setup and Installation gets stuck at system restore point. what are the issues?, help me out!!!

    Visual Studio 2013 Setup and Installation gets stuck at system restore point. what are the issues?, help me out ASAP

    Hello nitinrathod29,
    There are some possible fixes for this issue.
    Please check the following thread:
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/47bc6990-00f2-4d85-b3f8-9de03c637f43/visual-studio-2013-update-2rc-stucks-on-creating-system-restore?forum=vssetup
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/81478f51-37bb-4dbb-bd0c-beade1fd50ab/visual-studio-2012-professional-installation-getting-stuck-creating-a-system-restore-point?forum=vssetup
    So here are them:
    1. Check if you have any software which may prevent the installer from running. For example, Windows Defender or any other software which may restrict your permissions.
    2. Just start the computer.
    3. Run the setup as admin.
    4. Check your UAC permissions.
    Best regards,
    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • HT1657 I have rented a movie via apple tv and it gets stuck at 4 minutes left to download. I have not been able to watch and I can't get it to completely download.

    I have rented a movie via apple tv and it gets stuck at 4 minutes left to download. I have not been able to watch and I can't get it to completely download.

    You've tried restarting the film ? If it still skips then try the 'report a problem' link from your purchase history : log into your account on your computer's iTunes via Store > View My Account and you should then see a Purchase History section with a 'see all' link to the right of it ; click on that and you should see a list of your purchases ; find that film and use the 'Report a Problem' link and fill in details about the problem (iTunes support should reply within, I think, 24 to 48 hours).
    Some people have had a problem with the 'report a problem' link (it's been taking people to this site on a browser instead of showing a form in iTunes) - if it does that to you then try contacting iTunes support via this page : http://www.apple.com/support/itunes/contact/- click on Contact iTunes Store Support on the right-hand side of the page.

  • I am trying to uninstall itunes and keep getting a reject that reads "the feature you are trying to use is on a network resource that is unavailable."  Its windows...help!

    I am trying to uninstall itunes and keep getting an error that reads "the feature you are trying to use is on a network resource that is unavailable."  Itunes on windows.  Help!

    (1) Download the Windows Installer CleanUp utility installer file (msicuu2.exe) from the following Major Geeks page (use one of the links under the "DOWNLOAD LOCATIONS" thingy on the Major Geeks page):
    http://majorgeeks.com/download.php?det=4459
    (2) Doubleclick the msicuu2.exe file and follow the prompts to install the Windows Installer CleanUp utility. (If you're on a Windows Vista or Windows 7 system and you get a Code 800A0046 error message when doubleclicking the msicuu2.exe file, try instead right-clicking on the msicuu2.exe file and selecting "Run as administrator".)
    (3) In your Start menu click All Programs and then click Windows Install Clean Up. The Windows Installer CleanUp utility window appears, listing software that is currently installed on your computer.
    (4) In the list of programs that appears in CleanUp, select any iTunes entries and click "Remove", as per the following screenshot:
    (5) Quit out of CleanUp, restart the PC and try another iTunes install. Does it go through properly this time?

  • Firefox connects to google-analytics for some sites, and then gets stuck, and I can never get onto the site. How do I prevent Firefox from connecting to google-analytics? I use XP with service pak 3 in English

    Firefox connects to google-analytics for some sites,when i click on links in websites or emails and then gets stuck, and I can never get onto the site. Or opens a new bower behind the one i'm usind that's blank i don't even know it's there untill i close the one i'm using. How do I prevent Firefox from connecting to google-analytics? Or opening a blank bowser behind the one i'm using. I use XP with service pak 3 in English
    == This happened ==
    A few times a week
    == a couple months ago

    I got the same issue.
    I go on a website and all the sudden another window pops up with "results.google-analytics.com" or "search.google-analytics.com". It has often ads for other sites for example. black single dating site
    how can I can I stop that from happening again?
    I didn't download or do anything, just visit websites, that I visit on a regular basis.
    OS: Windows XP
    Firefox Version: 3.6.6

  • Trying to update iTunes and I get the error - "errors occured while installing the updates. If the problem persists, choose Tools Download only and try installing manually". Currently on version 10.5.3.3 trying to update to 11.0.2 on a Windows 7 64-bit

    Trying to update iTunes and I get the error - "errors occured while installing the updates. If the problem persists, choose Tools > Download only and try installing manually". Currently on version 10.5.3.3 trying to update to 11.0.2 on a Windows 7 64-bit.

    Try updating your iTunes using an iTunesSetup.exe (or iTunes64Setup.exe) installer file downloaded from the Apple website:
    http://www.apple.com/itunes/download/

  • I just updated my iphone 4 to IOS 7.0.4 and it gets stuck backing up on step 2 of 7 and sometimes says step 2 of 4. I've got 2.09 GB free and ive reset it and still wont back up.

    I just updated my iphone 4 to IOS 7.0.4 and it gets stuck backing up on step 2 of 7 and sometimes says step 2 of 4. I've got 2.09 GB free and ive reset it and still wont back up. It also says my phone has never been backed up to the computer, but ive been doing it for 2 years on it. My Ipad 2 also was getting stuck on step 2.

    Hey there Ashleyehall,
    Thank you for your question. It sounds like iTunes is unable to complete a backup of your iPhone. I found this article for you to help troublehsoot the issue named:
    Apple software on Windows: May see performance issues and blank iTunes Store
    http://support.apple.com/kb/TS4123
    Symptoms
    iTunes stops responding when backing up or syncing with an iPhone, iPad, or iPod touch.
    Thank you for using Apple Support Communities.
    Take care,
    Sterling

  • I am trying to download itunes and I get this message: Service apple mobile device failed to start. Verify that you have sufficient privileges to start system services??? what do I do?

    i am trying to download itunes and i get this message: service apple mobile device failed to start. verify that you have suffient privileges to start system services. what do i have to do?

    Hi txatxo
    Thanks for using Apple Support Communities.  You may need to restart the Apple Mobile Device Service.  This article has instructions for doing this:
    How to restart the Apple Mobile Device Service (AMDS) on Windows
    http://support.apple.com/kb/ts1567
    If that doesn't resolve the issue, I would recommend trying a thorough uninstall and reinstall of iTunes as described here:
    Removing and reinstalling iTunes and other software components for Windows Vista, Windows 7, or Windows 8
    http://support.apple.com/kb/HT1923
    Cheers,
    - Ari

  • When trying to install itunes and i get a error that says there is a problem with this windos installer package. a prodram required for this install to complete could not be run. contact you support personnel or package vendor

    when trying to install itunes and i get a error that says there is a problem with this windos installer package. a prodram required for this install to complete could not be run. contact you support personnel or package vendor

    I have the same problem

Maybe you are looking for

  • Hiding fields in contract management SRM.

    Hi, How can we hide fields in the item data in the process contracts. the fields that i need to hide are: product type vendor number. Can we achieve it without using the BADI ??? Thanks, Pradeep. Edited by: Pradeep Purushottam on Apr 22, 2008 11:28 A

  • Stuck with array's NEED HELP!!!

    I have been working at this for two hours now and I need a someone else's opinion other than my own. I need to create a method that finds the row index of the row which contains the largest number divisible evenly by 6. and the column index of the co

  • List the items the user does not have access to.

    I have a requirement to show the user a list of all the libraries of a site with the number of items in each even if the user does not have rights to the library. (the link or button will be grayed out if the user has no access)   I am looking for op

  • Modification of an iview

    1. I need to replace the existing "Change My settings" link with a Z-transaction code 'Zxxxxxxx' in all the roles. How should I go about this...do I need to create another transaction iview? (it would be of gr8 help if any1 of U can give me detailed

  • After installing MAWI error ORA-01031 when trying to connect as sysdba

    I've installed mawi 2.18 on a brand new 10.2.0.5 ORACLE_HOME. After installing the patch i cannot connect anymore to the database as sysdba: the error i got is: ORA-01031: insufficient privileges If i unset TWO_TASK (as stated in mawi install procedu