Thread: Struct Char Array Problems

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    My Laptop
    Posts
    14

    Question Struct Char Array Problems

    I am trying to put a char array into a struct but everything I have tried has not worked.

    Code:
    typedef struct{
    int Grid_X;
    int Grid_Y;
    char Room_Name[30];
    int North;
    int South;
    int East;
    int West;
    int Warp;
    int Warp_X;
    int Warp_Y;
    int Chest;
    }Room;
    
    Room RG; //in main
    
    char Name[30] = (*RG).Room_Name; //line 71
    //In another function
    
    void set_room_start(Room *RG){
    (*RG).Grid_X = 0;
    (*RG).Grid_Y = 0;
    (*RG).Room_Name[30] = "Start Room"; //line 14
    (*RG).North = 1;
    (*RG).South = 1;
    (*RG).East = 1;
    (*RG).West = 1;
    (*RG).Warp = 1;
    (*RG).Warp_X = 5;
    (*RG).Warp_Y = 0;
    (*RG).Chest = 1;
    }

    That gives me warnings:
    "14 E:\Code\Source Code\Game Commands.c [Warning] assignment makes integer from pointer without a cast"

    "71 E:\Code\Source Code\Game Commands.c array initialized from non-constant array expression"

    Could some one point out the mistake I am making in my struct code? Thanx in advance
    RMDan

    Edit:
    For those who may be picky, Yes I did pas the address of RG to my functions.
    Last edited by RMDan; 06-18-2009 at 01:52 PM.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    arrays don't work that way
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Looks like maybe you need to use the strcpy function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    (*RG).Grid_X = 0;
    It is quite a bit easier to just write:
    Code:
    RG->Grid_X = 0;
    And as others have stated, read up on how to use strcpy().

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    #1 tip about the number one:

    Get one of something to actually work before you code ten of them the same, completely wrongly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    My Laptop
    Posts
    14
    I forgot about strcpy. That makes a huge difference.

    hk_mp5kpdw:
    Thanks that got rid of the errors

    bithub:
    Thanks for the tip. Going to change that in my engine later.

    MK27:
    My Struct is perfectly fine except for one line. I got the ints to work for me then I attempted to put a char in the Struct. So basically I did what you said so don't post criticism.

    RMDan
    Last edited by RMDan; 06-19-2009 at 12:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM