Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7537843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:57:36+00:00 2026-05-30T06:57:36+00:00

Ok, so im trying to learn some basic c since I need to know

  • 0

Ok, so im trying to learn some basic c since I need to know that in my corrent job. Im used to Java and have a bit of trouble with accesing and changing the value of a struct member.

The program im trying to build is a simple poker client.
I have the following struct:

typedef struct kort{
      int draget;
      char farg;
      int nummer;
      struct kort *next;
  }kort; `

I also have a function named “blandaKort()” which creates 52 members of the struct and add them to a array, assigning a suit and number. Code follows:

void blandaKort(){

   char farg[4]={'S','K','R','J'};
   int nummer[14]={0,2,3,4,5,6,7,8,9,10,11,12,13,14};
   kort kortArray[52]; 
   int tempRaknare=0;
   int i;
   int j;
   kort *huvud=NULL;

   for(i=0; i<=3; i++){
           for(j=1; j<=13; j++){
           kort *huvud=NULL;
            //  kort k;

              kort *k;
              k=(kort*)malloc(sizeof(kort));

              k->farg=farg[i];
              k->nummer=nummer[j];
              k->draget=0;
              huvud=k;

            //  k.farg=farg[i];
            //  k.nummer=nummer[j];
              kortArray[tempRaknare]=*k;
              tempRaknare++;  
         }
 }
 tempRaknare, i, j =0;

 delaHand(kortArray);

}

The function delaHand() takes the array of cards and and choose two random cards. What im trying to accomplish is the set my flag “draget” to 1, which tells me which cards are drawn. Code follows:

void delaHand(kort kortArray[]){

 srand(time(NULL));
 int x = rand() % 52 + 1;
 int y = rand() %52+1;

 kort *k;
 k=(kort*)malloc(sizeof(kort));

 kort kortHand[2];
 //if(kortArray[x].draget!=1){
     kortHand[0]=kortArray[x];
     *k=kortArray[x];
     k->draget=1;
     kortArray[x]=*k;

   //  }
 //if(kortArray[y].draget!=1){
     kortHand[1]=kortArray[y];
     kortArray[y].draget=1;

…..

when im printing the members of kortHand[] it shows the correct suit and number, but the variable draget remains unchanged.
cheers

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:57:37+00:00Added an answer on May 30, 2026 at 6:57 am

    The following is functionally equivalent to the code you posted. I have made a number of changes, though, so please point out any bits that need clarifying.

    typedef struct _kort {
        int draget;
        char farg;
        int nummer;
        struct _kort *next;
    } kort;
    
    void blandaKort(void){
    
        char farg[4] = { 'S', 'K', 'R', 'J' };
        kort kortArray[52];
        int i;
    
        for(i = 0; i < 52; i++){
            kortArray[i].farg   = farg[i / 13];
            kortArray[i].nummer = i % 13 + 2;
            kortArray[i].draget = 0;
        }
    
        delaHand(kortArray);
    }
    
    void delaHand(kort kortArray[]){
    
        srand(time(NULL));
        int x = rand() % 52 + 1;
        int y = rand() % 52 + 1;
    
        // Note: the valid indexes of kortArray are 0 - 51.
        // x and y will have values that range from 1 - 52.
        // This is probably not what you want.
    
        kortArray[x].draget = 1;
        kortArray[y].draget = 1;
    
        // ...
    }
    

    Your basic problem was in copying a struct when you wanted to merely copy the pointer. E.g. *k = kortArray[x]; copies the values of the struct, so that you now have pointers to two structs that happen to have the same values. In contrast, k = &(kortArray[x]); would result in two pointers that both point to the same struct in memory. The above code avoids that issue by simply always directly referring to the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn some of the basic and advanced features of visual
I am trying to learn some VB.NET for my coo-op that starts next week,
I'm trying to do a little PDO CRUD to learn some PDO. I have
I have been trying to learn Erlang and have been running into some problems
I'm trying to learn some basic regular expression and having a hard time getting
I am trying to learn and use html5 and have a basic layout but
I am trying to learn some things from AJAX and jQuery, i believe that
I'm trying to learn some basic ajax using Django. My simple project is an
I'm starting to learn socket.io and node.js I'm trying to do some pretty basic
I'm trying to have a site that has the basic structure: <1 div> <3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.