Thread: how do I set char a[j] = *char b[d]?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    how do I set char a[j] = *char b[d]?

    Hello everyone...

    I've played around with my code from a previous posting (outputing spintf to array) to make things a bit easier to figure out.

    I'm trying to populate one character array with data from a second that's constantly changing. In the case below, the array "char decktype" is what I'm trying to populate. The second array "char value" will be changing as it goes through the FOR loop.

    But, it's not allowing me to do this. I'm running code blocks on a windows machine.

    Does anyone have any suggestions? Thanks very much for any help!

    Code:
    #include <stdio.h>
    #include <string.h>
    main(){
    
    int d, j;
    d = 0;
    
    char decktype[312]; // Controlled by int j - Will store type of card
    char *value[13] = {"Ace", "Deuce", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; //controlled by int d
    
    
    for (j=0; j<=311; j++)
        {
          decktype[j]=*value[d];
              d++;
            if (d>12) {d=0} //Resets the card value to Ace after King has been assigned.
        }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    Code:
     if (d>12) {d=0;} // fixed missing semicolon
    it runs fine on my machine. (which means i tested it for being able to compile and run)

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why not use strcpy?
    And main returns int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    What's wrong with decktype[j] = value[d]; ? Why the * at all?

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    5
    Hi and thanks so far for everyone's help....

    Bladactania: When I remove the * and try to set decktype[j] = value[d]; I'm presented with an error of "Invalid conversion from char* to char." This error disappears when I use *value[d].

    Elysia: If I try to use strcpy as you mentioned, I don't think it's copying the data over. The program crashes before anything is printed on the screen.

    Thanks for your help, to everyone who has posted... I'm trying to teach myself and I appreciate all your efforts! Also... I'm not so sure what I'd use in place of the main () command in this instance.... I thought it was a standard beginning...

    Code:
    #include <stdio.h>
    #include <string.h>
    main(){
    
    int d, j;
    d = 0;
    
    char decktype[312]; // Controlled by int j - Will store type of card
    char *value[13] = {"Ace", "Deuce", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; //controlled by int d
    
    
    for (j=0; j<=311; j++)
        {
        strcpy (decktype[j], value[d]);
        printf ("%s\n", decktype[j]);
        d++;
        if (d>12) {d=0;} //Resets the card value to Ace after King has been assigned.
        }
    
    return 0;}

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I've been really good to you in doing this. Which i don't normally do. Now don't just copy and paste it. Sit down and go through the changes which I've made. And your need to learn the code indentation.

    Code:
    for( ; j<311 ; j++ )
      {
         strcpy(decktype[j],value[d]);
         d++;
         
         if(d==13) 
           d=0;
      }
    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not ONE of the posted bits of code actually make sense.

    What is the char decktype[312] supposed to be - a 6-char string with the name of the card? If so, it would make more sense to have decktype[52][6] - that's 52 strings of 6 chars each [5 actual chars, and a terminating zero, that is].

    Another option is to have char *decktype[52], and just have a pointer to the constants in value. You have no intention of actually modifying the values within the strings, do you? [Like, you are not going to do: decktype[x] = "d", decktype[x+1] = "u", and expect to write "duck" instead of "jack", right?] - if you intend to change the actual strings, then this method won't work. But if all you want to do is have something you can print, then using a constant string is fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by todd_v View Post
    Elysia: If I try to use strcpy as you mentioned, I don't think it's copying the data over. The program crashes before anything is printed on the screen.
    Look closely:
    strcpy (decktype[j], value[d]);
    dectype[j] is a CHAR, but strcpy needs the address (ie a pointer) to where to copy the characters.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    Sweet satisfaction!

    Hi , and thanks again to everyone who has helped with this... especially you, Elysia....

    It's working now, the end result looks like this:

    Code:
    #include <stdio.h>
    #include <string.h>
    main(){
    
    int d, j;
    d = 0;
    
    char *decktype[312] = {};
    char *value[13] = {"Ace", "Deuce", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    
    for (j=0; j<=311; j++)
        {
        decktype[j] = value[d];
        printf ("%d %s\n", j, decktype[j]);
        d++;
        if (d>12) {d=0;} //Resets the card value to Ace after King has been assigned.
        }
    
    return 0;}
    You guys are the best!

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that string literals such as what you have should really be const char: SourceForge.net: Common mistakes and errors - cpwiki
    Main should return int: SourceForge.net: Implicit main - cpwiki
    And your loop is quite unnecessarily complicated. You are copying (or rather assigning) from value[j] to decktype[j] (ie, same index) all the time, so you can get rid of d and make it loop for only as big as value is and then shorten down decktype, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM